-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathvue.config.js
340 lines (320 loc) · 9.61 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
'use strict';
const path = require('path');
const {formatDate} = require('@liwb/cloud-utils');
const webpack = require('webpack');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const chalk = require('chalk');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const TerserPlugin = require('terser-webpack-plugin');
const pkg = require('./package.json');
const port = 5577;
// page title
const name = `${pkg.name}`;
const resolve = (dir) => {
return path.join(__dirname, './', dir);
};
const isProd = () => {
return process.env.NODE_ENV === 'production';
};
function addStyleResource(rule) {
rule
.use('style-resource')
.loader('style-resources-loader')
.options({
patterns: [
path.resolve(__dirname, 'src/assets/less/variable.less'),
path.resolve(__dirname, 'node_modules/magicless/magicless.less')
],
injector: 'prepend'
});
}
const getOptimization = () => {
let optimization = {};
if (isProd()) {
optimization = {
// https://webpack.docschina.org/configuration/optimization/#optimization-minimizer
minimizer: [
new TerserPlugin({
terserOptions: {
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
compress: {
warnings: false,
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log']
}
}
})
]
};
}
return optimization;
};
const genPlugins = () => {
const plugins = [
new ProgressBarPlugin({
format: ' build [:bar] ' + chalk.green.bold(':percent') + ' (:elapsed seconds)',
clear: false
}),
new webpack.DllReferencePlugin({
context: process.cwd(),
manifest: require('./public/vendor/element-manifest.json')
}),
new webpack.DllReferencePlugin({
context: process.cwd(),
manifest: require('./public/vendor/axios-manifest.json')
}),
new webpack.DllReferencePlugin({
context: process.cwd(),
manifest: require('./public/vendor/vendor-manifest.json')
}),
// 将 dll 注入到 生成的 html 模板中
new AddAssetHtmlPlugin({
// dll文件位置
filepath: path.resolve(__dirname, './public/vendor/*.js'),
// dll 引用路径
publicPath: './vendor',
// dll最终输出的目录
outputPath: './vendor'
}),
new AddAssetHtmlPlugin({
// dll文件位置
filepath: path.resolve(__dirname, './public/config.local.js'),
hash: true
}),
new AddAssetHtmlPlugin({
// dll文件位置
filepath: path.resolve(__dirname, './public/vendor/*.css'),
// dll 引用路径
publicPath: './vendor',
// dll最终输出的目录
outputPath: './vendor',
typeOfAsset: 'css'
}),
// bannerPlugin
new webpack.BannerPlugin({
banner: `Current version ${pkg.version} and build time ${formatDate(
new Date(),
'yyyy-MM-dd HH:mm:ss'
)}`
})
];
if (isProd()) {
plugins.push(
new CompressionWebpackPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp(
'\\.(' +
['js', 'css'].join('|') +
')$'
),
threshold: 10240,
minRatio: 0.8,
cache: true
})
);
}
return plugins;
};
module.exports = {
/**
* You can set by yourself according to actual condition
* You will need to set this if you plan to deploy your site under a sub path,
* for example GitHub pages. If you plan to deploy your site to https://foo.github.io/bar/,
* then assetsPublicPath should be set to "/bar/".
* In most cases please use '/' !!!
* Detail https://cli.vuejs.org/config/#baseurl
*/
publicPath: './',
assetsDir: 'static',
lintOnSave: process.env.NODE_ENV !== 'production',
productionSourceMap: false,
// webpack-dev-server 相关配置
devServer: {
open: process.platform === 'darwin',
host: '0.0.0.0',
port,
https: false,
hotOnly: false,
overlay: {
warnings: false,
errors: true
},
proxy: {
// change xxx-api/login => mock/login
// detail: https://cli.vuejs.org/config/#devserver-proxy
[process.env.VUE_APP_BASE_API]: {
target: `http://localhost:${port}/mock`,
changeOrigin: true,
pathRewrite: {
['^' + process.env.VUE_APP_BASE_API]: ''
}
}
},
after: require('./mock/mockServer.ts')
},
// css相关配置
css: {
// 是否使用css分离插件 ExtractTextPlugin
extract: isProd() ? true : false,
// 开启 CSS source maps?
sourceMap: isProd() ? true : false,
// css预设器配置项
loaderOptions: {}
},
configureWebpack: () => ({
name: name,
resolve: {
alias: {
'@': resolve('src'),
'@assets': resolve('src/assets'),
'@less': resolve('src/assets/less'),
'@js': resolve('src/assets/js'),
'@components': resolve('src/components'),
'@mixins': resolve('src/mixins'),
'@filters': resolve('src/filters'),
'@store': resolve('src/store'),
'@views': resolve('src/views'),
// 文件别名
'services': resolve('src/services'),
'variable': resolve('src/assets/less/variable.less'),
'mixins': resolve('node_modules/magicless/magicless.less')
}
},
plugins: genPlugins(),
// 生产环境去掉 console.log
// https://github.com/cklwblove/vue-cli3-template/issues/12
optimization: getOptimization()
}),
// webpack配置
// see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
chainWebpack: (config) => {
// module
// style-resources-loader
const types = ['vue-modules', 'vue', 'normal-modules', 'normal'];
types.forEach((type) =>
addStyleResource(config.module.rule('less').oneOf(type))
);
// svg
// exclude icons
config.module
.rule('svg')
.exclude.add(resolve('src/icons'))
.end();
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/icons'))
.end()
.use('url-loader')
.loader('url-loader')
.end();
config
.when(process.env.NODE_ENV === 'development',
config => config.devtool('source-map')
);
// runtime.js 内联的形式嵌入
config
.plugin('preload')
.tap(args => {
args[0].fileBlacklist.push(/runtime\./);
return args;
});
// plugin
// webpack-html-plugin
config
.plugin('html')
.tap(args => {
args[0].minify = {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
};
return args;
});
// set preserveWhitespace
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
.tap(options => {
options.compilerOptions.preserveWhitespace = true;
return options;
})
.end();
// optimization
// https://imweb.io/topic/5b66dd601402769b60847149
config
.when(process.env.NODE_ENV === 'production',
config => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.after('html')
.use('script-ext-html-webpack-plugin', [{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}])
.end();
config
.optimization
.splitChunks({
chunks: 'all',
cacheGroups: {
vendors: {
name: 'chunk-vendors',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // 只打包初始时依赖的第三方
},
// elementUI: {
// name: 'chunk-elementUI', // split elementUI 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[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
// },
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // 可自定义拓展你的规则
minChunks: 3, // 最小公用次数
priority: 5,
reuseExistingChunk: true
}
}
});
config.optimization.runtimeChunk('single');
}
);
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run analyz`
config
.when(process.env.IS_ANALYZ,
config => config
.plugin('webpack-bundle-analyzer')
.use(BundleAnalyzerPlugin, [{
analyzerPort: 8888,
generateStatsFile: false
}])
);
// `npm run build --generate_report`
config
.when(process.env.npm_config_generate_report,
config => config
.plugin('webpack-bundle-analyzer-report')
.use(BundleAnalyzerPlugin, [{
analyzerMode: 'static',
reportFilename: 'bundle-report.html',
openAnalyzer: false
}])
);
}
};