-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue.config.js
93 lines (88 loc) · 3.22 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
// 导入compression-webpack-plugin
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
// const webpack = require('webpack');
const path = require('path');
// 本地环境是否需要使用cdn
const devNeedCdn = true;
module.exports = {
devServer: {
proxy: {
'^/api': {
target: 'https://xydh.fun',
ws: true,
onProxyReq(proxyReq) {
// 这里删除 origin 头部,避免代理跨域
proxyReq.removeHeader('origin');
},
},
},
},
configureWebpack: config => {
// 引入uglifyjs-webpack-plugin
const UglifyPlugin = require('terser-webpack-plugin');
if (process.env.NODE_ENV === 'production') {
// 为生产环境修改配置
config.mode = 'production';
// 将每个依赖包打包成单独的js文件
const optimization = {
minimizer: [
new UglifyPlugin({
terserOptions: {
warnings: false,
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log'],
},
},
sourceMap: false,
parallel: true,
}),
],
};
Object.assign(config, {
optimization,
});
// gzip压缩
const productionGzipExtensions = ['html', 'js', 'css'];
config.plugins.push(
new CompressionWebpackPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
threshold: 10240, // 只有大小大于该值的资源会被处理 10240
minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
deleteOriginalAssets: false, // 删除原文件
})
);
// 分析打包后的文件
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static', // 生成html文件
})
);
} else {
// 为开发环境修改配置
config.mode = 'development';
}
// 添加新的 externals 配置
Object.assign(config, {
externals: {
vue: 'Vue',
vuex: 'Vuex',
'element-ui': 'ELEMENT',
lodash: '_',
},
});
},
// chainWebpack: config => {
// config.plugins.delete('prefetch');
// config.plugin('chunkPlugin').use(webpack.optimize.LimitChunkCountPlugin, [
// {
// maxChunks: 2, // 必须大于或等于 1,此处设置成最多生成5个chuank.js文件
// minChunkSize: 10000,
// },
// ]);
// },
};