-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathwebpack.client.babel.ts
421 lines (372 loc) · 10.7 KB
/
webpack.client.babel.ts
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
import '../dotenv'
import path from 'path'
import glob from 'glob-all'
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import express from 'express'
import ExtraWatchWebpackPlugin from 'extra-watch-webpack-plugin'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import LodashWebpackPlugin from 'lodash-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import OptimizeCssAssetsPlugin from 'optimize-css-assets-webpack-plugin'
import PreloadWebpackPlugin from 'preload-webpack-plugin'
// import PurgecssPlugin from 'purgecss-webpack-plugin'
import SizePlugin from 'size-plugin'
import kill from 'tree-kill'
import webpack from 'webpack'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import webpackCompression from './lib/webpackCompression'
import webpackFriendlyConsole from './lib/webpackFriendlyConsole'
import webpackLoadAssets from './lib/webpackLoadAssets'
import webpackLoadJavascript from './lib/webpackLoadJavascript'
import webpackLoadStyles from './lib/webpackLoadStyles'
import webpackStylelint from './lib/webpackStylelint'
import webpackTerser from './lib/webpackTerser'
import webpackTsChecker from './lib/webpackTsChecker'
import { findModuleRoot } from '../../lib/findModuleRoot'
import { getenv } from '../../lib/getenv'
import htmlTags from './lib/htmlTags'
import babelConfig from '../../babel.config'
process.once('SIGINT', () => {
kill(process.pid, 'SIGTERM')
process.exit(0)
})
process.once('SIGTERM', () => {
kill(process.pid, 'SIGTERM')
process.exit(0)
})
const MODE: 'development' | 'production' = getenv('NODE_ENV') === 'development' ? 'development' : 'production' // prettier-ignore
const production = MODE === 'production'
const development = MODE === 'development'
const analyze = getenv('ANALYZE', '0') === '1'
const profile = getenv('PROFILE', '0') === '1'
const debuggableProd = getenv('DEBUGGABLE_PROD', '0') === '1'
const sourceMaps = true
const devServerPort = getenv('WEB_DEV_SERVER_PORT', '3000')
const analyzerPort = parseInt(getenv('WEB_DEV_BUNDLE_ANALYZER_PORT', '8888'), 10) // prettier-ignore
const fancyConsole = getenv('DEV_FANCY_CONSOLE', '0') === '1'
const fancyClearConsole = getenv('DEV_FANCY_CLEAR_CONSOLE', '0') === '1'
const disableLint = getenv('DEV_DISABLE_LINT', '0') === '1'
const { moduleRoot, pkg } = findModuleRoot()
const buildPath = path.join(moduleRoot, '.build', analyze ? 'analyze' : MODE, 'web') // prettier-ignore
function alias(development: boolean) {
let productionAliases = {}
if (profile) {
productionAliases = {
...productionAliases,
'react-dom$': 'react-dom/profiling',
'scheduler/tracing': 'scheduler/tracing-profiling',
}
}
if (development) {
productionAliases = {
...productionAliases,
}
}
return productionAliases
}
function entry(development: boolean, entries: string[]) {
if (development || debuggableProd) {
return [
'map.prototype.tojson', // to visualize Map in Redux Dev Tools
'set.prototype.tojson', // to visualize Set in Redux Dev Tools
...entries,
]
}
return entries
}
function outputFilename(development: boolean, ext = 'js') {
if (development || analyze) {
return `content/[name].${ext}`
}
if (debuggableProd) {
return `content/[name].[chunkhash:8].${ext}`
}
return `content/[chunkhash:8].${ext}`
}
export default {
mode: MODE,
bail: true,
name: pkg.name,
target: 'web',
devtool: false,
stats: fancyConsole
? false
: {
all: false,
errors: true,
warnings: true,
moduleTrace: true,
colors: true,
},
performance: {
hints: false,
},
entry: entry(development, [path.join(moduleRoot, `src/index.polyfilled.ts`)]),
output: {
path: buildPath,
filename: outputFilename(development),
chunkFilename: outputFilename(development),
publicPath: '/',
hashDigest: 'hex',
hashDigestLength: 16,
hashFunction: 'sha256',
pathinfo: false,
},
devServer: {
contentBase: path.join(buildPath, '..'),
before: (app: express.Application) => {
app.use(express.static(path.join(buildPath, '..', 'sourcemaps')))
app.use(express.static(path.join(moduleRoot, 'static')))
},
compress: true,
disableHostCheck: true,
historyApiFallback: true,
host: '0.0.0.0',
hot: true,
lazy: false,
overlay: {
warnings: false,
errors: true,
},
port: devServerPort,
publicPath: '/',
quiet: false,
logLevel: 'info',
clientLogLevel: 'warning',
writeToDisk: true,
},
module: {
rules: [
...webpackLoadJavascript({
babelConfig,
options: { caller: { target: 'web' } },
sourceMaps,
transpiledLibs: [
'@loadable',
'@redux-saga',
'delay',
'immer',
'lodash',
'p-min-delay',
'react-router',
'redux/es',
],
}),
...webpackLoadStyles({
isDev: development,
isServer: false,
sourceMap: sourceMaps,
}),
...webpackLoadAssets({
isDev: development,
inlineSmallerThan: 0,
subdirectory: 'assets/',
}),
],
},
resolve: {
symlinks: false,
mainFields: [
'source',
'ts:main',
'ts:module',
'tsmain',
'tsmodule',
'module',
'jsnext:main',
'esmodule',
'es:module',
'esm',
'es2015',
'main',
],
extensions: [
'.wasm',
'.ts',
'.tsx',
'.es.js',
'.mjs',
'.web.js',
'.web.jsx',
'.js',
'.jsx',
'.md',
'.mdx',
],
alias: alias(development),
},
plugins: [
new HtmlWebpackPlugin({
filename: path.join(buildPath, 'index.html'),
inject: true,
template: path.join(moduleRoot, 'src', 'index.ejs'),
vars: {
title: getenv('WEB_APP_NAME_FRIENDLY'),
},
}),
new PreloadWebpackPlugin({
rel: 'preload',
include: ['main', 'pages/Home'],
fileBlacklist: [/\.map/, /\.hot-update\./],
}),
!analyze &&
new ExtraWatchWebpackPlugin({
files: ['src/types/**/*.d.ts'],
dirs: [],
}),
!disableLint && !analyze && webpackStylelint(),
!disableLint &&
!analyze &&
webpackTsChecker({
memoryLimit: 1024,
tslint: path.join(moduleRoot, 'tslint.json'),
tsconfig: path.join(moduleRoot, 'tsconfig.json'),
reportFiles: [
'src/**/*.{js,jsx,ts,tsx}',
'!src/**/__tests__/**/*.{js,jsx,ts,tsx}',
'!src/**/*.(spec|test).{js,jsx,ts,tsx}',
'!static/**/*',
],
}),
new webpack.EnvironmentPlugin({
BABEL_ENV: process.env.BABEL_ENV,
DEBUGGABLE_PROD: process.env.DEBUGGABLE_PROD,
NODE_ENV: process.env.NODE_ENV,
}),
...(fancyConsole
? webpackFriendlyConsole({
clearConsole: !analyze && fancyClearConsole,
projectRoot: path.resolve(moduleRoot),
packageName: pkg.name || 'web',
progressBarColor: 'red',
})
: []),
new MiniCssExtractPlugin({
filename: outputFilename(development, 'css'),
chunkFilename: outputFilename(development, 'css'),
}),
development && new ReactRefreshWebpackPlugin({ disableRefreshCheck: true }),
production &&
!analyze &&
new CopyWebpackPlugin([{ from: './static', to: './' }]),
...(production && !analyze
? webpackCompression({
cache: true,
exclude: [/.*\.map$/],
brotli: false,
})
: []),
// new PurgecssPlugin({
// paths: () => [
// ...glob.sync(`${path.join(moduleRoot, 'src')}/**/*.{jsx,tsx}`, { nodir: true }), // prettier-ignore
// ],
// whitelist: htmlTags,
// }),
new LodashWebpackPlugin({
caching: true,
chaining: true,
cloning: true,
coercions: true,
collections: true,
currying: true,
deburring: true,
exotics: false,
flattening: true,
guards: true,
memoizing: true,
metadata: true,
paths: true,
placeholders: true,
shorthands: true,
unicode: false,
}),
// Setup for `moment` locales
new webpack.ContextReplacementPlugin(/^\.\/locale$/, context => {
if (!context.context.includes('/moment/')) {
return
}
// context needs to be modified in place
Object.assign(context, {
// include only CJK
regExp: /^\.\/(en|de)/,
// point to the locale data folder relative to moment's src/lib/locale
request: '../../locale',
})
}),
new webpack.optimize.AggressiveMergingPlugin(),
new ExtraWatchWebpackPlugin({
files: [
path.join(moduleRoot, 'generated/**'),
path.join(moduleRoot, 'src/types/**/*.d.ts'),
],
dirs: [],
}),
new webpack.SourceMapDevToolPlugin({
filename: '../sourcemaps/[filebase].map[query]',
publicPath: '/sourcemaps/',
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore: BUG: This parameter is missing in @types/webpack declarations
fileContext: 'web/content/sourcemaps',
noSources: production,
}),
analyze &&
new BundleAnalyzerPlugin({
analyzerMode: 'server',
analyzerHost: '0.0.0.0',
analyzerPort,
openAnalyzer: false,
defaultSizes: 'gzip',
}),
analyze &&
new SizePlugin({
writeFile: true,
publish: false,
}),
].filter(Boolean),
optimization: {
moduleIds: analyze || development || debuggableProd ? 'named' : 'hashed',
chunkIds: analyze || development || debuggableProd ? 'named' : 'total-size',
flagIncludedChunks: true,
concatenateModules: true,
providedExports: true,
usedExports: true,
noEmitOnErrors: true,
minimize: production,
runtimeChunk: false,
splitChunks: production && {
automaticNameDelimiter: '.',
chunks: 'all',
minSize: 30000,
maxSize: 0,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
name: true,
cacheGroups: {
vendors: false,
runtimeChunk: false,
default: {
minChunks: 2,
priority: -100,
reuseExistingChunk: true,
},
},
},
minimizer: [
production && webpackTerser({ sourceMaps, node: false, profile }),
production &&
new OptimizeCssAssetsPlugin({
cssProcessorPluginOptions: {
preset: [
'default',
{
normalizeWhitespace: !development,
discardComments: { removeAll: true },
},
],
},
}),
].filter(Boolean),
},
}