-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
197 lines (191 loc) · 6.12 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlBeautifyPlugin = require('html-beautify-webpack-plugin');
const settings = {
name: 'Harmony',
devServerUrl: 'http://localhost:8080',
jsEntry: {
'grid-tools': './src/grid-tools',
'demo-index': './demoSrc/index.scss',
},
destination: path.resolve(__dirname),
demoSrc: path.resolve(__dirname, 'demoSrc'),
};
// Configure the twig loader
const configureTwigLoader = () => {
return {
test: /\.twig$/,
loader: 'twig-loader',
options: {
debug: true,
// Twig options - eg: autoescape: true
// https://twig.symfony.com/doc/2.x/api.html
},
};
};
// Configure Babel loader
const configureBabelLoader = () => {
return {
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
modules: false,
useBuiltIns: 'usage',
corejs: '3.0.1',
},
],
],
},
},
};
};
// Configure Babel loader
const configureHtmlLoader = () => {
return {
test: /\.html$/,
loader: require.resolve('file-loader'),
options: {
name: '[name].[ext]',
},
};
};
// Configure the stylesheet loader
const configureStylesheetLoader = isProduction => {
return isProduction
? {
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader, // 4. Convert the JS to a CSS file
{
loader: 'css-loader', // 3. Convert CSS to JS object
options: {
importLoaders: 2,
sourceMap: true,
},
},
{
loader: 'postcss-loader', // 2. Run CSS through PostCss
options: {
sourceMap: true,
},
},
'sass-loader', // 1. Convert SCSS to CSS
],
}
: {
test: /\.(sa|sc|c)ss$/,
use: [
'style-loader', // 4. Insert hot CSS into the page
'css-loader', // 3. Convert CSS to JS object
{
loader: 'postcss-loader', // 2. Run CSS through PostCss
options: {
plugins: [require('autoprefixer')],
},
},
'sass-loader', // 1. Convert SCSS to CSS
],
};
};
module.exports = (env, argv) => {
const isProduction = argv.mode === 'production';
return {
entry: settings.jsEntry,
output: {
path: settings.destination,
libraryTarget: 'umd',
library: 'lib',
umdNamedDefine: true,
globalObject: `(typeof self !== 'undefined' ? self : this)`,
},
optimization: {
minimizer: [
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true,
}),
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
map: {
inline: false,
annotation: true,
},
safe: true,
discardComments: true,
},
}),
],
},
devServer: {
public: settings.devServerUrl,
contentBase: path.resolve(__dirname, settings.demoSrc),
quiet: true,
stats: 'errors-only',
host: '0.0.0.0',
disableHostCheck: true,
},
module: {
rules: [
configureHtmlLoader(),
configureTwigLoader(),
configureBabelLoader(),
configureStylesheetLoader(isProduction),
],
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [
path.resolve(__dirname, 'demoBuilt'),
],
verbose: false, // disable logging
root: path.resolve(__dirname, '/'),
}),
new FriendlyErrorsWebpackPlugin({
compilationSuccessInfo: {
messages: [
`The ${settings.name} demo is running at: ${settings.devServerUrl}`,
],
},
onErrors: (severity, errors) => {
if (severity !== 'error') return;
const error = errors[0];
console.log(error.message);
},
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: path.resolve(
__dirname,
settings.demoSrc,
`./index.twig`
),
}),
new HtmlWebpackPlugin({
filename: 'introduction.html',
template: path.resolve(
__dirname,
settings.demoSrc,
`./introduction.twig`
),
}),
new HtmlBeautifyPlugin(),
isProduction
? new MiniCssExtractPlugin({
filename: '[name].css',
})
: () => {},
],
};
};