-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvue.config.js
314 lines (311 loc) · 9.16 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
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
/**
* release
*/
// prod /data/dgiot/dgiot/lib/dgiot_api-4.3.0/priv/www
// local E:\work\code\dgiot\dgiot-dashboard\dist
/**
* @description vue.config.js全局配置
*/
const path = require('path')
const {
publicPath,
assetsDir,
outputDir,
lintOnSave,
transpileDependencies,
title,
abbreviation,
devPort,
providePlugin,
build7z,
buildGzip,
imageCompression,
webpackBanner,
webpackBarName,
systemStatic: localUrl,
Keywords,
Description,
dateTime,
proxy,
isPwa,
pwaConfig,
isSmp,
CDN_URL,
} = require('./src/config')
const { version, author } = require('./package.json')
const { ESBuildMinifyPlugin } = require('esbuild-loader')
const Webpack = require('webpack')
const WebpackBar = require('webpackbar')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
// const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const smp = new SpeedMeasurePlugin()
const productionGzipExtensions =
/\.(js|css|json|txt|html|ico|svg|ttf|woff|png|gif|woff|woff2|woff3)(\?.*)?$/i
process.env.VUE_APP_TITLE = title
process.env.VUE_APP_AUTHOR = author
process.env.VUE_APP_UPDATE_TIME = dateTime
process.env.VUE_APP_VERSION = version
process.env.VUE_APP_Keywords = Keywords
process.env.VUE_APP_Description = Description
process.env.VUE_APP_URL = proxy[0].target
process.env.proxy = proxy
process.env.VUE_APP_CDN_URL =
process.env.NODE_ENV === 'development' ? proxy[1].target + CDN_URL : CDN_URL
process.env.CDN_URL =
process.env.NODE_ENV === 'development' ? proxy[1].target + CDN_URL : CDN_URL
const staticUrl = process.env.CDN_URL
? `${process.env.CDN_URL}/assets/`
: '/assets/'
function getChainWebpack(config) {
// 使用 esbuild 编译 js 文件
const esrule = config.module.rule('js')
// 清理自带的 babel-loader
esrule.uses.clear()
esrule
.use('esbuild-loader')
.loader('esbuild-loader')
.options({
loader: 'ts', // 如果使用了 ts, 或者 vue 的 class 装饰器,则需要加上这个 option 配置, 否则会报错:ERROR: Unexpected "@"
target: 'es2015',
tsconfigRaw: require('./tsconfig.json'),
})
// // 删除底层 terser, 换用 esbuild-minimize-plugin
// config.optimization.minimizers.delete('terser')
// // 使用 esbuild 优化 css 压缩
// config.optimization
// .minimizer('esbuild')
// .use(ESBuildMinifyPlugin, [{ minify: true, css: true }])
config.plugins.delete('preload')
config.plugins.delete('prefetch')
config.plugins.delete('preload-index')
config.plugins.delete('prefetch-index')
config.plugin('html').tap((args) => {
var _staticUrl = localUrl
const { css, js } = _staticUrl
_staticUrl = {
css: [],
js: [],
}
css.forEach((_css) => {
_staticUrl.css.push(
`${staticUrl}css/${_css}?v=${process.env.VUE_APP_VERSION}&t=${dateTime}`
)
})
js.forEach((_js) => {
_staticUrl.js.push(
`${staticUrl}js/${_js}?v=${process.env.VUE_APP_VERSION}&t=${dateTime}`
)
})
args[0].staticUrl = _staticUrl
return args
})
config.resolve.symlinks(true)
config.module.rule('svg').exclude.add(resolve('src/icon'))
config.module
.rule('dgiotIcon')
.test(/\.svg$/)
.include.add(resolve('src/icon'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'dgiot-icon-[name]',
})
config.when(process.env.NODE_ENV === 'development', (config) => {
config.devtool('source-map')
})
config.when(process.env.NODE_ENV === 'production', (config) => {
if (process.env.CDN_URL) {
console.log(`当前时间${dateTime}`)
console.log(`当前使用了cdn,cdn资源链接地址为${process.env.CDN_URL}`)
} else console.log(`当前未使用cdn,可能会导致打包体积过大`)
config.performance.set('hints', false)
config.devtool('none')
config.optimization.splitChunks({
chunks: 'all',
minSize: 300000, //字节 引入的文件大于300kb才进行分割
maxSize: 500000, //500kb,尝试将大于500kb的文件拆分成n个500kb的文件
minChunks: 1, // 模块的最小被引用次数
maxAsyncRequests: 5, // 按需加载的最大并行请求数
maxInitialRequests: 3, // 一个入口最大并行请求数
automaticNameDelimiter: '-dgiot-', // 文件名的连接符
cacheGroups: {
vendors: {
name: 'vendors',
//自定义打包模块
test: /[\\/]node_modules[\\/]/,
priority: -10, //优先级,先打包到哪个组里面,值越大,优先级越高
// filename: 'vendors.js',
},
elementUI: {
name: 'element',
priority: 20,
test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
},
},
})
config
.plugin('banner')
.use(Webpack.BannerPlugin, [`${webpackBanner}${dateTime}`])
if (imageCompression) {
config.module
.rule('images')
.use('image-webpack-loader')
.loader('image-webpack-loader')
.options({
bypassOnDebug: true,
})
.end()
}
if (buildGzip) {
config.plugin('compression').use(CompressionWebpackPlugin, [
{
filename: '[path][base].gz', // 一个 {Function} (asset) => asset 函数,接收原资源名(通过 asset 选项)返回新资源名
algorithm: 'gzip', // 可以是 (buffer, cb) => cb(buffer) 或者是使用 zlib 里面的算法的 {String}
test: productionGzipExtensions,
threshold: 2048, //对1K以上的数据进行压缩
minRatio: 0.8, // 只有压缩率比这个值小的资源才会被处理
deleteOriginalAssets: false, //是否删除源文件
},
new Webpack.optimize.LimitChunkCountPlugin({
maxChunks: 5,
minChunkSize: 100,
}),
])
}
})
}
const resolve = (dir) => {
return path.join(__dirname, dir)
}
const proxyStatic = {}
const cssExport = {
requireModuleExtension: true,
sourceMap: true,
loaderOptions: {
scss: {
additionalData(content, loaderContext) {
const { resourcePath, rootContext } = loaderContext
const relativePath = path.relative(rootContext, resourcePath)
if (
relativePath.replace(/\\/g, '/') !==
'src/dgiot/styles/variables/variables.scss'
) {
return '@import "~@/dgiot/styles/variables/variables.scss";' + content
}
return content
},
},
},
extract: {
filename: `output/assets/css/[name].dgiot.css?v=${process.env.VUE_APP_VERSION}&t=${dateTime}`,
chunkFilename: `output/assets/css/[name].dgiot.css?v=${process.env.VUE_APP_VERSION}&t=${dateTime}`,
},
}
const configure = {
externals: {
'vue-qr': 'VueQr',
react: 'React',
'react-dom': 'ReactDOM',
bcrypt: 'bcrypt',
'be-full': 'BeFull',
JSONEditor: 'JSONEditor',
AMap: 'VueAMap',
konva: 'Konva',
VCharts: 'v-charts',
'vue-count-to': 'CountTo',
'vue-konva': 'VueKonva',
mockjs: 'Mock',
'js-md5': 'md5',
'js-base64': 'Base64',
$: 'jquery',
mqtt: 'mqtt',
'paho-mqtt': 'paho',
Sortable: 'Sortable',
vue: 'Vue',
vuex: 'Vuex',
'vue-router': 'VueRouter',
clipboard: 'clipboard',
'vue-i18n': 'VueI18n',
XLSX: 'xlsx',
colors: 'colors',
FileSaver: 'file-saver',
Multipane: 'vue-multipane',
'js-cookie': 'Cookies',
// 'vue-baidu-map': 'VueBaiduMap',
echarts: 'echarts',
screenfull: 'screenfull',
Qs: 'qs',
'ace-builds': 'ace',
qs: 'Qs',
moment: 'moment',
jsplumb: 'jsplumb',
JSEncrypt: 'jsencrypt',
CodeMirror: 'codemirror',
nprogress: 'NProgress',
'vue-codemirror': 'vueCodemirror',
vuedraggable: 'vueDraggable',
'element-china-area-data': 'elementChinaAreaData',
'vue-flv-player': 'vueFlvPlayer',
'vue-aliplayer-v2': 'VueAliplayerV2',
},
resolve: {
alias: {
'@': resolve('src'),
'*': resolve(''),
},
},
plugins: [
new CleanWebpackPlugin({ cleanStaleWebpackAssets: false }),
// new HardSourceWebpackPlugin(),
new Webpack.ProvidePlugin(providePlugin),
new WebpackBar({
name: webpackBarName,
}),
],
output: {
filename: `output/assets/js/[name].dgiot.js?v=${process.env.VUE_APP_VERSION}&t=${dateTime}`,
chunkFilename: `output/assets/js/[name].dgiot.js?v=${process.env.VUE_APP_VERSION}&t=${dateTime}`,
},
}
const pwa = isPwa ? pwaConfig : {}
proxy.forEach((url) => {
proxyStatic[`${url.path}`] = {
target: url.target,
ws: true,
changeOrigin: true,
pathRewrite: {
['^' + `${url.path}`]: '',
},
}
})
const devServer = {
hot: true,
port: devPort,
open: true,
noInfo: false,
overlay: {
warnings: true,
errors: true,
},
proxy: proxyStatic,
}
const configureWebpack = isSmp ? smp.wrap(configure) : configure
module.exports = {
publicPath,
assetsDir,
outputDir,
lintOnSave: process.env.NODE_ENV !== 'production',
transpileDependencies,
devServer,
pwa,
configureWebpack,
chainWebpack(config) {
return getChainWebpack(config)
},
runtimeCompiler: true,
productionSourceMap: false,
css: cssExport,
}