Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split @vue/cli 3 "pages" vendor file

Tags:

vue.js

vue-cli

i follow this topic Build the app in multi-page mode in Vue Cli doc, it working fine but i realize that there is only 1 vendor file instead of 2 (per page entry).

How can i split vendor file? i don't want to have one unnecessary big vendor file shared across many page entries. (i want to have it in single app).

enter image description here

enter image description here

For screenshot above, i want to have one vendor file for index.xxx.js and one for report.xxx.js

Please suggest, Thank you

like image 883
Phouvanh Kongchansavath Avatar asked Oct 17 '22 16:10

Phouvanh Kongchansavath


1 Answers

module.exports = {
  pages: {
    index: {
      entry: 'src/monitor/main.js',
      template: 'public/index.html',
      chunks: ['chunk-common', 'chunk-index-vendors', 'index']
    },
    report: {
      entry: 'src/report/main.js',
      chunks: ['chunk-common', 'chunk-report-vendors', 'report']
    }
  },

  chainWebpack: config => {
    const options = module.exports
    const pages = options.pages
    const pageKeys = Object.keys(pages)

    // Long-term caching

    const IS_VENDOR = /[\\/]node_modules[\\/]/

    config.optimization
      .splitChunks({
        cacheGroups: {
          ...pageKeys.map(key => ({
            name: `chunk-${key}-vendors`,
            priority: -11,
            chunks: chunk => chunk.name === key,
            test: IS_VENDOR,
            enforce: true,
          })),
          common: {
            name: 'chunk-common',
            priority: -20,
            chunks: 'initial',
            minChunks: 2,
            reuseExistingChunk: true,
            enforce: true,
          },
        },
      })
  }
}

It's based on the GitHub thread: https://github.com/vuejs/vue-cli/issues/2381#issuecomment-425038367

like image 105
buggy Avatar answered Oct 21 '22 05:10

buggy