-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
193 lines (191 loc) · 5.57 KB
/
webpack.common.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
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const PurifyCssWebpack = require("purifycss-webpack");
const glob = require("glob");
const devMode = process.env.NODE_ENV !== "production";
module.exports = {
entry: {
app: ["@babel/polyfill", "./src/index.js"]
},
output: {
filename: "[name]-[hash].bundle.js",
chunkFilename: "[name]-[hash]-[id].js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /\.(le|c)ss$/,
include: [path.resolve(__dirname, "src")],
use: [
'css-hot-loader',
devMode ? "style-loader" : MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
modules: {
mode: 'local',
localIdentName: '[path][name]__[local]--[hash:base64:5]',
context: path.resolve(__dirname, 'src'),
},
importLoaders: 1,
localsConvention: 'asIs'
}
}, {
loader: 'px2rem-loader',
options: {
remUnit: 75,
remPrecision: 8
}
},
{
loader: "less-loader",
// options: {
// sourceMap: true
// }
},
{
loader: "postcss-loader"
}
]
},
{
test: /\.(png|svg|jpg|gif)$/,
exclude: path.resolve(__dirname, "./src/img/svg"),
use: {
loader: "url-loader",
options: {
limit: 1024 * 5,
outputPath: "img"
}
}
},
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
}
},
{
test: /\.svg$/,
loader: "svg-sprite-loader",
include: path.resolve(__dirname, "./src/img/svg"), // 只带自己人玩
options: {
symbolId: `icons_[name]`
}
}
]
},
node: {
fs: "empty"
},
optimization: {
minimize: devMode ? false : true,
splitChunks: {
chunks: "all",
minSize: 30000,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
automaticNameDelimiter: "~",
name: true,
cacheGroups: {
vendor: {
chunks: "all",
test: /[\\/]node_modules[\\/]/,
minChunks: 2,
priority: -10,
minSize: 0
},
commons: {
chunks: 'initial',
minChunks: 2,
maxInitialRequests: 5, // The default limit is too small to showcase the effect
minSize: 0, // This is example is too small to create commons chunks
name: 'common'
},
default: { // 默认缓存组名
minChunks: 2, // 最少引用两次才会被拆分
priority: -20, // 权重 -20
reuseExistingChunk: true // 如果主入口中引入了两个模块,其中一个正好也引用了后一个,就会直接复用,无需引用两次
}
}
},
runtimeChunk: {
name: "manifest"
},
minimizer: [
new TerserPlugin({
cache: true,
parallel: 4,
terserOptions: {
compress: {
// drop_console: true
}
}
})
]
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.less', '.json', '.css'],//自动解析确定的扩展,省去你引入组件时写后缀的麻烦
alias: { //配置一些短路径
src: path.resolve(__dirname, '../src'),
components: path.resolve(__dirname, '../src/components'),
utils: path.resolve(__dirname, '../src/utils'),
},
modules: ['node_modules'],//webpack 解析模块时应该搜索的目录
},
plugins: [
new MiniCssExtractPlugin({
filename: devMode ? "[name].css" : "[name].[hash].css",
chunkFilename: devMode ? "[name][id].css" : "[name][id].[hash].css"
}),
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, "./dist/index.html"),
template: path.resolve(__dirname, "./index-tmpl.html"),
title: "饿了么",
inject: true,
showErrors: true,
hash: true,
favicon: "./logo.png",
vendor: "./vendor.dll.js",
minify:
process.env.NODE_ENV === "development"
? false
: {
minifyCSS: true,
minifyJS: true,
removeComments: true, //移除HTML中的注释
collapseWhitespace: true, //折叠空白区域 也就是压缩代码
removeAttributeQuotes: true //去除属性引用
}
}),
new webpack.ProvidePlugin({
React: "react",
ReactDOM: "react-dom",
Component: ["react", "Component"]
}),
new webpack.DllReferencePlugin({
manifest: path.resolve(__dirname, ".", "dist", "manifest.json")
}),
new PurifyCssWebpack({
paths: glob.sync([ // 传入多文件路径
path.resolve(__dirname, './*.html'), // 处理根目录下的html文件
path.resolve(__dirname, './src/*.js') // 处理src目录下的js文件
])
}),
new CleanWebpackPlugin(["dist"], {
root: path.join(__dirname, "."),
exclude: ["manifest.json", "vendor.dll.js"],
verbose: true,
dry: false
}),
new CopyWebpackPlugin([{ from: "logo.png" }])
]
};