-
Notifications
You must be signed in to change notification settings - Fork 10
/
vue.config.js
227 lines (218 loc) · 8.09 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
'use strict'
const defaultSettings = require('./src/settings.js')
const utils = require('./build/utils')
const CompressionPlugin = require('compression-webpack-plugin')
// 监测打包各个阶段的时间
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
// bundle分析
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
// const TerserPlugin = require('terser-webpack-plugin')
// const FilterWarningsPlugin = require('webpack-filter-warnings-plugin')
const name = defaultSettings.title || 'HyperfAdmin' // page title
const hashType = process.env.NODE_ENV === 'development' ? 'hash' : 'contenthash:8'
const port = process.env.port || process.env.npm_config_port || 9528 // dev port
// All configuration item explanations can be find in https://cli.vuejs.org/config/
const smp = new SpeedMeasurePlugin({
outputFormat: 'human'
})
module.exports = {
publicPath: '/',
outputDir: 'dist',
assetsDir: 'static',
pages: utils.getEntry(),
lintOnSave: process.env.NODE_ENV === 'development',
productionSourceMap: !(process.env.ENV === 'production'), // 去除map文件
devServer: {
port: port,
open: true,
overlay: {
warnings: false,
errors: true
},
proxy: {
[process.env.VUE_APP_BASE_API]: {
// 本地联调是请代理到对应开发ip
target: `http://127.0.0.1:9511`,
changeOrigin: true,
pathRewrite: {
['^' + process.env.VUE_APP_BASE_API]: ''
},
logLevel: 'debug'
}
},
before(app) {
app.get('/', (req, res, next) => {
if (process.env.BUILD_UNIT && process.env.ENV === 'development') {
res.redirect(`/${process.env.BUILD_UNIT}/`)
} else {
res.redirect('/system/')
}
})
}
// after: require('./mock/mock-server.js')
},
configureWebpack(config) {
const baseConf = {
name: name,
resolve: {
alias: {
'@': utils.resolve('src'),
'vue$': 'vue/dist/vue.esm.js'
}
},
output: {
filename: `[name]/js/[name].[${hashType}].js`,
publicPath: '/',
chunkFilename: `static/js/[name].[${hashType}].js`
},
stats: 'none', // 屏蔽打包报警
plugins: []
}
if (process.env.ENV === 'development') {
if (process.env.BUILD_BNDAL) {
baseConf.plugins.push(new BundleAnalyzerPlugin())
}
if (process.env.BUILD_SMP) {
return smp.wrap(baseConf)
}
}
if (process.env.ENV === 'production') {
baseConf.plugins = [
new CompressionPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i, // 匹配文件名
threshold: 10240, // 对超过10k的数据压缩
deleteOriginalAssets: false // false 不删除源文件 true 删除源文件
})
// 打包时过滤掉 console 打印
// new TerserPlugin({
// terserOptions: {
// compress: {
// warnings: false,
// drop_console: true,
// drop_debugger: true,
// pure_funcs: ['console.log']
// }
// }
// })
// new FilterWarningsPlugin({
// exclude: /mini-css-extract-plugin[^]*Conflicting order between:/
// })
]
}
return baseConf
},
chainWebpack(config) {
config.plugins.delete('preload')
config.plugins.delete('prefetch')
// set svg-sprite-loader
config.module
.rule('svg')
.exclude.add(utils.resolve('src/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(utils.resolve('src/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
// set preserveWhitespace
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
.tap(options => {
options.compilerOptions.preserveWhitespace = true
return options
})
.end()
config
// https://webpack.js.org/configuration/devtool/#development
.when(process.env.NODE_ENV === 'development',
config => config.devtool('cheap-source-map')
)
config
.when(process.env.NODE_ENV !== 'development',
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: {
libs: {
name: 'chunk-libs',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // only package third parties that are initially dependent
},
vue: {
name: 'chunk-vue', // 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[\\/]_?vue(.*)/ // in order to adapt to cnpm
},
lodash: {
name: 'chunk-lodash', // 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[\\/]_?lodash(.*)/ // in order to adapt to cnpm
},
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
},
codeMirror: {
name: 'chunk-codeMirror', // 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[\\/]_?codemirror(.*)/ // in order to adapt to cnpm
},
antv: {
name: 'chunk-antv', // 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[\\/]_?@antv(.*)/ // in order to adapt to cnpm
},
jsonEditor: {
name: 'chunk-jsonEditor', // 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[\\/]_?v-jsoneditor(.*)/ // in order to adapt to cnpm
},
vue2editor: {
name: 'chunk-vue2editor', // 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[\\/]_?vue2-editor(.*)/ // in order to adapt to cnpm
},
commons: {
name: 'chunk-commons',
test: utils.resolve('src/components'), // can customize your rules
minChunks: 1, // minimum common number
priority: 5,
reuseExistingChunk: true,
chunks: 'initial'
}
}
})
// config.optimization.runtimeChunk('single')
}
)
config.plugin('extract-css')
.use(require('mini-css-extract-plugin'), [
{
filename: `[name]/css/[name].[${hashType}].css`,
chunkFilename: `static/css/[name].[${hashType}].css`
}
])
// config.plugins.delete('friendly-errors') // 屏蔽打包报警
}
}