-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathvue.config.js
181 lines (177 loc) · 5.56 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
const path = require('path')
const webpack = require('webpack')
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const SentryWebpackPlugin = require('@sentry/webpack-plugin')
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
const { ESBuildMinifyPlugin } = require('esbuild-loader')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
const IS_PROD = process.env.NODE_ENV === 'production'
const getAliasPath = dir => path.join(__dirname, dir)
module.exports = {
productionSourceMap: false,
lintOnSave: !IS_PROD,
publicPath: IS_PROD ? 'https://cdn.kimjisoo.cn/' : '/',
pages: {
index: {
entry: './src/main.js',
template: './public/index.html',
title: 'ZMind思维导图',
chunks: [
'chunk-vendors',
'chunk-vendors-2',
'chunk-ele',
'index'
]
}
},
chainWebpack: config => {
config.when(!IS_PROD, config => {
const rule = config.module.rule('js')
// 清理自带的 babel-loader
rule.uses.clear();
// 添加 esbuild-loader
rule
.use('esbuild-loader')
.loader('esbuild-loader')
.options({
target: 'es2015'
})
// 删除底层 terser, 换用esbuild-minimize-plugin
config.optimization.minimizers.delete('terser')
// 使用 esbuild 优化 css 压缩
config.optimization
.minimizer('esbuild')
.use(ESBuildMinifyPlugin, [{ minify: true, css: true }])
})
config.when(IS_PROD, config => {
// 移除prefetch插件 减少对首页加载的带宽占用
// config.plugins.delete('prefetch')
// config.optimization.runtimeChunk({
// // https://webpack.docschina.org/configuration/optimization/#optimizationruntimechunk
// name: (entrypoint) => `runtime~${entrypoint.name}`,
// }),
config.optimization.splitChunks({
cacheGroups: {
// 其他的第三方库集中在一起
vendors: {
name: 'chunk-vendors',
test: /[\\/]node_modules[\\/]/,
chunks: 'initial',
priority: 2,
reuseExistingChunk: true,
enforce: true
},
// @sentry|@vueuse|cropperjs 很大 单独分包
vendors2: {
name: 'chunk-vendors-2',
test: /[\\/]node_modules[\\/]@sentry|@vueuse|cropperjs/,
chunks: 'initial',
priority: 3,
reuseExistingChunk: true,
enforce: true
},
// 只有index入口用 且很大 需要单独打包
elementui: {
name: 'chunk-ele',
test: /[\\/]node_modules[\\/]@?element-plus[\\/]/,
chunks: 'initial',
priority: 5,
reuseExistingChunk: true,
enforce: true
}
}
}),
config.optimization.minimizer('terser').tap(args => {
// 移除 debugger
args[0].terserOptions.compress.drop_debugger = true
// 移除 console.log
args[0].terserOptions.compress.pure_funcs = ['console.log']
// 移除 注释
args[0].terserOptions.output = {
comments: false
}
return args
})
})
const svgRule = config.module.rule('svg')
svgRule.uses.clear()
svgRule
.test(/\.svg$/)
.include.add(path.resolve(__dirname, 'src/assets/pic'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
.use('svgo-loader')
.loader('svgo-loader')
.options({
name: 'removeAttrs',
params: { attrs: 'fill' }
})
.end()
config.module
.rule('worker')
.test(/\.worker\.js$/)
.use('worker-loader')
.loader('worker-loader')
.end()
// 解决:worker 热更新问题
config.module.rule('js').exclude.add(/\.worker\.js$/)
config.resolve.alias
.set('@', getAliasPath('src'))
.set('assets', getAliasPath('src/assets'))
.set('hooks', getAliasPath('src/hooks'))
.set('views', getAliasPath('src/views'))
.set('store', getAliasPath('src/store'))
.set('components', getAliasPath('src/components'))
},
configureWebpack: {
resolveLoader: {
modules: ['node_modules', path.resolve(__dirname, 'loaders')],
},
module: {
rules: [
{
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto'
}
]
},
plugins: [
new HardSourceWebpackPlugin(),
new webpack.DefinePlugin({
BASE_API_URL: IS_PROD
? JSON.stringify('https://mapapi.kimjisoo.cn')
: JSON.stringify('http://localhost:3003')
}),
AutoImport({
resolvers: [ElementPlusResolver()]
}),
Components({
resolvers: [ElementPlusResolver()]
}),
require('unplugin-element-plus/webpack')({}),
new SentryWebpackPlugin({
dryRun: !IS_PROD, // 只有生成环境才上传source map
include: 'dist',
ignore: ['node_modules', 'vue.config.js']
}),
//* 实际上 七牛cdn对打包好的源文件自动进行了gzip
// new CompressionWebpackPlugin({
// filename: '[path].gz[query]',
// algorithm: 'gzip',
// test: /\.js$|\.css$|\.html$|\.ttf$|\.eot$|\.woff$/,
// threshold: 10240,
// minRatio: 0.8,
// deleteOriginalAssets: false
// }),
new SpeedMeasurePlugin()
]
}
}