-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvue.config.js
185 lines (173 loc) · 6.97 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
"use strict"
const path = require("path");
const utils = require("./build/utils.js");
let pages = utils.getEntrys();
let is_product = utils.is_product;
function resolve(dir) {
return path.join(__dirname, dir)
};
module.exports = {
publicPath : is_product ? '/web/' : '/',
outputDir : is_product ? resolve('/dist/web/') : 'dist',
// 多页面multi-page模式下构建应用,每个page应该有一个对应的 JavaScript 入口文件。
// 这里使用了动态配置pages的方法,减少重复代码的书写,统一配置。
pages,
//生产环境JS文件中移除sourceMap
productionSourceMap: false,
configureWebpack : config => {
if (is_product) {
//生产环境移除console语句
config.optimization.minimizer[0].options.terserOptions.compress.warnings = false;
config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
config.optimization.minimizer[0].options.terserOptions.compress.drop_debugger = true;
config.optimization.minimizer[0].options.terserOptions.compress.pure_funcs = ['console.log'];
}
},
chainWebpack : config => {
//【1】设置别名
config.resolve.alias.set('@', resolve('src')).set('common', resolve('src/common')).set('components', resolve('src/components')).set('assets', resolve('src/assets'));
//【2】设置扩展,主要是为了cdn 的引入
config.externals({
'vue' : 'Vue',
'vue-router' : 'VueRouter',
'axios' : 'axios',
'element-plus': 'ElementPlus',
'vuex' : 'Vuex',
});
//【3】设置pug的使用
config.module.rules.delete('pug');
config.module.rule('pug').test(/\.pug$/)
// this applies to <template lang="pug"> in Vue components
.oneOf('vue-loader').resourceQuery(/^\?vue/).use('pug-plain').loader(require.resolve('pug-plain-loader')).end().end()
// this applies to pug imports inside JavaScript, i.e. .pug files
.oneOf('pug-files').uses.delete('pug-plain-loader').end().use('pug-loader').loader('pug-loader').end()
//【4】设置image-webpack-loader
config.module.rule("images").use("image-webpack-loader").loader("image-webpack-loader").options({
mozjpeg : {
progressive: true,
quality : 65
},
// optipng.enabled: false will disable optipng
optipng : {
enabled: false,
},
pngquant: {
quality: [0.65, 0.90],
speed : 4
},
gifsicle: {
interlaced: false,
},
}).end();
//【5】删除无效的config.html
config.plugins.delete('html-config');
//【6】移除预加载配置
for (var attr in pages) {
var delete_pre_name = 'prefetch-' + attr;
var delete_pre_load_name = 'preload-' + attr;
config.plugins.delete(delete_pre_name)
config.plugins.delete(delete_pre_load_name)
}
;
//【7】代码压缩及分割
config.optimization.minimize = true;
config.optimization.splitChunks({
maxAsyncRequests : Infinity,
maxInitialRequests: Infinity,
cacheGroups : {
default: false,
//【1】提取(api/config/script)配置
env : {
chunks : function (chunk) {
return chunk.name != 'config'
},
test : /[\\/]src[\\/](api|config|script)[\\/]/,
priority: 30,
name : `env`,
},
//【2】提取PC端入口(/router/store)配置
entry : {
chunks : 'initial',
test : /[\\/]src[\\/](router|store)[\\/]index[\\.]js/,
priority: 29,
name : `entry`,
enforce : true,
},
//【3】vendors
vendors: {
chunks : 'all',
test : /[\\/]node_modules[\\/]/,
name : `vendors`,
priority: 27,
},
//【4】common
common : {
chunks : 'all',
name : `common`,
minChunks : 2,
enforce : true,
priority : 21,
reuseExistingChunk: true
},
}
});
//【8】设置编辑后未改变的chunks 保持MD5不变
config.optimization.runtimeChunk({
name: (entrypoint) => {
return 'runtime'
}
});
//【9】生产模式下复制静态文件及.htaccess的配置
if (is_product) {
config.plugin('copy').use(require('copy-webpack-plugin'), [
[{
from : path.resolve(__dirname, './public'),
to : './',
ignore: ['.*', 'images/*.*', 'lib/**/*.*','cloud-config.js']
},{
from: path.resolve(__dirname, './.htaccess'),
to : '../'
},{
from: path.resolve(__dirname, './public/cloud-config.js'),
to : '../'
}]
])
}
;
},
css : {
loaderOptions: {
// 给 sass-loader 传递选项
sass: {
// @/ 是 src/ 的别名
// 所以这里假设你有 `src/variables.sass` 这个文件
// 注意:在 sass-loader v8 中,这个选项名是 "prependData"
prependData: `@import "~@/style/common/variable.scss"`
},
// 默认情况下 `sass` 选项会同时对 `sass` 和 `scss` 语法同时生效
// 因为 `scss` 语法在内部也是由 sass-loader 处理的
// 但是在配置 `prependData` 选项的时候
// `scss` 语法会要求语句结尾必须有分号,`sass` 则要求必须没有分号
// 在这种情况下,我们可以使用 `scss` 选项,对 `scss` 语法进行单独配置
scss: {
prependData: `@import "~@/style/common/variable.scss";`
},
}
},
lintOnSave : false,
// 服务器配置
devServer : {
clientLogLevel : 'warning',
historyApiFallback: {
//hacfin 路由配置
index : '/html/treeselect.html',
rewrites: [
]
},
port : 3001,
overlay : {
warnings: false,
errors : true
}
},
}