fix: 修复多页面 cdn 引用

This commit is contained in:
CNine 2020-02-17 22:32:06 +08:00
parent e5a077ce98
commit 0be46301ab
1 changed files with 18 additions and 7 deletions

View File

@ -3,6 +3,7 @@ const VueFilenameInjector = require('@d2-projects/vue-filename-injector')
const ThemeColorReplacer = require('webpack-theme-color-replacer')
const forElementUI = require('webpack-theme-color-replacer/forElementUI')
const cdnDependencies = require('./dependencies-cdn')
const { chain, set, each } = require('lodash')
// 拼接路径
const resolve = dir => require('path').join(__dirname, dir)
@ -24,6 +25,13 @@ const cdn = {
js: cdnDependencies.map(e => e.js).filter(e => e)
}
// 多页配置,默认未开启,如需要请参考 https://cli.vuejs.org/zh/config/#pages
const pages = undefined
// const pages = {
// index: './src/main.js',
// subpage: './src/subpage.js'
// }
module.exports = {
// 根据你的实际情况更改这里
publicPath,
@ -40,6 +48,7 @@ module.exports = {
}
}
},
pages,
configureWebpack: config => {
const configNew = {}
if (process.env.NODE_ENV === 'production') {
@ -61,15 +70,17 @@ module.exports = {
chainWebpack: config => {
/**
* 添加 CDN 参数到 htmlWebpackPlugin 配置中
* 已适配多页
*/
config.plugin('html').tap(args => {
if (process.env.NODE_ENV === 'production') {
args[0].cdn = cdn
} else {
args[0].cdn = []
}
return args
const htmlPluginNames = chain(pages).keys().map(page => 'html-' + page).value()
const targetHtmlPluginNames = htmlPluginNames.length ? htmlPluginNames : ['html']
each(targetHtmlPluginNames, name => {
config.plugin(name).tap(options => {
set(options, '[0].cdn', process.env.NODE_ENV === 'production' ? cdn : [])
return options
})
})
/**
* 删除懒加载模块的 prefetch preload降低带宽压力
* https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch