-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathrspack.config.js
More file actions
287 lines (281 loc) · 7.97 KB
/
rspack.config.js
File metadata and controls
287 lines (281 loc) · 7.97 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const path = require('path')
const { Configuration, DefinePlugin, BannerPlugin } = require('@rspack/core')
const HtmlRspackPlugin = require('html-rspack-plugin')
const { VueLoaderPlugin } = require('vue-loader')
const { publicPath, assetsDir, outputDir, title, devPort } = require('./src/config')
const dayjs = require('dayjs')
const time = dayjs().format('YYYY-M-D HH:mm:ss')
const fs = require('fs-extra')
const { webpackBanner } = require('./layouts')
// 设置环境变量
process.env.VUE_APP_TITLE = title || 'vue-admin-better'
process.env.VUE_APP_UPDATE_TIME = time
process.env.BASE_URL = publicPath
// 删除这一行,避免覆盖rspack.js中设置的值
// process.env.NODE_ENV = process.env.NODE_ENV || 'development'
process.env.VUE_APP_MOCK_ENABLE = 'true' // 启用mock
process.env.VUE_APP_AUTHOR = 'vue-admin-better' // 设置作者
const resolve = (dir) => path.join(__dirname, dir)
// 定义一个模式变量,避免冲突
const mode = process.env.NODE_ENV || 'development'
/**
* @type {Configuration}
*/
module.exports = {
mode: mode,
context: __dirname,
entry: {
app: './src/main.js',
},
output: {
path: resolve(outputDir),
publicPath: publicPath,
filename: 'js/[name].[contenthash:8].js',
chunkFilename: 'js/[name].[contenthash:8].js',
assetModuleFilename: `${assetsDir}/[name].[hash][ext][query]`,
},
// 增加性能提示配置
performance: {
// 提高阈值以减少警告
maxEntrypointSize: 3000000, // 3MB
maxAssetSize: 1000000, // 1MB
// 只在生产环境显示性能警告
hints: mode === 'production' ? 'warning' : false,
},
module: {
rules: [
{
test: /\.vue$/,
use: [
{
loader: 'vue-loader',
options: {
compilerOptions: {
preserveWhitespace: false,
},
},
},
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@vue/cli-plugin-babel/preset'],
},
},
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.scss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
auto: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
},
},
},
{
loader: 'sass-loader',
options: {
additionalData: (content, loaderContext) => {
const { resourcePath, rootContext } = loaderContext
const relativePath = path.relative(rootContext, resourcePath)
if (relativePath.replace(/\\/g, '/') !== 'src/styles/variables.scss') {
return `@import "~@/styles/variables.scss";${content}`
}
return content
},
},
},
],
},
{
test: /\.(png|jpe?g|gif|svg)$/,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 10 * 1024, // 10KB
},
},
},
{
test: /\.(woff2?|eot|ttf|otf)$/,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 30 * 1024, // 增加到30KB
},
},
},
{
resourceQuery: /raw/,
type: 'asset/source',
},
],
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'@': resolve('src'),
vue$: 'vue/dist/vue.esm.js',
path: 'path-browserify',
fs: false,
},
fallback: {
path: require.resolve('path-browserify'),
},
},
plugins: [
new VueLoaderPlugin(),
new DefinePlugin({
// 完全移除可能引起冲突的NODE_ENV定义,让rspack.js来处理
'process.env.BASE_URL': JSON.stringify(process.env.BASE_URL),
'process.env.VUE_APP_TITLE': JSON.stringify(process.env.VUE_APP_TITLE),
'process.env.VUE_APP_MOCK_ENABLE': JSON.stringify(process.env.VUE_APP_MOCK_ENABLE),
'process.env.VUE_APP_AUTHOR': JSON.stringify(process.env.VUE_APP_AUTHOR),
'process.env.VUE_APP_UPDATE_TIME': JSON.stringify(process.env.VUE_APP_UPDATE_TIME),
}),
new HtmlRspackPlugin({
template: './public/index.html',
filename: 'index.html',
title: title || 'vue-admin-better',
inject: 'body',
templateParameters: {
BASE_URL: publicPath,
VUE_APP_TITLE: process.env.VUE_APP_TITLE,
VUE_APP_AUTHOR: process.env.VUE_APP_AUTHOR,
},
minify:
mode === 'production'
? {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
collapseBooleanAttributes: true,
removeScriptTypeAttributes: true,
}
: false,
}),
// 添加版权信息到打包文件头部
new BannerPlugin({
banner: webpackBanner(time),
entryOnly: false,
include: /\.(js|css)$/,
}),
// 添加CopyPlugin功能,将public目录下除index.html外的文件复制到dist目录
{
apply(compiler) {
compiler.hooks.afterEmit.tap('CopyPublicFolderPlugin', (compilation) => {
// 确保目标目录存在
const targetPath = path.resolve(__dirname, 'dist');
fs.ensureDirSync(targetPath);
// 复制public目录下的所有文件(除了index.html)
fs.copySync(
path.resolve(__dirname, 'public'),
targetPath,
{
filter: (src) => {
// 不复制index.html文件,因为HtmlRspackPlugin会处理它
return !src.endsWith('index.html');
}
}
);
});
}
}
],
optimization: {
splitChunks: {
automaticNameDelimiter: '-',
chunks: 'all',
// 增加maxInitialRequests以允许更多的初始化块
maxInitialRequests: 6,
// 减小最小块大小,允许更细粒度的分割
minSize: 20000,
cacheGroups: {
chunk: {
name: 'vab-chunk',
test: /[\\/]node_modules[\\/]/,
minSize: 131072,
maxSize: 524288,
chunks: 'async',
minChunks: 2,
priority: 10,
},
vue: {
name: 'vue',
test: /[\\/]node_modules[\\/](vue(.*)|core-js)[\\/]/,
chunks: 'initial',
priority: 20,
},
elementUI: {
name: 'element-ui',
test: /[\\/]node_modules[\\/]element-ui(.*)[\\/]/,
priority: 30,
},
// 单独拆分常用工具库
vendors: {
name: 'vendors',
test: /[\\/]node_modules[\\/](lodash|axios|qs|dayjs)[\\/]/,
chunks: 'all',
priority: 35,
},
// 拆分样式资源
styles: {
name: 'styles',
test: /\.(css|scss)$/,
chunks: 'all',
enforce: true,
priority: 40,
},
extra: {
name: 'vab-layouts',
test: resolve('src/layouts'),
priority: 40,
},
},
},
// 添加压缩配置
minimize: mode === 'production',
// 如果是生产环境,增加tree shaking
usedExports: mode === 'production',
},
devServer: {
hot: true,
// 修改端口,避免冲突
port: 8090,
historyApiFallback: true,
static: {
directory: path.join(__dirname, 'public'),
},
client: {
overlay: {
errors: true,
warnings: false,
},
},
open: {
target: ['http://localhost:8090'],
},
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error('dev-server is not defined')
}
if (process.env.VUE_APP_MOCK_ENABLE === 'true') {
const mockServer = require('./mock/index')
mockServer(devServer.app)
}
return middlewares
},
},
}