-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.prod.js
140 lines (139 loc) · 3.59 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
133
134
135
136
137
138
139
140
const merge = require("webpack-merge");
const common = require("./webpack.common.js");
const TerserJSPlugin = require("terser-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require("path");
const CompressionPlugin = require("compression-webpack-plugin");
const glob = require("glob");
const PurgecssPlugin = require("purgecss-webpack-plugin");
const ImageminPlugin = require("imagemin-webpack-plugin").default;
const imageminMozjpeg = require("imagemin-mozjpeg");
module.exports = merge(common, {
mode: "production",
devtool: "cheap-module-eval-source-map",
node: {
fs: "empty"
},
output: {
// Contenthash substitution used for cache bursting
filename: "js/[name].[contenthash].bundle.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /\.(png|svg|jpe?g|gif|ico)$/i,
use: [
{
loader: "file-loader",
options: {
outputPath: "images/",
name: "[name].[contenthash].[ext]",
esModule: false
}
}
]
},
// Loads all audio files;
{
test: /\.(ogg|wma|mp3|wav|mpe?g)$/i,
use: {
loader: "file-loader",
options: {
outputPath: "audio/",
name: "[name].[contenthash].[ext]",
esModule: false
}
}
},
// Loads all font files
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
use: {
loader: "file-loader",
options: {
outputPath: "fonts/",
name: "[name].[contenthash].[ext]",
esModule: false
}
}
},
// Loads all JSON and text files; add more based on your needs
// {
// test: /\.(txt|JSON)$/i,
// use: {
// loader: "file-loader",
// options: {
// outputPath: "data/",
// name: "[name].[ext]",
// esModule: false
// }
// }
// },
// loads all html files
{
test: /\.(html)$/,
use: {
loader: "html-loader",
options: {
minimize: true,
root: path.resolve(__dirname, "dist")
}
}
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: "css/style.[contenthash].css",
chunkFilename: "css/style.[contenthash].css"
}),
new ImageminPlugin({
optipng: {
optimizationLevel: 6
},
plugins: [
imageminMozjpeg({
quality: 100,
progressive: true
})
]
}),
new CompressionPlugin({
test: /\.(html|css|js)(\?.*)?$/i
}),
new PurgecssPlugin({
paths: glob.sync("src/**/*", { nodir: true })
})
],
optimization: {
minimizer: [
// Minify JS; by default applies to all .js files;
new TerserJSPlugin({
cache: true,
parallel: true,
sourceMap: true
}),
// Minify CSS; default applies to all .css files
new OptimizeCSSAssetsPlugin({})
],
splitChunks: {
chunks: "all",
maxInitialRequests: Infinity,
minSize: 0,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
reuseExistingChunk: true,
name(module) {
const packageName = module.context.match(
/[\\/]node_modules[\\/](.*?)([\\/]|$)/
)[1];
return `vendor/npm.${packageName.replace("@", "")}`;
}
}
}
}
}
});