-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.prod.js
81 lines (75 loc) · 2.18 KB
/
webpack.prod.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
const path = require('path');
const merge = require('webpack-merge');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { webpackCommom, htmlFiles, jsFiles } = require('./webpack.common');
const HtmlFilesConfig = htmlFiles.map(fileName => {
let chunks = ['main'];
// if there is pages-about key in jsFiles object
// then add it to chunk otherwise only main chunk is added
// because if condition is not met
if (`pages-${fileName.slice(0, -5)}` in jsFiles) {
chunks = [`pages-${fileName.slice(0, -5)}`, ...chunks];
}
return new HtmlWebpackPlugin({
filename: `./${fileName}`,
template: `./${fileName}`,
chunks,
minify: {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
},
});
});
const prodOptions = {
mode: 'production',
output: {
filename: 'js/[name].[contentHash].js',
path: path.resolve(__dirname, 'dist'),
// Remove this when webpack updates to v5 because this behaiour will come by default
futureEmitAssets: true,
},
optimization: {
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
splitChunks: {
chunks: 'all',
},
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.(s[ac]|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../',
},
},
'css-loader',
'sass-loader',
],
},
],
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({ filename: 'css/[name].[contentHash].css' }),
...HtmlFilesConfig,
],
};
module.exports = merge(webpackCommom, prodOptions);