-
Notifications
You must be signed in to change notification settings - Fork 2
/
vue.config.js
150 lines (138 loc) · 4.04 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
const glob = require('glob')
const path = require('path')
const resolve = folder => path.resolve(__dirname, folder)
const VUE_APP_ALLOW_ENTRY = process.env.VUE_APP_ALLOW_ENTRY || ''
// 多页面入口路径
const PAGE_PATH = resolve('src/pages')
/**
* 获取多页面配置对象
*/
function getPagesConfig(entry) {
const pages = {}
// 规范中定义每个单页文件结构
// index.html,main.js,App.vue
glob.sync(PAGE_PATH + '/*/main.js')
.forEach(filePath => {
const pageName = path.basename(path.dirname(filePath))
if (entry && entry !== pageName) return
pages[pageName] = {
entry: filePath,
// 除了首页,其他按第二级目录输出
// 浏览器中直接访问/news,省去/news.html
fileName: `${pageName === 'index' ? '' : pageName + '/'}index.html`,
template: path.dirname(filePath) + '/index.html',
chunks: ['vue-common', '<%= options['ui-framework'] %>', 'echarts', 'vendors', 'manifest', pageName]
}
})
return pages
}
/**
* 样式预处理器全局变量资源插件
* @param {String} rule webpack 规则
*/
function addStyleResource (rule) {
rule.use('style-resource')
.loader('style-resources-loader')
.options({
patterns: [
resolve('./src/assets/<%= options.cssPreprocessor%>/var.<%= options.cssPreprocessor%>'),
],
})
}
const pages = getPagesConfig(VUE_APP_ALLOW_ENTRY)
module.exports = {
// 多页配置
pages: {
...pages
// ,
// 手动设置单页
// about: 'src/pages/about/main.js'
},
// 自定义webpack配置
configureWebpack: {
cache: true,
plugins: [],
performance: {
hints: false
},
optimization: {
runtimeChunk: process.env.NODE_ENV === 'production' ? { name: 'manifest' } : false,
splitChunks: {
automaticNameDelimiter: '--',
cacheGroups: {
vendors: {
name: 'vendors',
chunks: 'initial',
test: /[\\/]node_modules[\\/]/,
priority: 2
},
vue: {
name: 'vue-common',
test: (module) => {
return /vue|axios/g.test(module.context)
},
chunks: 'initial',
priority: 10
},
'<%= options['ui-framework'] %>': {
name: '<%= options['ui-framework'] %>',
test: module => /<%= options['ui-framework'] %>/g.test(module.context),
chunks: 'initial',
priority: 10
},
echarts: {
name: 'echarts',
test: module => /echarts/g.test(module.context),
chunks: 'initial',
priority: 10
}
}
}
}
},
// 扩展webpack配置
chainWebpack: config => {
// 移除 prefetch 插件
config.plugins.delete('prefetch')
config.plugins.delete('preload')
Object.keys(pages).forEach(page => {
config.plugins.delete(`preload-${page}`)
config.plugins.delete(`prefetch-${page}`)
})
config.resolve
.alias
.set('vue$', resolve('./node_modules/vue/dist/vue.common.js'))
.set('assets', resolve('src/assets'))
.set('components', resolve('src/components'))
.set('I18n', resolve('src/i18n'))
.set('View', resolve('src/view'))
.set('Lib', resolve('src/lib'))
.set('API', resolve('src/lib/services'))
// 添加 css 全局变量资源插件
const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
types.forEach(
type => addStyleResource(config.module.rule('<%= options.cssPreprocessor%>').oneOf(type))
)
},
// 开发服务器配置
devServer: {
port: `<%= options['Server Port'] %>`,
proxy: {
'/mock': {
target: 'https://easy-mock.com/mock/5ba83adde786c911a33a5090',
changeOrigin: true,
secure: false,
pathRewrite: {
'/mock': ''
}
}
}
},
// 样式
// css: {
// loaderOptions: {}
// }
// 插件配置
pluginOptions: {
}
}