-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue.config.js
133 lines (125 loc) · 3.52 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
const glob = require('glob')
const path = require('path')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css', 'html']
const isProduction = process.env.NODE_ENV === 'production'
function resolve (dir) {
return path.join(__dirname, dir)
}
let jsFileNames = []
function getEntry (globPath) {
let entries = {}
let tmp = []
let htmls = {}
// 页面名字唯一
// 获取/src/pages/**下所有的html文件
glob.sync(globPath + 'html').forEach((entry) => {
// ['.', 'src', 'pages', 'test', 'test.html'].splice(-3)
// ['pages', 'test', 'test.html']
tmp = entry.split('/').splice(-3)
let fileName = tmp[tmp.length - 1].split('.')[0]
htmls[fileName] = entry
})
// 获取/src/pages/**下所有的js文件
glob.sync(globPath + 'js').forEach((entry) => {
// console.log(entry)
tmp = entry.split('/').splice(-3)
let fileName = tmp[tmp.length - 1].split('.')[0]
jsFileNames.push(fileName)
entries[fileName] = {
entry,
template: htmls[fileName],
filename: fileName + '.html',
chunks: ['chunk-vendors', 'chunk-common', fileName]
}
})
// console.log(entries)
return entries
}
let htmls = getEntry('./src/pages/**/*.')
module.exports = {
pages: htmls,
publicPath: isProduction ? 'https://static.test.com/' : './',
outputDir: 'dist',
assetsDir: 'static',
devServer: {
port: 8987,
open: false,
index: '/test.html'
},
filenameHashing: true,
productionSourceMap: !isProduction,
chainWebpack: config => {
// console.log(jsFileNames)
// 删除 preload
jsFileNames.forEach(name => {
config.plugins.delete(`preload-${name}`)
})
// 路径别名
config.resolve.alias
.set('@', resolve('src'))
.set('assets', resolve('src/assets'))
.set('components', resolve('src/components'))
config.module
.rule('images')
.use('image-webpack-loader')
.loader('image-webpack-loader')
.options({
bypassOnDebug: true
})
.end()
config.optimization.splitChunks({
cacheGroups: {
vendors: {
name: 'chunk-vendors',
minChunks: jsFileNames.length,
test: /node_modules/,
priority: -10,
chunks: 'initial'
},
common: {}
}
})
},
configureWebpack: config => {
config.externals = {
'vue': 'Vue',
'vue-router': 'VueRouter',
'vuex': 'Vuex',
'axios': 'axios'
}
console.log(isProduction)
if (isProduction) {
// 压缩gzip
config.plugins.push(new CompressionPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
threshold: 10240, // 大于10K // 只有大小大于该值的资源会被处理 10240
minRatio: 0.8 // 只有压缩率小于这个值的资源才会被处理
// deleteOriginalAssets: true // 删除原文件
}))
// 代码压缩
config.plugins.push(new UglifyJsPlugin({
uglifyOptions: {
// 移出console.log
compress: {
// warnings: false, // 若打包错误,则注释这行
drop_console: true,
drop_debugger: false,
pure_funcs: ['console.log']
}
},
sourceMap: false,
parallel: true
}))
}
},
css: {
// CSS 提取至一个独立的 CSS 文件中
extract: true
},
pluginOptions: {
}
}