-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
177 lines (173 loc) · 4.53 KB
/
webpack.config.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
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin"); // react 热更新
const HtmlWebpackPlugin = require("html-webpack-plugin");
const AssetsPlugin = require("./config/plugins/assetsPlugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const ESLintPlugin = require('eslint-webpack-plugin');
const path = require("path");
module.exports = {
mode: "development",
entry: {
main: "./src/index.js",
},
output: {
path: path.resolve("./dist"),
filename: "js/[name].[contenthash].bundle.js",
clean: true,
},
// watch: true,
devServer: {
host: "localhost",
port: "8000",
hot: true,
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
extensions: [".js", ".jsx", ".css", ".json"],
},
resolveLoader: {
modules: ["node_modules", "config/loaders"],
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: ["babel-loader"],
exclude: /(node_modules)/,
},
{
test: /\.css$/, // 正则表达式 用于匹配需要处理的文件
use: [
// loader的执行顺序 从右往左 从下往上
MiniCssExtractPlugin.loader,
{
loader: "css-loader", // 可以处理css中的 url @media @import
options: {
importLoaders: 1, // 处理@import
esModule: false, // background 中的 url 默认会被处理成require
},
},
{
loader: "postcss-loader", // 为了对css做兼容 具体功能需要配置
options: {
postcssOptions: {
plugins: [
// require("autoprefixer"),
"postcss-preset-env", // 当前预设包含autoprefixer功能
], // 添加样式前缀
},
},
},
],
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
importLoaders: 1,
esModule: false,
},
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
// require("autoprefixer"),
"postcss-preset-env",
],
},
},
},
"less-loader",
],
},
{
test: /\.(png|svg|gif|jpe?g)$/,
type: "asset/resource",
generator: {
filename: "static/[hash][ext][query]",
},
},
],
},
plugins: [
new AssetsPlugin(),
new ReactRefreshWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./public/index.html",
}),
new CopyWebpackPlugin({
patterns: [
{
from: "public",
to: path.resolve("./dist"),
globOptions: {
ignore: ['**/index.html']
// ignore: path.resolve("./public/index.html"),
},
},
],
}),
new MiniCssExtractPlugin({
filename: "css/[name].[contenthash:8].css",
}),
new ESLintPlugin({
context: path.resolve(__dirname, 'src'), // 指定要检查的文件夹
exclude: 'node_modules', // 排除检查的文件夹
// 其他配置
}),
],
optimization: {
chunkIds: "deterministic",
// runtimeChunk: true,
// 没有使用到的将会被标记
usedExports: true,
sideEffects: true,
minimize: true,
minimizer: [
// css压缩
new CssMinimizerPlugin(),
// js压缩
new TerserPlugin({
terserOptions: {
keep_classnames: true,
keep_fnames: true,
mangle: false,
format: {
comments: false,
},
compress: {
drop_console: true, //移除所有console相关代码;
},
},
extractComments: false,
}),
],
splitChunks: {
chunks: "all",
minSize: 60000,
minChunks: 1,
cacheGroups: {
sysVendor: {
test: /[\\/]node_modules[\\/]/,
filename: "js/vendor_[contenthash].js",
priority: 10,
},
react: {
test: /(react|react-dom)[\\/]/,
filename: "js/react_[contenthash].js",
maxSize: Infinity,
priority: 20,
},
},
},
},
};