-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.cjs
224 lines (209 loc) · 6.58 KB
/
webpack.config.cjs
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
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const CssMinimizerWebpackPlugin = require("css-minimizer-webpack-plugin");
const TerserWebpackPlugin = require("terser-webpack-plugin");
const CaseSensitivePathsWebpackPlugin = require("case-sensitive-paths-webpack-plugin");
const EslintWebpackPlugin = require("eslint-webpack-plugin");
const StyleLintWebpackPlugin = require("stylelint-webpack-plugin");
const path = require("path");
module.exports = (env, argv) => {
const isEnvProduction = process.env.NODE_ENV == "production";
const isEnvDevelopment = process.env.NODE_ENV == "development";
const getCommonStyleLoader = cssOptions => {
const loaders = [
isEnvDevelopment && require.resolve("style-loader"),
isEnvProduction && MiniCssExtractPlugin.loader,
{
loader: require.resolve("css-loader"),
options: cssOptions
},
{ loader: require.resolve("postcss-loader") }
].filter(Boolean);
return loaders;
};
/**
* @type {import("webpack/types").Configuration}
*/
const config = {
mode: isEnvProduction ? "production" : "development",
entry: "./src/index.tsx",
output: {
path: isEnvProduction ? path.resolve(__dirname, "dist") : undefined,
filename: isEnvProduction ? "scripts/[name].[contenthash:10].js" : "scripts/[name].js",
chunkFilename: isEnvProduction
? "scripts/[name].[contenthash:10].chunk.js"
: "scripts/[name].chunk.js",
assetModuleFilename: "assets/images/[hash:10][ext][query]",
clean: true
},
module: {
rules: [
{
oneOf: [
{
test: /\.css$/i,
exclude: /\.module\.css$/i,
use: getCommonStyleLoader({
importLoaders: 1,
modules: {
mode: "icss"
}
})
},
{
test: /\.module\.css$/i,
use: getCommonStyleLoader({
importLoaders: 1,
modules: {
mode: "local"
}
})
},
{
test: /\.sa|css$/i,
exclude: /\.module\.s[a|c]ss$/i,
use: [
...getCommonStyleLoader({
importLoaders: 3,
modules: {
mode: "icss"
}
}),
require.resolve("sass-loader")
]
},
{
test: /\.module\.s[a|c]ss$/i,
use: [
...getCommonStyleLoader({
importLoaders: 3,
modules: {
mode: "local"
}
}),
require.resolve("sass-loader")
]
},
{
test: /\.(png|jpe?g|gif|svg)$/,
type: "asset",
parser: {
dataUrlCondition: {
maxSize: 10 * 1024
}
}
},
{
test: /\.(ttf|woff2?)$/,
type: "asset/resource",
generator: {
filename: "assets/fonts/[hash:10][ext][query]"
}
},
{
test: /\.(tsx?|jsx?)$/i,
include: path.resolve(__dirname, "src"),
use: require.resolve("babel-loader")
}
]
}
]
},
plugins: [
new EslintWebpackPlugin({
configType: "flat",
eslintPath: "eslint/use-at-your-own-risk",
extensions: [".ts", ".tsx", ".js", ".jsx"],
context: path.resolve(__dirname, "./"),
exclude: "node_modules",
cache: true,
cacheLocation: path.resolve(__dirname, "node_modules/.cache/.eslintcache")
}),
new StyleLintWebpackPlugin({
extensions: [".css"],
context: path.resolve(__dirname, "./"),
exclude: "node_modules",
cache: true,
cacheLocation: path.resolve(__dirname, "node_modules/.cache/.stylelintcache")
}),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, "public/index.html")
}),
isEnvProduction &&
new MiniCssExtractPlugin({
filename: "static/css/[name].[contenthash:10].css",
chunkFilename: "static/css/[name].[contenthash:10].chunk.css"
}),
isEnvDevelopment && new CaseSensitivePathsWebpackPlugin(),
isEnvDevelopment && new ReactRefreshWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, "public"),
to: path.resolve(__dirname, "dist"),
toType: "dir",
noErrorOnMissing: true, // 不生成错误
globOptions: {
// 忽略文件
ignore: ["**/index.html"]
},
info: {
// 跳过terser压缩js
minimized: true
}
}
]
})
].filter(Boolean),
optimization: {
minimize: isEnvProduction,
minimizer: [new CssMinimizerWebpackPlugin(), new TerserWebpackPlugin()],
splitChunks: {
chunks: "all",
cacheGroups: {
layouts: {
name: "layouts",
test: path.resolve(__dirname, "src/layouts"),
priority: 40
},
// 将react相关的库单独打包,减少node_modules的chunk体积。
react: {
name: "react",
test: /[\\/]node_modules[\\/]react(.*)?[\\/]/,
chunks: "initial",
priority: 20
},
libs: {
name: "chunk-libs",
test: /[\\/]node_modules[\\/]/,
priority: 10, // 权重最低,优先考虑前面内容
chunks: "initial"
}
}
},
runtimeChunk: {
name: entrypoint => `runtime~${entrypoint.name}`
}
},
resolve: {
extensions: [".tsx", ".jsx", ".ts", ".js", ".json"],
alias: {
"@": path.resolve(__dirname, "src"),
"@components": path.resolve(__dirname, "src/components")
}
},
devServer: {
open: true,
host: "localhost",
port: 3000,
hot: true,
compress: true,
historyApiFallback: true
},
devtool: isEnvProduction ? "source-map" : "cheap-module-source-map",
performance: false // 关闭性能分析,提升速度
};
return config;
};