-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue.config.js
126 lines (118 loc) · 3.38 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
const CompressionPlugin = require('compression-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const path = require('path')
const IS_PROD = process.env.NODE_ENV === 'production'
const cdnDomian = '/'
module.exports = {
publicPath: IS_PROD ? cdnDomian : '/',
css: {
loaderOptions: {
sass: {
implementation: require('sass'),
data: `@import '~styles/var.scss';
@import '~styles/mixin.scss';`
}
}
},
configureWebpack: () => ({
devtool: 'source-map',
resolve: {
alias: {
'~styles': path.resolve('./src/assets/styles'),
'~images': path.resolve('./src/assets/images')
}
}
}),
chainWebpack: config => {
// #region svg-config
const rule = config.module.rule('svg')
rule.exclude.add(path.resolve('./src/assets/icons/svg'))
const svgRule = config.module.rule('auto-svg') // 找到svg-loader
svgRule.uses.clear() // 清除已有的loader, 如果不这样做会添加在此loader之后
svgRule
.test(/\.(svg)(\?.*)?$/)
.exclude
.add(/node_modules/) // 正则匹配排除node_modules目录
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
// #endregion svg-config
if (process.env.NODE_ENV === 'production') {
// #region 图片压缩
config.module
.rule('images')
.test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
.exclude
.add(path.resolve('src/assets/icons/svg'))
.end()
.use('img-loader')
.loader('img-loader').options({
plugins: [
require('imagemin-jpegtran')(),
require('imagemin-pngquant')({
quality: [0.75, 0.85]
})
]
})
// #endregion
// #region 启用GZip压缩
config
.plugin('compression')
.use(CompressionPlugin, {
asset: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
threshold: 10240,
minRatio: 0.8,
cache: true
})
.tap(args => { })
// #endregion
// #region 忽略生成环境打包的文件
var externals = {
vue: 'Vue',
axios: 'axios',
'element-ui': 'ELEMENT',
'vue-router': 'VueRouter',
vuex: 'Vuex'
}
config.externals(externals)
const cdn = {
css: [
// element-ui css
'//unpkg.com/element-ui/lib/theme-chalk/index.css'
],
js: [
// vue
'//cdn.staticfile.org/vue/2.5.22/vue.min.js',
// vue-router
'//cdn.staticfile.org/vue-router/3.0.2/vue-router.min.js',
// vuex
'//cdn.staticfile.org/vuex/3.1.0/vuex.min.js',
// axios
'//cdn.staticfile.org/axios/0.19.0-beta.1/axios.min.js',
// element-ui js
'//unpkg.com/element-ui/lib/index.js'
]
}
config.plugin('html')
.tap(args => {
args[0].cdn = cdn
return args
})
// #endregion
// #region 分析打包体积
if (process.env.IS_ANALYZE) {
config.plugin('webpack-report').use(BundleAnalyzerPlugin, [
{
analyzerMode: 'static'
}
])
}
// #endregion 分析打包体积
}
}
}