Skip to content

Commit

Permalink
fix: 修复多页面 cdn 引用
Browse files Browse the repository at this point in the history
  • Loading branch information
Aysnine committed Feb 17, 2020
1 parent e5a077c commit 0be4630
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -40,6 +48,7 @@ module.exports = {
}
}
},
pages,
configureWebpack: config => {
const configNew = {}
if (process.env.NODE_ENV === 'production') {
Expand All @@ -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
Expand Down

0 comments on commit 0be4630

Please sign in to comment.