forked from buqiyuan/vue3-antdv-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue.config.js
248 lines (232 loc) · 7.87 KB
/
vue.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
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
const path = require('path');
const { defineConfig } = require('@vue/cli-service');
const webpack = require('webpack');
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
const dayjs = require('dayjs');
const TerserPlugin = require('terser-webpack-plugin');
const defineOptions = require('unplugin-vue-define-options/webpack');
const resolve = (dir) => path.join(__dirname, dir); // 路径
const pkg = require('./package.json');
process.env.VUE_APP_VERSION = pkg.version;
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
const IS_DEV = ['development'].includes(process.env.NODE_ENV);
// port = 8098 npm run dev OR npm run dev --port = 8098
const port = process.env.port || process.env.npm_config_port || 8098; // dev port
const __APP_INFO__ = {
pkg,
lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
};
// https://next.cli.vuejs.org/
module.exports = defineConfig({
lintOnSave: IS_DEV, //关闭eslint检查
// publicPath: isDev ? '' : querystring.unescape('<%=request.getContextPath()%>'),
publicPath: process.env.BASE_URL,
// filenameHashing: false,
productionSourceMap: false,
css: {
loaderOptions: {
// css: {
// modules: {
// auto: (path) => {
// return path.includes('ant-design-vue/dist/antd.dark.css');
// },
// },
// },
less: {
lessOptions: {
javascriptEnabled: true,
modifyVars: {},
},
additionalData: `
@import "ant-design-vue/lib/style/themes/default.less";
@import "~@/styles/variables.less";
`,
},
// sass: {
// additionalData: `
// @use 'sass:math';
// @import "@/styles/global.scss";`
// }
},
},
chainWebpack: (config) => {
// 移除 preload 插件
config.plugins.delete('preload');
// 移除 prefetch 插件
config.plugins.delete('prefetch');
// 优化二次启动速度
config.cache({
// 将缓存类型设置为文件系统,默认是memory
type: 'filesystem',
buildDependencies: {
// 更改配置文件时,重新缓存
config: [__filename],
},
});
// https://webpack.js.org/configuration/optimization/#optimizationruntimechunk
config.optimization.runtimeChunk('single');
config
// https://webpack.js.org/configuration/devtool/#development
.when(IS_DEV, (config) => config.devtool('cheap-source-map'));
// 配置相关loader,支持修改,添加和替换相关的loader
config.resolve.alias.set('@', resolve('src'));
config.resolve.alias.set('vue-i18n', 'vue-i18n/dist/vue-i18n.cjs.js');
if (process.env.DEBUG_ANTDV) {
console.info('DEBUG_ANTDV', process.env.DEBUG_ANTDV);
config.resolve.alias.set('ant-design-vue/es/', 'ant-design-vue/components/');
config.resolve.alias.set('ant-design-vue/lib/', 'ant-design-vue/components/');
config.resolve.alias.set('vue', 'ant-design-vue/node_modules/vue');
}
config.plugin('html').tap((args) => {
args[0].title = 'vue3-antd-admin管理系统';
return args;
});
config.module
.rule('css')
.exclude.add(resolve('node_modules/ant-design-vue/dist/antd.dark.css'))
.end();
config.module.rule('raw-css').resourceQuery(/raw/).type('asset/source');
// 忽略解析markdown文件
config.module.noParse(/\.md$/);
// config.module.rule('css').test(/\.ts$/).resourceQuery(/raw/).type('asset/source').end();
// svg rule loader
config.module.rule('svg').exclude.add(resolve('src/assets/icons')).end();
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/assets/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'svg-icon-[name]',
});
config.when(IS_PROD, (config) => {
// split
config.optimization.splitChunks({
chunks: 'all', //指定哪些模块需要打包
cacheGroups: {
libs: {
name: 'chunk-libs',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial', // only package third parties that are initially dependent
},
antdv: {
name: 'chunk-ant-design-vue', // split ant-design-vue into a single package
priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?ant-design-vue(.*)/, // in order to adapt to cnpm
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // can customize your rules
minChunks: 3, // 被引用3次就提取出来
priority: 5,
reuseExistingChunk: true, // 表示是否使用已有的 chunk,如果为 true 则表示如果当前的 chunk 包含的模块已经被抽取出去了,那么将不会重新生成新的。
},
},
});
config.module
.rule('md')
.test(/\.md$/)
.type('javascript/auto')
.use('asset')
.loader('asset')
.options({
limit: 100,
esModule: false,
generator: () => '',
});
});
},
configureWebpack: (config) => {
// 开启顶级await
config.experiments = {
topLevelAwait: true,
};
config.resolve.fallback = { path: require.resolve('path-browserify') };
config.plugins.push(
// 定义全局变量
new webpack.DefinePlugin({
__APP_INFO__: JSON.stringify(__APP_INFO__),
}),
// 打包速度分析
new SpeedMeasurePlugin(),
// use defineOptions https://github.com/sxzz/unplugin-vue-define-options
defineOptions({
include: [/\.vue$/, /\.vue\?vue/],
}),
);
if (IS_PROD) {
// terser-webpack-plugin (https://webpack.docschina.org/plugins/terser-webpack-plugin/);
const TerserPluginIndex = config.optimization.minimizer.findIndex(
(n) => n.__pluginName === 'terser',
);
config.optimization.minimizer[TerserPluginIndex] = new TerserPlugin({
terserOptions: {
warnings: false,
format: {
comments: false,
},
compress: {
drop_debugger: true, // 注释console
drop_console: true,
pure_funcs: ['console.log'], // 移除console
},
},
extractComments: false, // 是否将注释提取到一个单独的文件中
parallel: true, // 是否并⾏打包
});
}
},
devServer: {
port,
client: {
progress: true,
overlay: {
errors: true,
warnings: false,
runtimeErrors: false,
},
},
// watchOptions: {
// // 开发时,自动保存代码导致构建频繁且会报错,又不想手动保存,则可以开启延迟构建
// aggregateTimeout: 1500,
// ignored: /node_modules/,
// },
proxy: {
// '/mock-api': {
// target: `http://localhost:${port}`,
// changeOrigin: true,
// logLevel: 'debug',
// pathRewrite: {
// '^/mock-api': ''
// }
// },
'^/api': {
// target: process.env.VUE_APP_API_URL,
target: 'https://nest-api.buqiyuan.site/api/',
// target: 'http://127.0.0.1:7001',
changeOrigin: true,
logLevel: 'debug',
pathRewrite: {
'^/api': '',
},
on: {
// proxyReq: (proxyReq) => {
// // add custom header to request
// proxyReq.setHeader('Referer', 'https://vue3-antd-admin.vercel.app');
// },
},
},
'^/ws-api': {
target: 'wss://nest-api.buqiyuan.site',
// target: 'http://127.0.0.1:7002',
changeOrigin: true, //是否允许跨域
wss: true,
logLevel: 'debug',
},
},
setupMiddlewares: require('./src/mock/mock-server.js'),
},
});