vue.config.js
10.3 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
const VueFilenameInjector = require("./tools/vue-filename-injector")
const ThemeColorReplacer = require("webpack-theme-color-replacer")
const forElementUI = require("webpack-theme-color-replacer/forElementUI")
// const HardPlugin = require('hard-source-webpack-plugin') // 使用缓存,优化启动速度
const SentryWebpackPlugin = require("@sentry/webpack-plugin")
// const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
const timeStamp = new Date().getTime()
const CopyDir = require("./plugins/copyDir")
// const AsyncRomveDir = require('./plugins/removeDir')
const os = require("os")
// const { defineConfig } = require('@vue/cli-service')
/**
* 获取当前机器的ip地址
*/
function getIpAddress() {
var ifaces = os.networkInterfaces()
for (var 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 resolve = (dir) => require("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"
// 基础路径 注意发布之前要先修改这里
// const publicPath = '/dj-carlife-web/'
const publicPath = process.env.VUE_APP_PUBLIC_PATH
// 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",
// productionSourceMap: false,
// lintOnSave: true,
lintOnSave: false,
devServer: {
// publicPath, // 和 publicPath 保持一致
// port: 8082,
// 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) => {
// 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",
BScroll: "better-scroll"
// lodash: "_",
// "better-scroll": "BScroll"
}
}
// 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()
}
config.plugins = [...config.plugins, ...plugins]
// sentry 配置
process.env.NODE_ENV === "production" &&
Object.assign(config, {
plugins: [
...config.plugins
// new SentryWebpackPlugin({
// // webpack specific configuration
// release: "0.0.1",
// include: "./dist", // 需要上传到sentry服务器的资源目录,会自动匹配js 以及map文件
// ignore: ["node_modules", "vue.config.js"], // 忽略文件目录,当然我们在inlcude中制定了文件路径,这个忽略目录可以不加
// configFile: ".sentryclirc",
// urlPrefix: "~/dj-carlife-web/"
// })
]
})
},
// 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
chainWebpack: (config) => {
// 删除注释
config.optimization.minimizer("terser").tap((args) => {
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',
// },
}
}