-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.prod.js
229 lines (227 loc) · 7.71 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
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
const path = require("path");
const postcssFlexbugsFixes = require("postcss-flexbugs-fixes");
const postcssPresetEnv = require("postcss-preset-env");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// Line 223
// const ManifestPlugin = require("webpack-manifest-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const safePostCssParser = require("postcss-safe-parser");
const CleanWebpackPlugin = require("clean-webpack-plugin");
module.exports = {
mode: "production",
// Stop compilation early in production
bail: true,
entry: "./src/index.js",
output: {
// The build folder.
path: path.resolve(__dirname, "build"),
// We use "/" in development for homepage.
publicPath: "./",
// Add /* filename */ comments to generated require()s in the output.
pathinfo: false,
// There will be one main bundle, and one file per asynchronous chunk.
// In development, it does not produce real files.
filename: "static/js/[name].[chunkhash:8].js",
// There are also additional JS chunk files if you use code splitting.
chunkFilename: "static/js/[name].[chunkhash:8].chunk.js"
},
// devtool
devtool: "eval-source-map",
optimization: {
minimize: true,
minimizer: [
// This is only used in production mode
new TerserPlugin({
terserOptions: {
parse: {
// we want terser to parse ecma 8 code. However, we don't want it
// to apply any minfication steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234
ecma: 8
},
compress: {
ecma: 5,
warnings: false,
// Disabled because of an issue with Uglify breaking seemingly valid code:
// https://github.com/facebook/create-react-app/issues/2376
// Pending further investigation:
// https://github.com/mishoo/UglifyJS2/issues/2011
comparisons: false,
// Disabled because of an issue with Terser breaking valid code:
// https://github.com/facebook/create-react-app/issues/5250
// Pending futher investigation:
// https://github.com/terser-js/terser/issues/120
inline: 2
},
mangle: {
safari10: true
},
output: {
ecma: 5,
comments: false,
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488
ascii_only: true
}
},
// Use multi-process parallel running to improve the build speed
// Default number of concurrent runs: os.cpus().length - 1
parallel: true,
// Enable file caching
cache: true,
// sourceMap
sourceMap: false
}),
// This is only used in production mode
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
parser: safePostCssParser,
map: false
}
})
],
// Automatically split vendor and commons
// https://twitter.com/wSokra/status/969633336732905474
// https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
splitChunks: {
chunks: "all",
//????????????????? Default is false and does not work ???????????? //
name: true
},
// Keep the runtime chunk separated to enable long term caching
// https://twitter.com/wSokra/status/969679223278505985
runtimeChunk: true
},
module: {
// Makes missing exports an error instead of warning.
// https://webpack.js.org/configuration/module/
strictExportPresence: true,
rules: [
// Disable require.ensure as it's not a standard language feature.
{ parser: { requireEnsure: false } },
// First, run the linter.
{
enforce: "pre",
test: /\.(js|mjs|jsx)$/,
exclude: /node_modules/,
use: ["eslint-loader"]
},
// Process application JS with Babel.
{
test: /\.(js|mjs|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader"]
},
// file-loader
{
test: /\.svg$/,
use: [
"@svgr/webpack",
{
loader: "file-loader",
options: {
name: "[hash].[ext]",
outputPath: "./static/images"
// outputPath: '../build/static/images'
}
}
]
},
// url-loader
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
use: [
{
loader: "url-loader",
options: {
limit: 3000,
name: "static/media/[name].[hash:8].[ext]"
}
}
]
},
// css-loader
{
test: /\.css$/,
exclude: /\.module\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: "./"
}
},
{ loader: "css-loader", options: { importLoaders: 1 } },
{
loader: "postcss-loader",
options: {
ident: "postcss",
plugins: () => [
postcssFlexbugsFixes,
postcssPresetEnv({
autoprefixer: {
flexbox: "no-2009"
},
stage: 3
})
]
}
}
],
// Don't consider CSS imports dead code even if the
// containing package claims to have no side effects.
// Remove this when webpack adds a warning or an error for this.
// See https://github.com/webpack/webpack/issues/6571
sideEffects: true
},
// sass-loader
{
test: /\.(scss|sass)$/,
exclude: /\.module\.(scss|sass)$/,
use: [
"style-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS, using Node Sass by default
],
// Don't consider CSS imports dead code even if the
// containing package claims to have no side effects.
// Remove this when webpack adds a warning or an error for this.
// See https://github.com/webpack/webpack/issues/6571
sideEffects: true
}
]
},
plugins: [
new CleanWebpackPlugin(["build"]),
new HtmlWebpackPlugin({
inject: true,
template: "./src/index.html",
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "static/css/[name].[contenthash:8].css",
chunkFilename: "static/css/[name].[contenthash:8].chunk.css"
})
// new ManifestPlugin({
// fileName: "asset-manifest.json",
// publicPath: "/"
// })
]
};