forked from cklwblove/vue-cli3-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue.config.js
226 lines (210 loc) · 6.12 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict';
const path = require('path');
const pkg = require('./package');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const resolve = (dir) => {
return path.join(__dirname, './', dir);
};
const isProd = () => {
return process.env.NODE_ENV === 'production';
};
const genPlugins = () => {
const plugins = [];
if (isProd()) {
plugins.push(
new CompressionWebpackPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp(
'\\.(' +
['js', 'css'].join('|') +
')$'
),
threshold: 10240,
minRatio: 0.8,
cache: true
})
);
}
// HtmlWebpackIncludeAssetsPlugin
// 为静态资源文件添加 hash,防止缓存
plugins.push(
new HtmlWebpackIncludeAssetsPlugin({
assets: ['config.local.js'],
append: false,
hash: true
})
);
return plugins;
};
module.exports = {
/**
* You can set by yourself according to actual condition
* You will need to set this if you plan to deploy your site under a sub path,
* for example GitHub pages. If you plan to deploy your site to https://foo.github.io/bar/,
* then assetsPublicPath should be set to "/bar/".
* In most cases please use '/' !!!
* Detail https://cli.vuejs.org/config/#publicPath
*/
publicPath: './',
assetsDir: 'static',
lintOnSave: process.env.NODE_ENV !== 'production',
productionSourceMap: false,
// webpack-dev-server 相关配置
devServer: {
open: process.platform === 'darwin',
host: '0.0.0.0',
port: 3000,
https: false,
hotOnly: false,
overlay: {
warnings: false,
errors: true
}
},
// css相关配置
css: {
// 是否使用css分离插件 ExtractTextPlugin
extract: isProd() ? true : false,
// 开启 CSS source maps?
sourceMap: isProd() ? true : false,
// css预设器配置项
loaderOptions: {},
// 启用 CSS modules for all css / pre-processor files.
modules: false
},
configureWebpack: () => ({
name: `${pkg.name}`,
resolve: {
alias: {
'@': resolve('src'),
'@assets': resolve('src/assets'),
'@less': resolve('src/assets/less'),
'@js': resolve('src/assets/js'),
'@components': resolve('src/components'),
'@mixins': resolve('src/mixins'),
'@filters': resolve('src/filters'),
'@store': resolve('src/store'),
'@views': resolve('src/views'),
// 文件别名
'services': resolve('src/services'),
'variable': resolve('src/assets/less/variable.less'),
'utils': resolve('node_modules/@liwb/cloud-utils/dist/cloud-utils.esm'),
'mixins': resolve('node_modules/magicless/magicless.less')
}
},
plugins: genPlugins()
}),
// webpack配置
// see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
chainWebpack: (config) => {
// module
// svg
config
.module
.rule('svg')
.exclude.add(resolve('src/icons'))
.end();
config
.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end();
config
.when(process.env.NODE_ENV === 'development',
(config) => config.devtool('cheap-source-map')
);
// runtime.js 内联的形式嵌入
config
.plugin('preload')
.tap((args) => {
args[0].fileBlacklist.push(/runtime\./);
return args;
});
// plugin
// webpack-html-plugin
config
.plugin('html')
.tap((args) => {
args[0].minify = {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
};
return args;
});
// optimization
// https://imweb.io/topic/5b66dd601402769b60847149
config
.when(process.env.NODE_ENV === 'production',
(config) => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.use('script-ext-html-webpack-plugin', [{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}]);
config
.optimization
.splitChunks({
chunks: 'all',
cacheGroups: {
vendors: {
name: 'chunk-vendors',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // 只打包初始时依赖的第三方
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // 可自定义拓展你的规则
minChunks: 3, // 最小公用次数
priority: 5,
reuseExistingChunk: true
}
}
});
config.optimization.runtimeChunk('single');
}
);
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run analyz`
config
.when(process.env.IS_ANALYZ,
(config) => config
.plugin('webpack-bundle-analyzer')
.use(BundleAnalyzerPlugin, [{
analyzerPort: 8888,
generateStatsFile: false
}])
);
// `npm run build --generate_report`
config
.when(process.env.npm_config_generate_report,
(config) => config
.plugin('webpack-bundle-analyzer-report')
.use(BundleAnalyzerPlugin, [{
analyzerMode: 'static',
reportFilename: 'bundle-report.html',
openAnalyzer: false
}])
);
}
};