-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
vue.config.js
267 lines (261 loc) · 7.69 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const VueFilenameInjector = require('@d2-projects/vue-filename-injector')
const ThemeColorReplacer = require('webpack-theme-color-replacer')
const forElementUI = require('webpack-theme-color-replacer/forElementUI')
const { set, each, compact, map, keys } = require('lodash')
const resolve = dir => require('path').join(__dirname, dir)
// Add environment variable
process.env.VUE_APP_VERSION = require('./package.json').version
process.env.VUE_APP_BUILD_TIME = require('dayjs')().format('YYYY-M-D HH:mm:ss')
// Build configuration for multiple pages
const pages = {
index: {
entry: 'src/main.js',
template: 'public/index.html',
filename: 'index.html',
chunks: [
'manifest',
'index',
'chunk-index',
'chunk-vendor',
'chunk-common',
'chunk-vue',
'chunk-element'
]
},
mobile: {
entry: 'src.mobile/main.js',
template: 'public/mobile.html',
filename: 'mobile.html',
chunks: [
'manifest',
'mobile',
'chunk-mobile',
'chunk-vendor',
'chunk-common',
'chunk-vue'
]
}
}
// Set up the external dependency package introduced by the method of using CDN
// For example
// if you set the Axios related link configuration here
// Axios will no longer participate in the packaging during the construction. Finally
// the external link you configured will be used to import Axios in the build result
const cdn = {
// Which external dependencies related to index page are introduced in the form of CDN links
index: [
// {
// name: 'axios',
// library: 'axios',
// js: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js',
// css: ''
// }
],
// Which external dependencies related to mobile page are introduced in the form of CDN links
mobile: [
// {
// name: 'axios',
// library: 'axios',
// js: 'https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js',
// css: ''
// }
]
}
// Set external dependent packages that do not participate in the build
const externals = {}
keys(pages).forEach(name => {
cdn[name].forEach(p => {
externals[p.name] = p.library
})
})
module.exports = {
publicPath: process.env.VUE_APP_PUBLIC_PATH || '/',
lintOnSave: true,
devServer: {
publicPath: process.env.VUE_APP_PUBLIC_PATH || '/',
disableHostCheck: process.env.NODE_ENV === 'development'
},
css: {
loaderOptions: {
sass: {
additionalData: '@use "@/assets/style/public.scss" as *;'
},
less: {
lessOptions: {
modifyVars: {
blue: '#2262AB'
}
}
}
}
},
pages,
configureWebpack: config => {
const configNew = {}
if (process.env.NODE_ENV === 'production') {
configNew.externals = externals
configNew.plugins = [
// gzip
new CompressionWebpackPlugin({
filename: '[path].gz[query]',
test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
threshold: 10240,
minRatio: 0.8,
deleteOriginalAssets: false
})
]
}
return configNew
},
// default: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
chainWebpack: config => {
config.optimization.runtimeChunk({
name: 'manifest'
})
config.optimization.splitChunks({
cacheGroups: {
// External dependencies common to all pages
libs: {
name: 'chunk-vendor',
chunks: 'initial',
minChunks: 1,
test: /[\\/]node_modules[\\/]/,
priority: 1,
reuseExistingChunk: true,
enforce: true
},
// Code common to all pages
common: {
name: 'chunk-common',
chunks: 'initial',
minChunks: 2,
maxInitialRequests: 5,
minSize: 0,
priority: 2,
reuseExistingChunk: true,
enforce: true
},
// External dependencies that are only used by the index page
index: {
name: 'chunk-index',
chunks: 'all',
minChunks: 1,
test: /[\\/]node_modules[\\/](sortablejs|screenfull|nprogress|hotkeys-js|fuse\.js|better-scroll|lowdb|shortid)[\\/]/,
priority: 3,
reuseExistingChunk: true,
enforce: true
},
// External dependencies that are only used by the mobile page
mobile: {
name: 'chunk-mobile',
chunks: 'all',
minChunks: 1,
test: /[\\/]node_modules[\\/](vant)[\\/]/,
priority: 3,
reuseExistingChunk: true,
enforce: true
},
// Vue family packages
vue: {
name: 'chunk-vue',
test: /[\\/]node_modules[\\/](vue|vue-router|vuex)[\\/]/,
chunks: 'all',
priority: 3,
reuseExistingChunk: true,
enforce: true
},
// only element-ui
element: {
name: 'chunk-element',
test: /[\\/]node_modules[\\/]element-ui[\\/]/,
chunks: 'all',
priority: 3,
reuseExistingChunk: true,
enforce: true
}
}
})
// Add the CDN settings to the settings of the htmlwebpackplugin plug-in
keys(pages).forEach(name => {
const packages = cdn[name]
config.plugin(`html-${name}`).tap(options => {
const setting = {
css: compact(map(packages, 'css')),
js: compact(map(packages, 'js'))
}
set(options, '[0].cdn', process.env.NODE_ENV === 'production' ? setting : [])
return options
})
})
// Remove prefetch preload settings for lazy load modules
each(keys(pages), name => {
config.plugins.delete(`prefetch-${name}`)
config.plugins.delete(`preload-${name}`)
})
// webpack-theme-color-replacer
config
.plugin('theme-color-replacer')
.use(ThemeColorReplacer, [{
fileName: 'css/theme-colors.[contenthash:8].css',
matchColors: [
...forElementUI.getElementUISeries(process.env.VUE_APP_ELEMENT_COLOR) // Element-ui主色系列
],
externalCssFiles: ['./node_modules/element-ui/lib/theme-chalk/index.css'], // optional, String or string array. Set external css files (such as cdn css) to extract colors.
changeSelector: forElementUI.changeSelector
}])
config
// The development environment sourcemap does not contain column information
.when(process.env.NODE_ENV === 'development',
config => config.devtool('cheap-source-map')
)
// Add file name
.when(
process.env.VUE_APP_SCOURCE_LINK === 'TRUE',
config => VueFilenameInjector(config, {
propName: process.env.VUE_APP_SOURCE_VIEWER_PROP_NAME
})
)
// markdown
config.module
.rule('md')
.test(/\.md$/)
.use('text-loader')
.loader('text-loader')
.end()
// svg
const svgRule = config.module.rule('svg')
svgRule.uses.clear()
svgRule
.include
.add(resolve('src/assets/svg-icons/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'd2-[name]'
})
.end()
// image exclude
const imagesRule = config.module.rule('images')
imagesRule
.test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
.exclude
.add(resolve('src/assets/svg-icons/icons'))
.end()
// set alias
config.resolve.alias
.set('@.mobile', resolve('src.mobile'))
},
// 不输出 map 文件
productionSourceMap: false,
// i18n
pluginOptions: {
i18n: {
locale: 'zh-chs',
fallbackLocale: 'en',
localeDir: 'locales',
enableInSFC: true
}
}
}