-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue.config.js
173 lines (164 loc) · 4.77 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
const path = require('path')
const webpack = require('webpack')
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
const smp = new SpeedMeasurePlugin({
outputFormat: 'human',
})
const resolve = dir => {
return path.join(__dirname, dir)
}
// 自动配置多页参数
const glob = require('glob')
const pages = {}
// 配置选项
glob.sync('./src/pages/**/main.ts').forEach(path => {
const chunk = path.split('./src/pages/')[1].split('/main.ts')[0]
pages[chunk] = {
entry: path,
template: 'public/index.html',
chunks: ['chunk-vendors', 'chunk-common', chunk]
}
})
const externals = {
'vue': 'Vue',
'vue-router': 'VueRouter',
'vuex': 'Vuex',
'axios': 'axios',
'element-ui': 'ELEMENT',
}
// 是否使用gzip
const productionGzip = true
// 需要gzip压缩的文件后缀
const productionGzipExtensions = ['js', 'css']
module.exports = {
publicPath: '/',
outputDir: 'dist',
assetsDir: 'static',
// 构建多页
pages,
lintOnSave: process.env.NODE_ENV === 'development',
productionSourceMap: true,
devServer: {
hot: true,
port: 9531,
https: false,
hotOnly: false,
overlay: {
warnings: true,
errors: true
}
// 设置代理
// proxy: 'https://www.easy-mock.com'
},
configureWebpack: process.env.NODE_ENV !== 'development' ? () => smp.wrap({
plugins: [
new BundleAnalyzerPlugin(),
// 提取公用库
new webpack.DllReferencePlugin({
context: process.cwd(),
manifest: require('./public/vendor/vendor-manifest.json')
}),
// 将 dll 注入到 生成的 html 模板中
new AddAssetHtmlPlugin({
// dll文件位置
filepath: resolve('./public/vendor/*.js'),
// dll 引用路径
publicPath: './vendor',
// dll最终输出的目录
outputPath: './vendor'
})
],
optimization: {
splitChunks: {
cacheGroups: {
// echarts: {
// name: 'chunk-echarts',
// test: /[\\/]node_modules[\\/]echarts[\\/]/,
// chunks: 'all',
// priority: 10,
// reuseExistingChunk: true,
// enforce: true,
// },
demo: {
name: 'chunk-demo',
test: /[\\/]src[\\/]pages[\\/]demo[\\/]/,
chunks: 'all',
priority: 20,
reuseExistingChunk: true,
enforce: true,
},
page: {
name: 'chunk-page',
test: /[\\/]src[\\/]/,
chunks: 'all',
priority: 10,
reuseExistingChunk: true,
enforce: true,
},
vendors: {
name: 'chunk-vendors',
test: /[\\/]node_modules[\\/]/,
chunks: 'all',
priority: 5,
reuseExistingChunk: true,
enforce: true,
}
}
}
}
}) : {},
chainWebpack: config => {
// 配置路径别名
config.resolve.alias
.set('@', resolve('src'))
.set('assets', resolve('src/assets'))
.set('components', resolve('src/components'))
.set('config', resolve('src/config'))
.set('utils', resolve('src/assets'))
config.plugins.delete('named-chunks')
// svg loader
const svgRule = config.module.rule('svg') // 找到svg-loader
svgRule.uses.clear() // 清除已有的loader, 如果不这样做会添加在此loader之后
svgRule.exclude.add(/node_modules/) // 正则匹配排除node_modules目录
svgRule // 添加svg新的loader处理
.test(/\.svg$/)
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
// 修改images loader 添加svg处理
const imagesRule = config.module.rule('images')
imagesRule.exclude.add(resolve('src/icons'))
config.module
.rule('images')
.test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
// 生成环境
if (process.env.NODE_ENV !== 'development') {
// 忽略打包的文件
config.externals(externals)
// 图片压缩
config.module
.rule('images')
.test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
.use('img-loader')
.loader('img-loader').options({
plugins: [
require('imagemin-jpegtran')(),
require('imagemin-pngquant')({
quality: [0.75, 0.85]
})
]
})
// 启用GZip压缩
productionGzip && new CompressionWebpackPlugin({
test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
threshold: 8192,
minRatio: 0.8
})
}
}
}