Skip to content

Commit 0be4630

Browse files
committed
fix: 修复多页面 cdn 引用
1 parent e5a077c commit 0be4630

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

vue.config.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const VueFilenameInjector = require('@d2-projects/vue-filename-injector')
33
const ThemeColorReplacer = require('webpack-theme-color-replacer')
44
const forElementUI = require('webpack-theme-color-replacer/forElementUI')
55
const cdnDependencies = require('./dependencies-cdn')
6+
const { chain, set, each } = require('lodash')
67

78
// 拼接路径
89
const resolve = dir => require('path').join(__dirname, dir)
@@ -24,6 +25,13 @@ const cdn = {
2425
js: cdnDependencies.map(e => e.js).filter(e => e)
2526
}
2627

28+
// 多页配置,默认未开启,如需要请参考 https://cli.vuejs.org/zh/config/#pages
29+
const pages = undefined
30+
// const pages = {
31+
// index: './src/main.js',
32+
// subpage: './src/subpage.js'
33+
// }
34+
2735
module.exports = {
2836
// 根据你的实际情况更改这里
2937
publicPath,
@@ -40,6 +48,7 @@ module.exports = {
4048
}
4149
}
4250
},
51+
pages,
4352
configureWebpack: config => {
4453
const configNew = {}
4554
if (process.env.NODE_ENV === 'production') {
@@ -61,15 +70,17 @@ module.exports = {
6170
chainWebpack: config => {
6271
/**
6372
* 添加 CDN 参数到 htmlWebpackPlugin 配置中
73+
* 已适配多页
6474
*/
65-
config.plugin('html').tap(args => {
66-
if (process.env.NODE_ENV === 'production') {
67-
args[0].cdn = cdn
68-
} else {
69-
args[0].cdn = []
70-
}
71-
return args
75+
const htmlPluginNames = chain(pages).keys().map(page => 'html-' + page).value()
76+
const targetHtmlPluginNames = htmlPluginNames.length ? htmlPluginNames : ['html']
77+
each(targetHtmlPluginNames, name => {
78+
config.plugin(name).tap(options => {
79+
set(options, '[0].cdn', process.env.NODE_ENV === 'production' ? cdn : [])
80+
return options
81+
})
7282
})
83+
7384
/**
7485
* 删除懒加载模块的 prefetch preload,降低带宽压力
7586
* https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch

0 commit comments

Comments
 (0)