-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvue.config.js
166 lines (158 loc) · 4.73 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
/**
* Author: chao_code520@163.com
* name: Aaron
*/
const path = require("path");
const GenerateAssetPlugin = require("generate-asset-webpack-plugin");
const cdnDependencies = require("./dependencies-cdn");
// 拼接路径
const resolve = dir => path.join(__dirname, dir);
process.env.VUE_APP_VERSION = require("./package.json").version;
const env = {
VUE_APP_API: process.env.VUE_APP_API
};
// 设置不参与构建的库
// let externals = {};
// cdnDependencies.forEach(package => {
// externals[package.name] = package.library;
// });
// 引入文件的 cdn 链接
const cdn = {
css: cdnDependencies.map(e => e.css).filter(e => e),
js: cdnDependencies.map(e => e.js).filter(e => e)
};
// 配置自动生成
/* eslint-disable no-new */
let createServerConfig = compilation => {
var parseEnv = Object.assign({ _hash: compilation.hash }, env); // process.env 环境配置项
Object.keys(parseEnv).forEach(function(key) {
parseEnv[key] = parseEnv[key].replace(/"/g, "");
});
return JSON.stringify(parseEnv, null, 2);
};
module.exports = {
publicPath: "./",
// 生产环境下source map, 可以将其设置为 false 以加速生产环境构建
productionSourceMap: false,
// 打包生成目录,不同的环境打不同包名
outputDir: "dist",
assetsDir: "static",
lintOnSave: process.env.NODE_ENV === "development",
// 开启less全局变量
css: {
loaderOptions: {
less: {
javascriptEnabled: true
}
},
extract: false
},
pluginOptions: {
"style-resources-loader": {
preProcessor: "less",
patterns: [
resolve("src/assets/css/common/_var.less"),
resolve("src/assets/css/common/_mixins.less")
] // 引入全局样式变量
}
},
devServer: {
open: true,
overlay: {
warnings: false,
errors: true
},
// detail: https://cli.vuejs.org/config/#devserver-proxy
proxy: {
"/api": {
target: process.env.VUE_APP_API,
changeOrigin: true,
pathRewrite: {
"^/api": ""
}
}
}
},
configureWebpack: {
// provide the app's title in webpack's name field, so that
// it can be accessed in index.html to inject the correct title.
name: process.env.VUE_APP_TITLE,
// externals: externals,
performance: {
hints: false
},
plugins: [
new GenerateAssetPlugin({
filename: "./public/config.json",
fn: (compilation, cb) => {
cb(null, createServerConfig(compilation));
},
extraFiles: []
})
],
// 配置解析别名
resolve: {
alias: {
"@base": resolve("src/components/Base")
}
}
},
chainWebpack: config => {
// detail: https://github.com/PanJiaChen/vue-element-admin/blob/master/vue.config.js
/**
* 添加 CDN 参数到 htmlWebpackPlugin 配置中
*/
config.plugin("html").tap(args => {
if (process.env.NODE_ENV === "production") {
args[0].cdn = cdn;
} else {
args[0].cdn = [];
}
return args;
});
/**
* 删除懒加载模块的 prefetch preload,降低带宽压力
* https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
* https://cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
* 而且预渲染时生成的 prefetch 标签是 modern 版本的,低版本浏览器是不需要的
*/
config.plugins.delete("prefetch").delete("preload");
// 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
config.resolve.symlinks(true);
// building config. webpack 4.0, 开发环境 sourcemap 不包含列信息
config.when(process.env.NODE_ENV === "development", config =>
config.devtool("cheap-source-map")
);
config.when(process.env.NODE_ENV === "production", config => {
config
.plugin("ScriptExtHtmlWebpackPlugin")
.after("html")
.use("script-ext-html-webpack-plugin", [
{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}
])
.end();
config.optimization.splitChunks({
chunks: "all",
cacheGroups: {
libs: {
name: "chunk-libs",
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: "initial" // only package third parties that are initially dependent, 只打包初始时依赖的第三方
},
commons: {
name: "chunk-commons",
test: resolve("src/components"), // can customize your rules
minChunks: 3, // minimum common number
priority: 5,
reuseExistingChunk: true
}
}
});
config.optimization.runtimeChunk("single");
});
}
};