-
Notifications
You must be signed in to change notification settings - Fork 45
/
vue.config.js
199 lines (174 loc) · 5.85 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
const path = require('path')
const webpack = require('webpack')
const createThemeColorReplacerPlugin = require('./config/plugin.config')
const projectConfig = require('./src/config/projectConfig')
const { defineConfig } = require("@vue/cli-service");
// 预览环境的服务端地址,没有启动后端时,可以通过此地址进行前端查看
// const serverAddress = 'http://admin.ballcat.cn'
// const serverAddress = 'http://ballcat-admin:8080'
const serverAddress = 'https://localhost:8080'
function resolve(dir) {
return path.join(__dirname, dir)
}
/**
* 判断当前是否是生产环境
* @returns {boolean}
*/
function isProd() {
return process.env.NODE_ENV === 'production'
}
// 页面标题
const name = projectConfig.projectTitle
// cdn 前缀,免费的 cdn 有 unpkg 和 jsdelivr
// 注意:免费 cdn 不稳定,建议生成使用付费 cdn,或者使用 oss 地址
const cndPrefix = 'unpkg.com'
// const cndPrefix = 'cdn.jsdelivr.net/npm'
const assetsCDN = {
css: [],
// https://unpkg.com/browse/[email protected]/
js: [
`//${cndPrefix}/[email protected]/dist/vue.min.js`,
`//${cndPrefix}/[email protected]/dist/vue-router.min.js`,
`//${cndPrefix}/[email protected]/dist/vuex.min.js`,
`//${cndPrefix}/[email protected]/dist/axios.min.js`
]
}
// webpack build externals
const prodExternals = {
vue: 'Vue',
'vue-router': 'VueRouter',
vuex: 'Vuex',
axios: 'axios'
}
// vue.config.js
const vueConfig = {
configureWebpack: {
// provide the app's title in webpack's name field, so that
// it can be accessed in index.html to inject the correct title.
name: name,
// webpack plugins
plugins: [
// Ignore all locale files of moment.js
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/,
})
],
// if prod is on, add externals
externals: isProd() ? prodExternals : {}
},
chainWebpack: (config) => {
// it can improve the speed of the first screen, it is recommended to turn on preload
// config.plugin('preload').tap(() => [
// {
// rel: 'preload',
// // to ignore runtime.js
// // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
// fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
// include: 'initial'
// }
// ])
// when there are many pages, it will cause too many meaningless requests
config.plugins.delete('prefetch')
config.resolve.alias
.set('@$', resolve('src'))
const svgRule = config.module.rule('svg')
svgRule.uses.clear()
// https://github.com/vuejs/vue-cli/issues/6785
svgRule.delete('type');
svgRule.delete('generator');
svgRule
.use('vue-loader')
.loader('vue-loader')
.end()
.use('vue-svg-loader')
.loader('vue-svg-loader');
// if prod is on
// assets require on cdn
if (isProd()) {
config.plugin('html').tap(args => {
args[0].cdn = assetsCDN
return args
})
// 内联 Manifest
// 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' // 只打包初始时依赖的第三方
},
antDesignVue: {
name: 'chunk-AntDesignVue', // 单独将 AntDesignVue 拆包
priority: 20, // 权重要大于 libs 和 app 不然会被打包进 libs 或者 app
test: /[\\/]node_modules[\\/]_?ant-design-vue(.*)/
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // 可自定义拓展你的规则
minChunks: 3, // // 最小共用次数
priority: 5,
reuseExistingChunk: true
}
}
})
// https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
config.optimization.runtimeChunk('single')
}
},
css: {
loaderOptions: {
less: {
lessOptions: {
modifyVars: {
// less vars,customize ant design theme
// 'primary-color': '#F5222D',
// 'link-color': '#F5222D',
'text-color': 'rgba(0, 0, 0, 0.85)', // 主文本色
'border-radius-base': '2px', // 按钮圆角
'layout-header-height': '48px', // layout 头高度
'menu-collapsed-width': '48px' // inline 菜单收起宽度
},
javascriptEnabled: true,
// 兼容 less3 到 less4 的语法变动问题 https://github.com/vueComponent/ant-design-vue/issues/3665
math: 'always'
}
}
}
},
devServer: {
// development server port 8000
port: 8000,
// If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
proxy: {
'^/api': {
target: serverAddress,
changeOrigin: true,
ws: true,
pathRewrite: {
'^/api': '/'
}
}
}
},
// disable source map in production
productionSourceMap: false,
lintOnSave: !isProd(),
// babel-loader no-ignore node_modules/*
transpileDependencies: true
}
if (projectConfig.enableLayoutSetting) {
// add `ThemeColorReplacer` plugin to webpack plugins
vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
}
module.exports = defineConfig(vueConfig)