-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.prod.js
132 lines (131 loc) · 3.39 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
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
const path = require('path');
const common = require('./webpack.common.js');
const { merge } = require('webpack-merge');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = merge(common, {
mode: 'production',
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash:15].js',
assetModuleFilename: '[path][name].[contenthash:15][ext]',
},
module: {
rules: [
{
test: /\.css$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '',
},
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
},
},
{
loader: 'postcss-loader',
},
],
},
{
// revert to "type: asset/resource" with svg added
// if file-loader stops working
test: /\.(png|jpe?g|svg|gif)$/i,
use: {
loader: 'file-loader',
options: {
esModule: false,
name: '[path][name].[contenthash:15].[ext]',
},
},
type: 'javascript/auto',
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: '/template.html',
filename: 'index.html',
inject: 'body',
minify: true,
// links html to entry
chunks: ['app'],
}),
new MiniCssExtractPlugin({
filename: 'style-[name].[contenthash:15].css',
chunkFilename: 'style-[name].[contenthash:15].css',
}),
new CleanWebpackPlugin({
cleanAfterEveryBuildPatterns: ['dist/'],
dry: false,
}),
],
optimization: {
moduleIds: 'deterministic',
minimizer: [
`...`,
new CssMinimizerPlugin(),
new ImageMinimizerPlugin({
// Only apply to files equal to or over 8192 bytes
filter: (source) => {
if (source.byteLength >= 8192) {
return true;
}
return false;
},
minimizerOptions: {
plugins: [
['mozjpeg', { progressive: true, quality: 25 }],
['pngquant', { quality: [0.5, 0.7] }],
['svgo', {}],
],
},
}),
new ImageMinimizerPlugin({
// Only apply to files under 8192 bytes
filter: (source) => {
if (source.byteLength < 8192) {
return true;
}
return false;
},
minimizerOptions: {
plugins: [
['mozjpeg', { progressive: false, quality: 75 }],
['pngquant', { quality: [0.6, 0.8] }],
],
},
}),
],
// use for multiple entries on a page
// runtimeChunk: 'single',
splitChunks: {
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
enforce: true,
},
normalizeCSS: {
test: /normalize\.s?css$/,
name: 'normalize',
chunks: 'all',
enforce: true,
},
},
},
},
});