-
Notifications
You must be signed in to change notification settings - Fork 123
/
vue.config.js
144 lines (141 loc) · 4.2 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
let path = require("path");
//去console插件
const TerserPlugin = require("terser-webpack-plugin");
//gzip压缩插件
const CompressionWebpackPlugin = require("compression-webpack-plugin");
//打包分析
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const cdnPlugin = require("webpack-cdn-plugin");
function resolve(dir) {
return path.join(__dirname, dir);
}
//对一些不经常改动的库,可以通过cdn引入,webpack不对他们打包
let externals = {
vue: "Vue",
axios: "axios",
"element-ui": "ELEMENT",
"vue-router": "VueRouter",
vuex: "Vuex",
echarts: "echarts",
"vue2-editor": "VueEditor"
};
module.exports = {
//基本路径
publicPath: "/dist/",
//输出文件目录
outputDir: "dist",
//放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录。
assetsDir: "static",
//生产环境不需要生产map文件
productionSourceMap: false,
chainWebpack: config => {
//这里是对环境的配置,不同的环境对应不同的BASE_URL
config.plugin("define").tap(args => {
args[0]["process.env"].VUE_APP_LOGOUT_URL = JSON.stringify(
process.env.VUE_APP_LOGOUT_URL
);
console.log(args[0]);
return args;
});
//只在生产环境生效
if (process.env.VUE_APP_CURRENTMODE === "production") {
config.externals(externals);
config.optimization.minimize(true);
config.optimization.splitChunks({
chunks: "all",
cacheGroups: {
public: {
name: "public",
test: resolve("src/components"),
minSize: 0, //表示在压缩前的最小模块大小,默认值是 30kb
minChunks: 2, // 最小公用次数
priority: 5, // 优先级
reuseExistingChunk: true // 公共模块必开启
},
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10
}
}
});
}
//设置别名
config.resolve.alias
.set("@", resolve("src"))
.set("@api", resolve("src/api/api")) //接口地址
.set("@assets", resolve("src/assets"));
},
// webpack插件配置
configureWebpack: config => {
//生产环境插件
let pluginsPro = [
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true, // Must be set to true if using source-maps in production
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true
}
}
}),
new CompressionWebpackPlugin({
filename: "[path].gz[query]",
algorithm: "gzip",
test: new RegExp("\\.(" + ["js", "css"].join("|") + ")$"),
threshold: 10240,
minRatio: 0.8
})
// new BundleAnalyzerPlugin(),
],
//通用插件
plugins = [
new cdnPlugin({
modules: [
{ name: "vue", var: "Vue", path: "dist/vue.min.js" },
{ name: "axios", var: "axios", path: "dist/axios.min.js" },
{ name: "vuex", var: "Vuex", path: "dist/vuex.min.js" },
{
name: "element-ui",
var: "ELEMENT",
path: "lib/index.js",
style: "lib/theme-chalk/index.css"
},
{ name: "echarts", var: "echarts", path: "dist/echarts.min.js" },
{
name: "vue-router",
var: "VueRouter",
path: "dist/vue-router.min.js"
}
],
publicPath: "/node_modules"
})
];
if (process.env.VUE_APP_CURRENTMODE !== "development") {
config.plugins = [...config.plugins, ...pluginsPro];
}
config.plugins = [...config.plugins, ...plugins];
},
devServer: {
port: 8888, // 端口
open: true, // 自动开启浏览器
compress: false, // 开启压缩
overlay: {
warnings: true,
errors: true
}
},
//定义scss全局变量
css: {
// 是否使用css分离插件 ExtractTextPlugin
extract: true,
// 开启 CSS source maps?
sourceMap: false,
loaderOptions: {
sass: {
data: `@import "@/assets/scss/global.scss";`
}
}
}
};