-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvue.config.js
162 lines (159 loc) · 5.11 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
const fs = require('fs')
const path = require('path')
const spritesmithPlugin = require('webpack-spritesmith')
const terserPlugin = require('terser-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const cdnDependencies = require('./dependencies.cdn')
const spritesmithTasks = []
fs.readdirSync('src/assets/sprites').map(dirname => {
if (fs.statSync(`src/assets/sprites/${dirname}`).isDirectory()) {
spritesmithTasks.push(
new spritesmithPlugin({
src: {
cwd: path.resolve(__dirname, `src/assets/sprites/${dirname}`),
glob: '*.png'
},
target: {
image: path.resolve(__dirname, `src/assets/sprites/${dirname}.[hash].png`),
css: [
[path.resolve(__dirname, `src/assets/sprites/_${dirname}.scss`), {
format: 'handlebars_based_template',
spritesheetName: dirname
}]
]
},
customTemplates: {
'handlebars_based_template': path.resolve(__dirname, 'scss.template.hbs')
},
// 样式文件中调用雪碧图地址写法
apiOptions: {
cssImageRef: `~${dirname}.[hash].png`
},
spritesmithOptions: {
algorithm: 'binary-tree',
padding: 10
}
})
)
}
})
// CDN 相关
const isCDN = process.env.VUE_APP_CDN == 'ON'
const externals = {}
cdnDependencies.forEach(pkg => {
externals[pkg.name] = pkg.library
})
const cdn = {
css: cdnDependencies.map(e => e.css).filter(e => e),
js: cdnDependencies.map(e => e.js).filter(e => e)
}
// gzip 相关
const isGZIP = process.env.VUE_APP_GZIP == 'ON'
module.exports = {
publicPath: '',
productionSourceMap: false,
devServer: {
open: true,
// proxy: {
// '/': {
// target: process.env.VUE_APP_API_ROOT,
// changeOrigin: true
// }
// },
// 用于 mock-server
// proxy: {
// '/mock': {
// target: '/',
// changeOrigin: true
// },
// '/': {
// target: process.env.VUE_APP_API_ROOT,
// changeOrigin: true
// }
// },
},
transpileDependencies: ['element-ui'],
configureWebpack: config => {
config.resolve.modules = ['node_modules', 'assets/sprites']
config.plugins.push(...spritesmithTasks)
if (isCDN) {
config.externals = externals
}
config.optimization = {
minimizer: [
new terserPlugin({
terserOptions: {
compress: {
warnings: false,
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log']
}
}
})
]
}
if (isGZIP) {
return {
plugins: [
new CompressionPlugin({
algorithm: 'gzip',
test: /\.(js|css)$/, // 匹配文件名
threshold: 10240, // 对超过10k的数据压缩
deleteOriginalAssets: false, // 不删除源文件
minRatio: 0.8 // 压缩比
})
]
}
}
},
pluginOptions: {
lintStyleOnBuild: true,
stylelint: {
fix: true
},
mock: {
entry: './src/mock/server.js',
debug: true,
disable: true
}
},
chainWebpack: config => {
const oneOfsMap = config.module.rule('scss').oneOfs.store
oneOfsMap.forEach(item => {
item.use('sass-resources-loader')
.loader('sass-resources-loader')
.options({
resources: [
'./src/assets/styles/resources/*.scss',
'./src/assets/sprites/*.scss'
]
})
.end()
})
config.module
.rule('svg')
.exclude.add(path.join(__dirname, 'src/assets/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(path.join(__dirname, 'src/assets/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
config.plugin('html')
.tap(args => {
args[0].title = process.env.VUE_APP_TITLE
if (isCDN) {
args[0].cdn = cdn
}
args[0].debugTool = process.env.VUE_APP_DEBUG_TOOL
return args
})
}
}