-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
302 lines (287 loc) · 9.17 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
const path = require('path')
const glob = require('glob')
const { omitBy, flatten, uniq, map, keys } = require('lodash')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const PreloadWebpackPlugin = require('preload-webpack-plugin')
const CssUrlRelativePlugin = require('css-url-relative-plugin')
const ManifestPlugin = require('webpack-manifest-plugin')
const safePostCssParser = require('postcss-safe-parser')
const TerserPlugin = require('terser-webpack-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const PurgecssPlugin = require('purgecss-webpack-plugin')
const HtmlWebpackExcludeAssetsPlugin = require('html-webpack-exclude-assets-plugin')
/* eslint-disable complexity */
const configWebpack = (_, argv) => {
if (argv && argv.mode === 'production') {
process.env.WEBPACK_MODE = 'production'
}
const {
isProduction,
isDevelopment,
// feature flags
shouldUseSourceMap,
shouldMinify,
shouldPreload,
shouldPreloadFonts,
shouldHashName,
shouldGenerateManifest,
shouldOpenWebpage,
shouldPurgeCss,
// paths
rootDir,
srcDir,
buildDir,
entry,
excludedAssets,
copies
} = require('./config')
const getStyleLoaders = require('./config/getStyleLoaders')
const config = {
context: rootDir,
mode: isDevelopment ? 'development' : 'production',
devtool: shouldUseSourceMap ? (isDevelopment ? 'inline-source-map' : 'source-map') : false,
entry,
output: {
filename: shouldHashName ? 'scripts/[name].[contenthash:8].js' : 'scripts/[name].js',
path: buildDir
},
module: {
rules: [
// allow parsing image src
{
test: /\.html$/,
use: [
{
loader: require.resolve('html-loader'),
options: {
minimize: false
}
}
]
},
{
test: /\.pug$/,
use: [
{
loader: require.resolve('pug-loader'),
options: {
minimize: false
}
}
]
},
{
test: /\.m?js$/,
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
configFile: false,
compact: false,
cacheDirectory: true,
// See #6846 for context on why cacheCompression is disabled
cacheCompression: false,
// Babel sourcemaps are needed for debugging into node_modules
// code. Without the options below, debuggers like VSCode
// show incorrect code and set breakpoints on the wrong lines.
sourceMaps: shouldUseSourceMap,
inputSourceMap: shouldUseSourceMap
}
},
{
test: /\.(sc|sa|c)ss$/,
use: getStyleLoaders(
{
importLoaders: 3
},
'sass-loader'
)
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
exclude: /fonts/,
use: [
{
loader: require.resolve('url-loader'),
options: {
esModule: false, // allow parsing image src
limit: isProduction ? 0 : 8192, // export files on production
name: '[name].[ext]',
fallback: 'file-loader',
outputPath: 'public/images'
}
}
// {
// loader: require.resolve('image-webpack-loader'),
// options: {
// mozjpeg: {
// progressive: true,
// quality: 65
// },
// pngquant: {
// quality: [0.65, 0.95],
// speed: 4
// },
// gifsicle: {
// interlaced: false
// },
// webp: {
// quality: 75
// }
// }
// }
].filter(Boolean)
},
{
test: /fonts|\.(woff|woff2|eot|ttf)$/i,
use: [
// {
// loader: require.resolve('url-loader'),
// options: {
// limit: 8192,
// name: '[name].[ext]',
// fallback: 'file-loader',
// outputPath: 'public/fonts'
// }
// }
{
loader: require.resolve('file-loader'),
options: {
name: '[name].[ext]',
outputPath: 'public/fonts'
}
}
]
}
]
},
plugins: [
new CleanWebpackPlugin(),
// new webpack.ProvidePlugin({
// $: 'jquery',
// jQuery: 'jquery',
// 'windows.jQuery': 'jquery'
// }),
new CopyWebpackPlugin(copies),
isProduction && new MiniCssExtractPlugin({
filename: shouldHashName ? 'styles/[name].[contenthash:8].css' : 'styles/[name].css'
// For chunks
// chunkFilename: shouldHashName ? 'styles/[name].[contenthash:8].chunk.css' : 'styles/[name].chunk.css'
}),
shouldPurgeCss && new PurgecssPlugin({
paths: glob.sync(`${srcDir}/**/*`, { nodir: true }),
whitelistPatterns: [] // static selectors for elements generated by 3rd party JS
}),
// Generate an asset manifest file with the following content:
// - "files" key: Mapping of all asset filenames to their corresponding
// output file so that tools can pick it up without having to parse
// `index.html`
// - "entrypoints" key: Array of files which are included in `index.html`,
// can be used to reconstruct the HTML if necessary
shouldGenerateManifest && new ManifestPlugin({
fileName: 'asset-manifest.json',
publicPath: '/',
generate: (seed, files, entrypoints) => {
const manifestFiles = files.reduce((manifest, file) => {
manifest[file.name] = file.path
return manifest
}, seed)
const entrypointFiles = flatten(uniq(map(entrypoints, (entryFiles, entryName) => {
if (entryName.includes('.min')) return false // minified
return entryFiles.filter(
fileName => !fileName.endsWith('.map')
)
}).filter(Boolean)))
return {
files: manifestFiles,
entrypoints: entrypointFiles
}
}
}),
// Ignore moment.js locales for lighter builds
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new webpack.HashedModuleIdsPlugin(),
shouldPreload && new PreloadWebpackPlugin({
include: 'initial'
}),
// preload fonts only
shouldPreloadFonts && new PreloadWebpackPlugin({
include: 'allAssets',
as: 'font',
fileWhitelist: [
/\.woff$/i
]
}),
new CssUrlRelativePlugin()
].filter(Boolean),
devServer: {
contentBase: srcDir,
writeToDisk: false,
open: shouldOpenWebpage
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules/,
chunks: 'initial',
name: 'vendor',
priority: 10,
enforce: true
}
}
},
minimizer: []
}
}
if (shouldMinify) {
config.optimization.minimizer.push(new TerserPlugin({
include: /\.min\.js$/,
parallel: true // Need specific number of CPUs for CI or other systems
}))
config.optimization.minimizer.push(
new OptimizeCSSAssetsPlugin({
assetNameRegExp: /\.min\.css$/,
cssProcessorOptions: {
parser: safePostCssParser,
map: shouldUseSourceMap
? {
// `inline: false` forces the sourcemap to be output into a
// separate file
inline: false,
// `annotation: true` appends the sourceMappingURL to the end of
// the css file, helping the browser find the sourcemap
annotation: true
}
: false
},
cssProcessorPluginOptions: {
preset: ['default', { minifyFontValues: { removeQuotes: false } }]
}
})
)
}
// [!_] is for layout ignore
const files = glob.sync(path.join(srcDir, 'pages', '[!_]*.{html,pug}'))
const injectedChunks = shouldMinify ? omitBy(entry, (_, f) => !f.includes('.min')) : entry
files.forEach(file => {
config.plugins.push(
new HtmlWebpackPlugin({
filename: `${path.basename(file, path.extname(file))}.html`,
template: file,
favicon: path.resolve(path.join(srcDir, '/assets/images/favicon.ico')),
minify: false,
chunks: keys(injectedChunks),
excludeAssets: excludedAssets
})
)
})
config.plugins.push(new HtmlWebpackExcludeAssetsPlugin())
return config
}
module.exports = configWebpack