-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.config.js
297 lines (270 loc) · 10.8 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// initial structure from: https://github.com/patternfly/patternfly-demo-app/blob/master/build/webpack.config.js
// more from: https://github.com/webpack-contrib/mini-css-extract-plugin
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const WriteFilesPlugin = require('write-file-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const BabelEnvDeps = require('webpack-babel-env-deps');
const CompressionPlugin = require('compression-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
// path to assets
const ASSET_PATH = process.env.ASSET_PATH || '/';
module.exports = (env, argv) => {
// get dev mode
const devMode = argv.mode === "development";
const statsMode = argv.stats === "true";
// output
console.log("build flags dev=" + devMode + ", stats=" + statsMode);
// return dynamic config based on options
return {
name: "gudgeon",
context: __dirname + "/web/",
entry: {
"gudgeon": "./app/js/gudgeon-index.js"
},
cache: devMode,
output: {
publicPath: ASSET_PATH,
path: __dirname + "/web/static",
filename: "js/[name].bundle.js",
hotUpdateChunkFilename: 'hot/[id].[hash].hot-update.js',
hotUpdateMainFilename: 'hot/[hash].hot-update.json'
},
// having consistency issues with watching for file changes so this
// came from stackoverflow initially (https://stackoverflow.com/questions/26708205/webpack-watch-isnt-compiling-changed-files)
watchOptions: {
poll: true,
ignored: ['node_modules']
},
optimization: {
// optimization flags
usedExports: true,
minimize: true,
concatenateModules: true,
mergeDuplicateChunks: true,
providedExports: true,
// various minimizers
minimizer: [
new TerserJSPlugin(),
new OptimizeCssAssetsPlugin({
cssProcessor: require('cssnano'),
cssProcessorPluginOptions: {
preset: ['default', {discardComments: {removeAll: true}}],
},
canPrint: true
})
]
},
plugins: [
// Avoid publishing files when compilation failed:
new webpack.NoEmitOnErrorsPlugin(),
//HMR
new webpack.HotModuleReplacementPlugin(),
//make webpack ignore moment locale require: https://github.com/moment/moment/issues/2517
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
//creates distribution css file rather than inlining styles
new MiniCssExtractPlugin({
filename: "css/[name].bundle.css",
chunkFilename: "css/" + (!devMode ? '[id].bundle.css' : '[id].[hash].bundle.css'),
}),
new CopyWebpackPlugin([
// copy favicon.ico, basically
{
from: {glob: __dirname + '/web/app/ico/*.ico'},
to: __dirname + '/web/static/',
flatten: true
},
// copy raw html if needed
{
from: {glob: __dirname + '/web/app/html/*.html'},
to: __dirname + '/web/static/',
flatten: true
},
// copy html templates
{
from: {glob: __dirname + '/web/app/html/*.tmpl'},
to: __dirname + '/web/static/',
flatten: true
},
// copy images from app
{
from: {glob: __dirname + '/web/app/img/*'},
to: './img',
flatten: true
},
// copy patternfly assets for app
{
from: {glob: './node_modules/@patternfly/patternfly/assets/images/*.*'},
to: './img',
flatten: true
},
]),
// writes files on changes to src
new WriteFilesPlugin(),
// bundle analyzer
new BundleAnalyzerPlugin({
analyzerMode: devMode ? 'server' : (statsMode ? 'static' : 'disabled'),
reportFilename: "../../build/report.html",
generateStatsFile: false,
openAnalyzer: devMode,
}),
// add compression plugin
new CompressionPlugin({
test: /\.(js|css|html|svg|eot|ttf|woff|woff2)$/,
deleteOriginalAssets: !devMode && !statsMode,
threshold: 4092,
minRatio: 0.90
}),
// lodash module replacement
new LodashModuleReplacementPlugin({
"currying": true,
"flattening": true,
"paths": true,
"placeholders": true,
"shorthands": true
})
],
module: {
rules: [
//js loader
{
// Only run `.js` and `.jsx` files through Babel
test: /\.jsx?$/,
// don't load modules that don't need transpilation
exclude: [
BabelEnvDeps.exclude()
],
resolve: {
extensions: ['.js', '.json']
},
use: [
{
loader: "babel-loader",
options: {
plugins: [
"lodash",
'@babel/plugin-proposal-class-properties',
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-transform-typescript',
'babel-plugin-typescript-to-proptypes',
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-object-rest-spread',
],
presets: [
'@babel/preset-react',
'@babel/preset-env'
]
}
}
]
},
// load css
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: devMode
}
},
{
loader: "css-loader",
options: {
sourceMap: devMode,
importLoaders: 1
}
},
{
loader: "postcss-loader",
options: {
sourceMap: devMode
}
}
]
},
// font/image url loaders
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'svg-url-loader',
options: {
limit: 16 * 1024,
mimetype: "image/svg+xml",
name: "img/[name].[ext]"
}
}
]
},
{
test: /\.(woff)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 16 * 1024,
mimetype: "application/font-woff",
name: "fonts/[name].[ext]"
}
}
]
},
{
test: /\.(woff2)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 16 * 1024,
mimetype: "application/font-woff2",
name: "fonts/[name].[ext]"
}
}
]
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 16 * 1024,
mimetype: "application/octet-stream",
name: "fonts/[name].[ext]"
}
}
]
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 16 * 1024,
mimetype: "application/vnd.ms-fontobject",
name: "fonts/[name].[ext]"
}
}
]
},
{
test: /\.(png|jpe?g|gif)(\?\S*)?$/,
use: [
{
loader: 'url-loader',
options: {
limit: 8 * 1024,
name: "img/[name].[ext]"
}
}
]
}
]
}
};
};