vue.config.js
11.9 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* eslint-disable no-unused-vars, node/prefer-global/process */
// const HardPlugin = require('hard-source-webpack-plugin') // 使用缓存,优化启动速度
const SentryWebpackPlugin = require("@sentry/webpack-plugin");
const ThemeColorReplacer = require("webpack-theme-color-replacer");
const forElementUI = require("webpack-theme-color-replacer/forElementUI");
const VueFilenameInjector = require("./tools/vue-filename-injector");
// const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
const timeStamp = new Date().getTime();
// const AsyncRomveDir = require('./plugins/removeDir')
const os = require("os");
const CopyDir = require("./plugins/copyDir");
// const { defineConfig } = require('@vue/cli-service')
/**
* 获取当前机器的ip地址
*/
function getIpAddress() {
const ifaces = os.networkInterfaces();
for (const dev in ifaces) {
const iface = ifaces[dev];
for (let i = 0; i < iface.length; i++) {
const { family, address, internal } = iface[i];
if (family === "IPv4" && address !== "127.0.0.1" && !internal) {
return address;
}
}
}
}
// 拼接路径
const path = require("path");
const resolve = dir => path.join(__dirname, dir);
// 增加环境变量
process.env.VUE_APP_VERSION = require("./package.json").version;
process.env.VUE_APP_BUILD_TIME = require("dayjs")().format("YYYY-M-D HH:mm:ss");
const isDev = process.env.NODE_ENV !== "development";
/**
* 低内存构建:降低 Terser 并行、关闭体积告警噪音等(不改 productionSourceMap)
* - Jenkins/CI 常带 CI=true;或流水线显设 VUE_LOW_MEM_BUILD=1
* - 本地可:Windows `set VUE_LOW_MEM_BUILD=1&& npm run build`,Unix `VUE_LOW_MEM_BUILD=1 npm run build`
*/
function isLowMemBuild() {
if (
process.env.VUE_LOW_MEM_BUILD === "1"
|| process.env.VUE_LOW_MEM_BUILD === "true"
) {
return true;
}
const ci = process.env.CI;
return ci === "true" || ci === "1" || ci === "True" || ci === "TRUE";
}
// 基础路径 注意发布之前要先修改这里
const publicPath = "/dj-carlife-web/";
// const publicPath = process.env.VUE_APP_PUBLIC_PATH
// const publicPath = "/dj-carlife-web-wangzh/"
// const publicPath = "/dj-carlife-web-wangql/"
// const publicPath = '/'
// const CDN_DOMAIN = 'https://cdn.bootcss.com'
const CDN_DOMAIN = "https://img02.glsx.com.cn/dj-carlife-web";
const cdn = {
css: [
`${CDN_DOMAIN}/nprogress.min.css`,
// `${CDN_DOMAIN}/element-ui.min.css`,
],
js: [
// `${CDN_DOMAIN}/vue.min.js`,
`${CDN_DOMAIN}/vue-router.min.js`,
`${CDN_DOMAIN}/vuex.min.js`,
`${CDN_DOMAIN}/axios.min.js`,
`${CDN_DOMAIN}/js.cookie.min.js`,
`${CDN_DOMAIN}/nprogress.min.js`,
`${CDN_DOMAIN}/echarts.min.js`,
// `${CDN_DOMAIN}/lodash.min.js`,
`${CDN_DOMAIN}/qs.min.js`,
`${CDN_DOMAIN}/ua-parser.min.js`,
`${CDN_DOMAIN}/html2canvas.min.js`,
// `${CDN_DOMAIN}/element-ui.min.js`,
`${CDN_DOMAIN}/better-scroll.min.js`,
// `${CDN_DOMAIN}/better-scroll.min.js`,
// `https://cdn.bootcdn.net/ajax/libs/better-scroll/2.4.2/better-scroll.min.js`,
// `https://img01.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/better-scroll.min.js`,
`${CDN_DOMAIN}/dayjs.min.js`,
// `${CDN_DOMAIN}/vue-qr.js`,
`${CDN_DOMAIN}/xlsx.min.js`,
// `${CDN_DOMAIN}/flex.css`
],
};
module.exports = {
publicPath, // 根据你的实际情况更改这里
outputDir: "copyDirTarget",
/** 生产需要 sourcemap 排错/监控栈还原;配合 Webpack filesystem cache 减轻重复构建耗时 */
productionSourceMap: true,
// lintOnSave: true,
lintOnSave: false,
devServer: {
// webpack-dev-server v5: overlay 已迁到 client.overlay
// 推荐:保留编译错红屏,只关掉 throw 这类运行时错
client: {
// overlay: {
// errors: true,
// warnings: false,
// runtimeErrors: false,
// },
// 想完全静默就把上面注释掉,改成:
overlay: false,
},
// publicPath, // 和 publicPath 保持一致
// port: 8081
// proxy: {
// "/api": {
// // target: "http://192.168.3.102:9203/rop-dj-smartcarlife/router", // API服务器的地址
// target: "http://dj.glsx.com.cn/rop-dj-smartcarlife/router", // API服务器的地址
// changeOrigin: true,
// pathRewrite: {
// "^/api": ""
// }
// }
// },
// https: true,
// host: getIpAddress()
},
css: {
loaderOptions: {
// 设置 scss 公用变量文件
// require("autoprefixer")
sass: {
data: "@import '~@/assets/style/public.scss';",
},
},
},
configureWebpack: (config) => {
// Webpack 5 持久化缓存:二次构建(含开启 productionSourceMap)显著提速;CI 可缓存 node_modules/.cache
config.cache = {
type: "filesystem",
cacheDirectory: path.resolve(__dirname, ".webpack-cache"),
buildDependencies: {
config: [path.resolve(__dirname, "vue.config.js")],
},
};
// console.log(config, "config")
// debugger
config.output = {
...config.output,
// 把应用打包成umd库格式
library: "myLibrary",
// 输出重构 打包编译后的文件名称 【模块名称.时间戳】
filename: `[name].${timeStamp}.js`,
libraryTarget: "umd",
globalObject: "this",
};
if (isDev) {
// if (!isDev) {
config.externals = {
// vue: "Vue",
"vue-router": "VueRouter",
"vuex": "Vuex",
"axios": "axios",
"js-cookie": "Cookies",
"nprogress": "NProgress",
"echarts": "echarts",
"ua-parser-js": "UAParser",
"qs": "Qs",
"html2canvas": "html2canvas",
// "element-ui": "ELEMENT",
// "dayjs": "dayjs",
// "vue-qr": "vue-qr",
"xlsx": "XLSX",
// 与 CDN UMD 全局名一致(非 BScroll)
// "better-scroll": "BetterScroll",
"BScroll": "better-scroll",
// lodash: "_",
};
}
// html2canvas: 'html2canvas',
// 'element-ui': 'ELEMENT',
// 'better-scroll': 'BScroll',
// xlsx: 'xlsx',
// 'vue-qr': 'vue-qr',
// dayjs: 'dayjs',
// 'flex.css': 'flex.css'
// 非开发环境
// const Plugins = [
// new MiniCssExtractPlugin({
// // Options similar to the same options in webpackOptions.output
// // both options are optional
// filename: devMode ? 'css/[name].css' : 'css/[name].[hash].css',
// chunkFilename: devMode ? 'css/[id].css' : 'css/[id].[hash].css',
// })]
// config.plugins = [...config.plugins, ...Plugins]
const plugins = [];
// webpack5 不需要 已经内部提供改支持
// plugins.push(
// new HardPlugin({
// cacheDirectory: assets(__dirname, 'cache'),
// // 解决报错问题
// test: /mini-css-extract-plugin[\\/]dist[\\/]loader/
// })
// )
if (isDev) {
plugins.push(new CopyDir()); // 把dist 文件修改成有两份来兼容部署时请求目录不一样的情况
// new AsyncRomveDir() //不能移除默认输出文件夹因为打包出来的文件会提示超过默认值
// 解决方法是不启用这个这个插件或者吧默认值配置大
// new SpeedMeasurePlugin(),
// new AsyncRomveDir()
}
// 生产构建:上传 sourcemap 到 Sentry(与 main.js 中 Sentry.init 的 release 一致)
if (process.env.NODE_ENV === "production" && process.env.VUE_APP_SIMPLE_SENTRY_UPLOAD === "true") {
plugins.push(
new SentryWebpackPlugin({
release: process.env.VUE_APP_VERSION,
include: path.resolve(__dirname, "copyDirTarget"),
// 与默认 ext(js/map/…) 一致,忽略静态资源以减轻扫描与 IO(.map/.js 仍会上传)
ignore: [
"node_modules",
"vue.config.js",
"**/*.png",
"**/*.jpg",
"**/*.jpeg",
"**/*.gif",
"**/*.webp",
"**/*.svg",
"**/*.ico",
"**/*.woff",
"**/*.woff2",
"**/*.ttf",
"**/*.eot",
],
configFile: path.resolve(__dirname, ".sentryclirc"),
// 与部署路径一致,便于 Sentry 将 map 与线上资源对齐
urlPrefix: `~${publicPath}`,
}),
);
}
if (isLowMemBuild()) {
config.performance = {
...config.performance,
hints: false,
};
}
config.plugins = [...config.plugins, ...plugins];
},
// 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
chainWebpack: (config) => {
// 删除注释
config.optimization.minimizer("terser").tap((args) => {
if (isLowMemBuild()) {
args[0].parallel = 1;
// 不单独抽 license 注释文件,略降 Terser 侧内存与文件句柄
args[0].extractComments = false;
} else {
args[0].parallel = Math.max(1, os.cpus().length - 1);
}
args[0].terserOptions.compress.drop_console = true;
args[0].terserOptions.compress.drop_debugger = true;
args[0].terserOptions.compress.pure_funcs = ["console.log"];
args[0].terserOptions.output = {
comments: false,
};
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");
// config
// .plugin('webpack-bundle-analyzer')
// .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
// 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
config.resolve.symlinks(true);
config.plugin("theme-color-replacer").use(ThemeColorReplacer, [
{
fileName: `css/theme-colors.[contenthash:8].css`,
// fileName: `${publicPath}` + 'css/theme-colors-[contenthash:8].css',
resolveCss(resultCss) {
// console.log(resultCss)
// console.log(`${publicPath}` + 'css/theme-colors-[contenthash:8].css')
// optional. Resolve result css code as you wish.
return resultCss.replace(
/process.env.VUE_APP_ELEMENT_COLOR/g,
process.env.VUE_APP_ELEMENT_COLOR,
);
},
matchColors: [
...forElementUI.getElementUISeries(
process.env.VUE_APP_ELEMENT_COLOR,
), // Element-ui主色系列
],
externalCssFiles: [
"./node_modules/element-ui/lib/theme-chalk/index.css",
], // optional, String or string array. Set external css files (such as cdn css) to extract colors.
changeSelector: forElementUI.changeSelector,
},
]);
config
// 开发环境
.when(
process.env.NODE_ENV === "development",
// sourcemap不包含列信息
config => config.devtool("cheap-source-map"),
)
// TRAVIS 构建 vue-loader 添加 filename
.when(
process.env.VUE_APP_BUILD_MODE === "TRAVIS"
|| process.env.NODE_ENV === "development",
VueFilenameInjector(config, {
propName: process.env.VUE_APP_SOURCE_VIEWER_PROP_NAME,
}),
);
// svg
const svgRule = config.module.rule("svg");
svgRule.uses.clear();
svgRule.include
.add(resolve("src/assets/svg-icons/icons"))
.end()
.use("svg-sprite-loader")
.loader("svg-sprite-loader")
.options({
symbolId: "d2-[name]",
})
.end();
// image exclude
const imagesRule = config.module.rule("images");
imagesRule
.test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
.exclude
.add(resolve("src/assets/svg-icons/icons"))
.end();
// 重新设置 alias
config.resolve.alias
.set("@", resolve("src"))
.set("@api", resolve("src/api"));
if (isDev) {
// if (!isDev) {
config.plugin("html").tap((args) => {
// args[0].minify.removeComments = false;
args[0].cdn = cdn;
return args;
});
}
},
// i18n
pluginOptions: {
i18n: {
locale: "zh-chs",
fallbackLocale: "en",
localeDir: "locales",
enableInSFC: true,
},
},
pwa: {
iconPaths: {
favicon32: `${publicPath}favicon.ico`,
favicon16: `${publicPath}favicon.ico`,
appleTouchIcon: `${publicPath}favicon.ico`,
maskIcon: `${publicPath}favicon.ico`,
msTileImage: `${publicPath}favicon.ico`,
},
// iconPaths: {
// favicon32: `${publicPath}favicon.ico` favicon.ico',
// favicon16: 'favicon.ico',
// appleTouchIcon: 'favicon.ico',
// maskIcon: 'favicon.ico',
// msTileImage: 'favicon.ico',
// },
},
};