-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
248 lines (215 loc) · 7.22 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
/* eslint-disable import/no-extraneous-dependencies */
// @ts-check
const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const TerserPlugin = require('terser-webpack-plugin')
//
// Possible problem:
// - If "entry" contains SCSS-files, an empty JS file is created besides the CSS output
// - https://github.com/webpack-contrib/mini-css-extract-plugin/issues/151
// - https://github.com/webpack/webpack/issues/11671
// - https://github.com/webpack/webpack/issues/7300
//
// Current solution (only if needed):
// - Run "npm i -D webpack-remove-empty-scripts"
// - Include the following, here:
// const RemovePlugin = require('webpack-remove-empty-scripts')
//
//
// Possible problem:
// - We can't use globs (e.g. "./public/js/app/*.{js,jsx}") in "entry"
//
// Current solution (only if needed):
// - Ensure unique basenames (or maybe use subDir)
// - Add "...composeEntriesFromGlob(pattern)" as entry-point
// - Run "npm i -D fast-glob"
// - Include the following, here:
// const FastGlob = require('fast-glob')
//
// const BabelConfig = require('./.babelrc.json')
const { babel: BabelConfig } = require('./package.json')
process.env.NODE_ENV = 'development'
const { uri: PROXY_PREFIX_URI } = require('./config/commonSettings').proxyPrefixPath
// const PROXY_PREFIX_URI = '/node'
const ENV_IS_DEV = ['dev', 'development'].includes(process.env.WEBPACK_ENV)
const WATCH_IS_ON = ['watch'].includes(process.env.WEBPACK_MODE)
// eslint-disable-next-line no-unused-vars
function composeEntriesFromGlob(pattern) {
// @ts-ignore
// eslint-disable-next-line no-undef
const matches = FastGlob.sync(pattern, { onlyFiles: true, cwd: __dirname, objectMode: true })
const result = {}
matches.forEach(item => {
result[item.name.split('.')[0]] = item.path
})
return result
}
function composePublicPathString(...subDirParts) {
const allParts = [
ENV_IS_DEV ? `http://localhost:3000` : '',
PROXY_PREFIX_URI,
...subDirParts.filter(text => typeof text === 'string' && text !== ''),
]
return allParts.map(text => text.replace(/^\/|\/$/g, '')).join('/') + '/'
}
function getTransformationRules({ contextIsNode, subDir = null }) {
const MAGIC_EXTENSIONS_IF_OMITTED_WITH_IMPORT = ['.js', '.jsx', '.json']
const ALLOW_MIX_OF_CJS_AND_ESM_IMPORTS = { sourceType: 'unambiguous' }
// @ts-ignore
// eslint-disable-next-line no-undef
const AVOID_EMPTY_JS_FILES_ON_SCSS_ENTRYPOINTS = typeof RemovePlugin === 'undefined' ? [] : [new RemovePlugin()]
return {
plugins: [...AVOID_EMPTY_JS_FILES_ON_SCSS_ENTRYPOINTS, new MiniCssExtractPlugin()],
resolve: {
extensions: MAGIC_EXTENSIONS_IF_OMITTED_WITH_IMPORT,
},
target: contextIsNode ? 'node' : 'browserslist:> 0.25%, not dead',
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: { ...BabelConfig, ...ALLOW_MIX_OF_CJS_AND_ESM_IMPORTS },
},
},
{
test: /\.s[ac]ss$/i,
use: contextIsNode
? 'null-loader'
: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
esModule: false,
},
},
'sass-loader',
],
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
use: contextIsNode
? 'null-loader'
: [
{
loader: 'file-loader',
options: {
publicPath: composePublicPathString('static', subDir),
name: '[name].[hash:8].[ext]',
},
},
],
},
],
},
}
}
function getOutputOptions({ contextIsNode, subDir = null }) {
const OUTPUT_LICENSE_FILES = false
const BUNDLES_CAN_BE_IMPORTED_ON_SERVER_SIDE = contextIsNode ? { library: { type: 'commonjs2' } } : null
// There are many different types of source-maps in Webpack
// (see https://webpack.js.org/configuration/devtool)
// Tested settings:
// const DEV_SOURCE_MAP_TYPE = 'eval'
// - comes w/o *.map files and still small bundle (app.js has 2.6 MB)
// - code view with many Webpack annotations when debugging in browser
// - first build-dev (i.e. with empty cache) took 17 seconds
// const DEV_SOURCE_MAP_TYPE = 'eval-cheap-module-source-map'
// - comes w/o *.map files, has bigger bundles instead (app.js has 6.0 MB)
// - code view with many Webpack annotations when debugging in browser
// - first build-dev (i.e. with empty cache) took 13 seconds
const DEV_SOURCE_MAP_TYPE = 'source-map'
// - comes with *.map files (app.js has 2.5 MB)
// - full code view when debugging in browser
// - first build-dev (i.e. with empty cache) took 18 seconds
// const DEV_SOURCE_MAP_TYPE = 'inline-source-map'
// - comes w/o *.map files, has bigger bundles instead (app.js has 6.1 MB)
// - full code view when debugging in browser
// - first build-dev (i.e. with empty cache) took 22 seconds
return {
optimization: ENV_IS_DEV
? undefined
: {
minimizer: [new TerserPlugin({ extractComments: OUTPUT_LICENSE_FILES })],
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist', subDir || ''),
publicPath: composePublicPathString('static', subDir),
...BUNDLES_CAN_BE_IMPORTED_ON_SERVER_SIDE,
},
devtool: ENV_IS_DEV ? DEV_SOURCE_MAP_TYPE : undefined,
}
}
function getWatchOptions() {
return WATCH_IS_ON
? {
watch: true,
watchOptions: {
aggregateTimeout: 3000,
ignored: /\bnode_modules\b/,
},
}
: {
watch: false,
}
}
function getConsoleLogOptions({ contextIsNode }) {
return {
performance: {
hints: contextIsNode || ENV_IS_DEV ? false : 'warning',
},
stats: {
all: false,
colors: true,
errors: true,
errorDetails: 'auto',
warnings: true,
publicPath: !WATCH_IS_ON,
assets: !WATCH_IS_ON,
builtAt: true,
version: true,
timings: true,
},
}
}
function getCacheOptions({ contextIsNode }) {
const IGNORE_CACHE_ON_CHANGED_WEBPACK_CONFIG = { config: [__filename] }
return ENV_IS_DEV
? {
cache: {
name: contextIsNode ? 'node' : 'browser',
type: 'filesystem',
buildDependencies: { ...IGNORE_CACHE_ON_CHANGED_WEBPACK_CONFIG },
},
}
: null
}
module.exports = [
{
entry: {
'ssr-app': './public/js/app/ssr-app.js',
},
mode: ENV_IS_DEV ? 'development' : 'production',
...getTransformationRules({ contextIsNode: true }),
...getOutputOptions({ contextIsNode: true }),
...getWatchOptions(),
...getConsoleLogOptions({ contextIsNode: true }),
...getCacheOptions({ contextIsNode: true }),
},
{
entry: {
vendor: './public/js/vendor.js',
app: './public/js/app/app.jsx',
},
mode: ENV_IS_DEV ? 'development' : 'production',
...getTransformationRules({ contextIsNode: false }),
...getOutputOptions({ contextIsNode: false }),
...getWatchOptions(),
...getConsoleLogOptions({ contextIsNode: false }),
...getCacheOptions({ contextIsNode: false }),
},
]