-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwebpack.config.js
454 lines (413 loc) · 11.7 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
'use strict'
// const requireSoSlow = require('require-so-slow')
const path = require('path')
const webpack = require('webpack')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const CopyWebpackPlugin = require('copy-webpack-plugin')
const ZipPlugin = require('zip-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const prerender = require('./prerender')
//const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
//const ErrorOverlayPlugin = require('error-overlay-webpack-plugin')
// const Critters = require('critters-webpack-plugin')
const glob = require('fast-glob')
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const PurgecssPlugin = require('purgecss-webpack-plugin')
// const ReplacePlugin = require('webpack-plugin-replace')
const replaceBuffer = require('replace-buffer')
//const WebpackDeepScopeAnalysisPlugin = require('webpack-deep-scope-plugin').default
//const ShakePlugin = require('webpack-common-shake').Plugin
const pureFuncs = require('side-effects-safe').pureFuncsWithUnusualException // pureFuncsWithUsualException
// const { DuplicatesPlugin } = require('inspectpack/plugin')
//const DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin')
// const WebpackMonitor = require('webpack-monitor')
// const AutoDllPlugin = require('autodll-webpack-plugin')
// const PrepackWebpackPlugin = require('prepack-webpack-plugin').default
// Profiling
// const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
// const smp = new SpeedMeasurePlugin({ granularLoaderData: false })
// const CpuProfilerWebpackPlugin = require('cpuprofile-webpack-plugin');
const ENV = process.env.NODE_ENV || 'development'
const isProduction = ENV === 'production'
const STATS = process.env.STATS_ENABLE !== undefined ? !!process.env.STATS_ENABLE : false // @TODO: Enable for stats
const html = {
alwaysWriteToDisk: true,
cache: true,
inject: 'head',
minify: isProduction
? {
collapseWhitespace: true,
removeComments: false,
removeEmptyAttributes: false,
removeRedundantAttributes: false,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: false,
}
: false,
ssr: params => {
return isProduction ? prerender('dist', params) : ''
},
template: 'index.ejs',
// hash: true,
title: 'Improved Layout for Instagram', // @TODO: Replace with https://github.com/GoogleChromeLabs/prerender-loader
}
const plugins = [
//new webpack.ProgressPlugin(),
new HtmlWebpackPlugin(html),
new CopyWebpackPlugin({
patterns: [
//{ from: '*.html' },
{
from: '*.json',
transform: (content, path) => {
if (!isProduction || !path.includes('manifest.json')) return content
return replaceBuffer(
content,
'"content_security_policy": { "extension_pages": "script-src \'self\' \'unsafe-eval\' http://localhost:8080; object-src \'self\'" },',
''
)
},
},
{ from: 'img/*.png' },
{ from: 'img/*.webp' },
{ from: 'content/*' },
{ from: '_locales/**' },
],
}),
]
if (isProduction) {
pureFuncs.push(
'classCallCheck',
'_classCallCheck',
'_possibleConstructorReturn',
'Object.freeze',
'invariant',
'classnames',
'value-equal',
'valueEqual',
'resolve-pathname',
'resolvePathname',
'warning',
'proptypes'
)
plugins.push(
/*new DuplicatePackageCheckerPlugin({
emitError: true,
verbose: true,
showHelp: true,
strict: true,
}),
new DuplicatesPlugin({
// Emit compilation warning or error? (Default: `false`)
emitErrors: false,
// Display full duplicates information? (Default: `false`)
verbose: false,
}),*/
// new webpack.IgnorePlugin(/prop-types$/),
// new Critters(),
// strip out babel-helper invariant checks
/*new ReplacePlugin({
patterns: [
{
regex: /throw\s+(new\s+)?(Type|Reference)?Er{2}or\s*\(/g,
value: 'return;(',
},
{
regex: /"use strict"/g,
value: '',
},
{
regex: /(e=>/,
value: '("use strict";e=>',
},
],
}),*/
/*new PrepackWebpackPlugin({
prepack: { delayUnsupportedRequires: true, abstractEffectsInAdditionalFunctions: true, reactEnabled: true },
}), // 04.05.18: Not compatible with Webpack 4; 28.01.2018: Error: PP0001: This operation is not yet supported on document at createAttributeNS at 1:49611 to 1:49612
*/
new MiniCssExtractPlugin(),
new PurgecssPlugin({
paths: glob.sync([`src/**`, `dist/**`], {
ignore: ['content/*'],
onlyFiles: true,
}),
safelist: [/col-/, /btn-/],
}),
//new ShakePlugin(), // @todo: Broken in webpack5
//new WebpackDeepScopeAnalysisPlugin(), // @todo: 25/10/2018 - doesn't reduce bundle size
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: '../report.html',
}),
/*new WebpackMonitor({
capture: true,
launch: true,
target: '../stats.json',
}),*/
/*new AutoDllPlugin({ // disabled as per https://github.com/mzgoddard/hard-source-webpack-plugin/issues/251
inject: true, // will inject the DLL bundles to index.html
filename: '[name]_[hash].js',
entry: {
vendor: ['nervjs', 'nerv-devtool', 'decko'],
},
}),*/
new ZipPlugin({
exclude: 'ssr-bundle.js',
filename: 'dist.zip',
path: '../',
})
)
} else {
plugins.push(
new HtmlWebpackHarddiskPlugin({
outputPath: path.resolve(__dirname, 'dist'),
}),
new FriendlyErrorsPlugin(),
new CaseSensitivePathsPlugin()
/*new AutoDllPlugin({ // disabled as per https://github.com/mzgoddard/hard-source-webpack-plugin/issues/251
inject: true, // will inject the DLL bundles to index.html
filename: '[name]_[hash].js',
entry: {
vendor: ['nervjs', 'nerv-devtool', 'decko'],
},
}),*/
)
}
/*plugins.push(
new HardSourceWebpackPlugin({
cacheDirectory: '../node_modules/.cache/hard-source/[confighash]',
})
)*/
const first = {
cache: {
buildDependencies: {
config: [__filename],
},
type: 'filesystem',
},
context: path.join(__dirname, 'src'),
devServer: {
compress: false,
contentBase: path.join(__dirname, 'dist'),
disableHostCheck: true,
headers: {
'Access-Control-Allow-Headers': 'X-Requested-With, content-type, Authorization',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Access-Control-Allow-Origin': '*',
},
host: 'localhost',
hot: true,
overlay: {
errors: true,
warnings: true,
},
port: 8080,
watchContentBase: false,
writeToDisk: filePath => {
return /(content\/|manifest.json|_locales|img)/.test(filePath)
},
},
devtool: isProduction ? false /* 'cheap-module-source-map'*/ /*'source-map'*/ : 'inline-cheap-module-source-map',
entry: {
app: './index.js',
feed: './feed.js',
},
mode: isProduction ? 'production' : 'development',
module: {
/*noParse: isProduction
? undefined
: [
// faster HMR
//new RegExp(nerv),
//new RegExp('.min.js'),
],*/
rules: [
{
exclude: /node_modules/,
loader: 'babel-loader',
options: {
cacheDirectory: true,
},
test: /\.jsx?$/i,
},
{
test: /\.cs{2}$/, // .css
use: [
isProduction
? {
loader: MiniCssExtractPlugin.loader,
}
: 'style-loader',
'css-loader',
],
},
// This rule fixes an issue where @quilted/preact-mini-compat does not use
// fully specified paths for its imports (e.g., uses bare/ relative import
// specifiers without the extension).
{
test: /\.mjs$/,
resolve: {
fullySpecified: false,
},
},
],
},
node: isProduction ? false : undefined,
optimization: isProduction
? {
minimizer: [
new TerserPlugin({
exclude: /content\//,
extractComments: false,
terserOptions: {
compress: {
arguments: true,
hoist_funs: true,
keep_infinity: true,
negate_iife: false, // test
pure_funcs: pureFuncs, // test
unsafe: true, // @fixme: Breaks report.html
unsafe_arrows: true,
unsafe_Function: true,
unsafe_methods: true,
unsafe_proto: true,
unsafe_regexp: true,
unsafe_undefined: true,
},
ecma: 8,
output: {
comments: false,
semicolons: false, // size before gzip could be smaller; size after gzip insignificantly larger
wrap_iife: true,
},
toplevel: true,
},
}),
new OptimizeCssAssetsPlugin({
cssProcessorPluginOptions: {
preset: [
'advanced',
{
discardComments: {
removeAll: true,
},
},
],
},
}),
],
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
commons: {
chunks: 'initial',
minChunks: 2,
name: 'commons',
},
styles: {
chunks: 'all',
name: 'main',
test: module => {
return module.nameForCondition && /\.cs{2}$/.test(module.nameForCondition()) && module.type.startsWith('javascript')
},
},
},
},
}
: {
runtimeChunk: 'single',
sideEffects: false, // faster dev builds
splitChunks: {
cacheGroups: {
commons: {
chunks: 'initial',
minChunks: 2,
name: 'commons',
},
vendor: {
chunks: 'all',
name: 'vendors',
test: /[/\\]node_modules[/\\]/,
},
},
},
},
output: {
filename: '[name].bundle.js',
path: path.join(__dirname, 'dist'),
pathinfo: false,
publicPath: isProduction ? '' : 'http://localhost:8080/', // @todo: check if false does impact development
//devtoolModuleFilenameTemplate: info => (isProd ? path.relative('/', info.absoluteResourcePath) : `webpack:///${info.resourcePath}`),
},
performance: {
hints: isProduction ? 'warning' : false,
}, //'nosources-source-map',
plugins,
recordsPath: path.resolve(__dirname, './records.json'),
resolve: {
alias: isProduction
? {
'create-react-class': 'preact-compat/lib/create-react-class',
'prop-types$': 'proptypes/disabled',
'react-dom/test-utils': 'preact/test-utils',
'react-dom-factories': 'preact-compat/lib/react-dom-factories',
react$: '@quilted/preact-mini-compat',
'react-dom$': '@quilted/preact-mini-compat',
}
: {
'create-react-class': 'preact-compat/lib/create-react-class',
react: 'preact/compat',
'react-dom': 'preact/compat',
'react-dom/test-utils': 'preact/test-utils',
'react-dom-factories': 'preact-compat/lib/react-dom-factories',
},
extensions: ['.mjs', '.js', '.json'],
},
stats:
isProduction && STATS
? {
reasons: true,
}
: {}, // can't be 'none' as per parallel-webpack
watch: !isProduction,
experiments: {
//futureDefaults: true,
//backCompat: false,
},
}
const second = {
cache: {
buildDependencies: {
config: [__filename],
},
type: 'filesystem',
},
context: first.context,
entry: ['./components/App'],
mode: 'development',
module: first.module,
output: {
filename: 'ssr-bundle.js',
libraryTarget: 'commonjs2',
path: path.join(__dirname, 'dist'),
},
recordsPath: path.resolve(__dirname, './records_html.json'),
resolve: first.resolve,
target: 'node',
experiments: {
//futureDefaults: true,
//backCompat: false,
},
}
// @TODO: https://blog.box.com/blog/how-we-improved-webpack-build-performance-95/
// requireSoSlow.write('require-trace.trace')
// module.exports = isProd ? [smp.wrap(first), second] : first
module.exports = isProduction ? [first, second] : first