diff --git a/vue.config.js b/vue.config.js index 1dfc8ace..a560c69a 100644 --- a/vue.config.js +++ b/vue.config.js @@ -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