-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
445 lines (405 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
/* Webpack configuration */
import dotenv from 'dotenv'
import { resolve, basename } from 'path'
import { readFileSync } from 'fs'
import { sync } from 'glob'
import webpack from 'webpack'
import DotenvWebpack from 'dotenv-webpack'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import StylelintWebpackPlugin from 'stylelint-webpack-plugin'
import TerserWebpackPlugin from 'terser-webpack-plugin'
import OptimizeCssAssetsWebpackPlugin from 'optimize-css-assets-webpack-plugin'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import ImageminWebpackPlugin from 'imagemin-webpack-plugin'
import FaviconsWebpackPlugin from 'favicons-webpack-plugin'
import PurgecssWebpackPlugin from 'purgecss-webpack-plugin'
import CompressionWebpackPlugin from 'compression-webpack-plugin'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import SitemapWebpackPlugin from 'sitemap-webpack-plugin'
import { CleanWebpackPlugin } from 'clean-webpack-plugin'
// Configuration
import pkg from './package'
import _ from './app.config'
dotenv.config()
// Helpers
const resolvePath = (...args) => resolve(__dirname, ...args)
const allHtml = sync(resolvePath('src', '[^_]*.hbs'))
const siteMap = JSON.parse(readFileSync(resolvePath('src', 'sitemap.json'), 'utf8'))
const webpackConfig = (env = {}, argv) => {
// Set `development` as Webpack default mode
argv.mode = argv.mode || 'development'
// Set `production` mode using `--mode=production`
const PRODUCTION = argv.mode === 'production'
// Enable debug mode using `--debug`
const DEBUG = argv.debug || false
// Enable gzip compression using `--gzip`
const GZIP = argv.gzip || false
// Environment specific file suffix
const SUFFIX = PRODUCTION ? `${pkg.version}.min` : 'dev'
// Local development server configuration
const devServer = {
// clientLogLevel: DEBUG ? 'debug' : 'silent',
stats: DEBUG ? 'verbose' : 'minimal',
// turn off all error logging
// required by `friendly-errors-webpack-plugin`
// quiet: true,
contentBase: [resolvePath('dist')],
watchContentBase: true,
writeToDisk: true,
host: 'localhost',
port: process.env.APP_PORT,
// public: process.env.APP_URL,
// publicPath: '/',
allowedHosts: [],
// proxy: {
// '/api': {
// target: 'http://localhost:8081',
// changeOrigin: true
// }
// },
inline: true,
hot: true,
disableHostCheck: true,
historyApiFallback: {
index: '/404.html'
},
// openPage: ['index.html'],
open: true
}
if (process.env.APP_URL !== '') {
devServer.public = process.env.APP_URL
} else {
process.env.APP_URL = `http://localhost:${process.env.APP_PORT}`
}
if (process.env.APP_API_URL !== '') {
devServer.allowedHosts.push(process.env.APP_API_URL)
} else {
process.env.APP_API_URL = `http://localhost:${process.env.APP_API_PORT}`
}
return ({
devServer,
context: resolvePath('src'),
entry: _.config.entry,
output: {
path: resolvePath('dist'),
publicPath: '/',
// chunkFilename: 'js/[name].[id].js',
filename: `js/[name].${SUFFIX}.js`
},
// Style of source mapping to enhance the debugging process
devtool: PRODUCTION ? 'source-map' : 'cheap-module-eval-source-map',
// How webpack notifies you of assets and entrypoints
// that exceed a specific file limit
performance: {
hints: false
},
// Make watching work properly
watchOptions: {
poll: true
},
// Options for resolving module requests
resolve: {
extensions: ['.js', '.css', '.scss', '.hbs'],
modules: ['node_modules']
// alias: {}
},
// Options affecting the normal modules
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
// exclude: /node_modules/,
include: resolvePath('src', 'js'),
loader: 'eslint-loader',
options: {
// eslintPath: '', // `.eslintrc` is used by default
emitError: true,
emitWarning: !PRODUCTION,
failOnError: PRODUCTION
}
},
{
test: /\.js$/,
// exclude: /node_modules/,
include: resolvePath('src', 'js'),
loader: 'babel-loader',
options: {
// cacheDirectory: true,
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'usage',
corejs: 3
}
]
]
}
},
{
test: /\.s[ac]ss$/i,
// exclude: /node_modules/,
include: resolvePath('src', 'scss'),
use: [
PRODUCTION || DEBUG ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 2
}
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
ident: 'postcss',
plugins: (loader) => [
require('postcss-preset-env')()
]
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
},
{
test: /\.(png|jpe?g|gif|svg)$/,
include: [
resolvePath('src', 'img')
],
loader: 'file-loader',
options: {
// Required for handlebars-loader to replace
// inline require statements. For images
esModule: false,
name: '[name].[ext]',
outputPath: 'img/'
}
},
{
test: /\.(woff|woff2|ttf|eot|svg)$/,
include: [
/@fortawesome/,
resolvePath('src', 'font')
],
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'font/'
}
},
{
test: /\.hbs$/,
include: resolvePath('src'),
loader: 'handlebars-loader',
options: {
helperDirs: [resolvePath('src', '_helpers')],
partialDirs: [resolvePath('src', '_partials')],
inlineRequires: '/img/'
}
}
]
},
// Add plugins to the compiler
plugins: [
// Remove output folder(s) before building
new CleanWebpackPlugin({
verbose: DEBUG
}),
// Supports dotenv and other environment variables
// and only exposes what you choose and use
new DotenvWebpack({
safe: true,
systemvars: true,
silent: DEBUG
}),
// Create global constants which can be configured at compile time
new webpack.DefinePlugin({
'process.env': {
PRODUCTION: JSON.stringify(PRODUCTION),
DEBUG: JSON.stringify(DEBUG)
}
}),
// Lint all CSS files with `stylelint`
new StylelintWebpackPlugin({
context: resolvePath('src', 'scss'),
files: ['**/*.scss'],
emitError: true,
failOnError: PRODUCTION
}),
// Move CSS into a separate output file
new MiniCssExtractPlugin({
filename: `css/[name].${SUFFIX}.css`
}),
// Copies individual files or entire directories
new CopyWebpackPlugin({
patterns: [{
context: resolvePath('public'),
from: '*.@(txt|xml|svg|jpg|jpeg|gif)',
to: resolvePath('dist')
}]
}),
// Generate HTML file(s)
...generateViews(allHtml, {
inject: false,
minify: PRODUCTION ? {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyCSS: true,
minifyJS: true
} : false,
// Pass config and env variables
_: {
..._,
env: {
PRODUCTION,
DEBUG,
...process.env
}
}
}, PRODUCTION),
// Optimize all images for production env only
new ImageminWebpackPlugin({
disable: !PRODUCTION,
test: /\.(png|jpe?g|gif|svg)$/i,
pngquant: {
quality: '65-90',
speed: 4
},
optipng: {
interlaced: false,
optimizationLevel: 5
},
jpegtran: {
progressive: true
},
gifsicle: {
interlaced: true,
optimizationLevel: 2
}
}),
// Generate all favicons from the `public/favicon.svg`
new FaviconsWebpackPlugin({
logo: resolvePath('public', 'favicon.svg'),
prefix: '/',
// devMode: 'webapp',
// inject: true,
favicons: {
lang: _.app.lang,
..._.config.favicons
}
}),
// Generates sitemap.xml
PRODUCTION
? new SitemapWebpackPlugin(process.env.APP_URL, siteMap, {
lastmod: true,
changefreq: 'monthly',
priority: '0.4',
skipgzip: !GZIP
})
: 0,
// Add dynamic banner to output bundle(s)
PRODUCTION
? new webpack.BannerPlugin([
new Date().toISOString().substr(0, 10),
pkg.name,
`@version ${pkg.version}`,
`@license ${pkg.license}`,
`@author ${pkg.author}`,
'Copyright (c) 2020'
].join('\n'))
: 0,
// Remove unused CSS with PurgeCSS
PRODUCTION
? new PurgecssWebpackPlugin({
paths: sync(`${resolvePath('src')}/**/*`, {
nodir: true
})
})
: 0,
// Prepare compressed versions of assets to serve them with Content-Encoding
GZIP
? new CompressionWebpackPlugin({
algorithm: 'gzip',
test: /\.(js|css)$/,
filename: '[path].gz[query]',
threshold: 10240,
minRatio: 0.8,
deleteOriginalAssets: false
})
: 0
].filter(Boolean),
// Optimizations depending on the chosen mode
optimization: {
minimize: PRODUCTION,
minimizer: [
// JavaScript parser, mangler and compressor toolkit for ES6+
new TerserWebpackPlugin({
sourceMap: true,
extractComments: false,
terserOptions: {
sourceMap: true,
warnings: true,
ecma: 5,
mangle: false
}
}),
// Plugin to optimize\minimize CSS assets using cssnano
new OptimizeCssAssetsWebpackPlugin({
sourceMap: true,
cssProcessorOptions: {
map: {
inline: false
},
autoprefixer: false
}
})
],
// Create a `vendors` chunk, which includes all code
// shared between entrypoints from `node_modules`
splitChunks: {
cacheGroups: {
default: false,
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
minChunks: 2
}
}
}
}
})
}
/**
* generateViews
*
* @param items {Array} List of paths to the `.hbs` files
* @param options {Object} html-webpack-plugin options
* @returns {Array} New instances of HtmlWebpackPlugin
*/
function generateViews (items, options) {
const views = []
items.map(item => {
const fileName = basename(item, '.hbs')
views.push(
new HtmlWebpackPlugin({
template: resolvePath('src', `${fileName}.hbs`),
filename: `${fileName}.html`,
...options
})
)
})
return views
}
module.exports = webpackConfig