-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue.config.js
175 lines (157 loc) · 4.94 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
const path = require('path')
const resolve = dir => path.join(__dirname, dir);
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const webpack = require("webpack");
// cdn预加载使用
const externals = {
'vue': 'Vue',
'vue-router': 'VueRouter',
'vuex': 'Vuex',
'axios': 'axios',
'element-ui': 'ELEMENT',
'js-cookie': 'Cookies',
'nprogress': 'NProgress'
}
const cdn = {
// 开发环境
dev: {
css: [
"https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/theme-chalk/index.css",
'https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.min.css'
],
js: []
},
// 生产环境
build: {
css: [
'https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/theme-chalk/index.css',
'https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.min.css'
],
js: [
"https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js",
"https://cdn.bootcdn.net/ajax/libs/vue-router/3.4.0/vue-router.min.js",
"https://cdn.bootcdn.net/ajax/libs/vuex/3.5.1/vuex.min.js",
"https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js",
"https://cdn.bootcdn.net/ajax/libs/element-ui/2.13.2/index.js",
'https://cdn.bootcdn.net/ajax/libs/js-cookie/2.2.1/js.cookie.min.js',
'https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.min.js'
]
}
}
// 是否使用gzip
const productionGzip = true;
// 需要gzip压缩的文件后缀
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
module.exports = {
devServer: {
host:'0.0.0.0',
port:8000,
open: true,
proxy: {
'/api': {
target: 'http://api',
changeOrigin: true,
// ws: true, //如果要代理 websockets,配置这个参数
// secure: false, // 如果是https接口,需要配置这个参数
pathRewrite: {
'^/api': ''
}
},
'/lottie': {
target: 'https://assets6.lottiefiles.com',
changeOrigin: true,
pathRewrite: {
'^/lottie': ''
}
}
},
},
publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本
productionSourceMap: process.env.NODE_ENV!== 'production', // 生产环境的 source map
//用于放置生成的静态资源 (js、css、img、fonts) 的;(项目打包之后,静态资源会放在这个文件夹下)
assetsDir: "static",
outputDir:'dist',
css: {
loaderOptions: {
sass: {
additionalData: `@import "@/assets/scss/common.scss";`
}
}
},
chainWebpack: config => {
// 添加别名
config.resolve.alias
.set("vue$", "vue/dist/vue.esm.js")
.set("@", resolve("src"))
.set("@assets", resolve("src/assets"))
.set("@js", resolve("src/assets/js"))
.set("@scss", resolve("src/assets/scss"))
.set("@imgs", resolve("src/assets/imgs"))
.set("@components", resolve("src/components"))
.set("@views", resolve("src/views"))
.set("@router", resolve("src/router"))
.set("@store", resolve("src/store"))
.set("@api", resolve("src/api"))
.set("@utils", resolve("src/utils"));
/**
* 添加CDN参数到htmlWebpackPlugin配置中, 详见public/index.html 修改
*/
config.plugin('html').tap(args => {
if (process.env.NODE_ENV === 'production') {
args[0].cdn = cdn.build
}
if (process.env.NODE_ENV === 'development') {
args[0].cdn = cdn.dev
}
return args
})
//压缩图片
// 需要 npm i -D image-webpack-loader
// config.module
// .rule("images")
// .use("image-webpack-loader")
// .loader("image-webpack-loader")
// .options({
// mozjpeg: { progressive: true, quality: 65 },
// optipng: { enabled: false },
// pngquant: { quality: [0.65, 0.9], speed: 4 },
// gifsicle: { interlaced: false }
// // webp: { quality: 75 }
// });
return config;
},
// 修改webpack config, 使其不打包externals下的资源
configureWebpack: config => {
const plugins = [];
plugins.push(
new webpack.ProvidePlugin({
$:"jquery",
jQuery:"jquery",
"windows.jQuery":"jquery"
})
);
if (process.env.NODE_ENV === 'production') {
// 生产环境npm包转CDN
config.externals = externals;
productionGzip && plugins.push(
new CompressionWebpackPlugin({
filename: "[path].gz[query]",
algorithm: "gzip",
test: productionGzipExtensions,
threshold: 10240,
minRatio: 0.8
})
);
}
config.plugins = [...config.plugins, ...plugins];
if (process.env.NODE_ENV === 'development') {
/**
* 关闭host check,方便使用ngrok之类的内网转发工具
*/
config.devServer = {
disableHostCheck: true
}
}
}
}