Skip to content

Instantly share code, notes, and snippets.

@rvanzon
Last active September 9, 2021 17:22
Show Gist options
  • Save rvanzon/6028e884f6735c41125fb2d140143102 to your computer and use it in GitHub Desktop.
Save rvanzon/6028e884f6735c41125fb2d140143102 to your computer and use it in GitHub Desktop.
Nuxt.js: reduce the number of chunks

To reduce the number of chunks of your project build with Nuxt.js add the next lines:

  if (!this.dev) {
    config.plugins.push(new webpack.optimize.LimitChunkCountPlugin({
      maxChunks: 3        
    }))
  }

right under extend (config, ctx) { of nuxt.config.js. Then build your project with npm run build. Et voilà, No more than 3 chunks :-) Loading times of my pages went from 1.2s to 600ms!

Another thing to try and fiddle with:

new webpack.optimize.LimitChunkCountPlugin({
        maxChunks: 15
      }),
      new webpack.optimize.MinChunkSizePlugin({
        minChunkSize: 10000      
      })

Made 5 bundles (besides the vendor and nuxt)

@AllanOricil
Copy link

@Runnea thanks.
But I only needed one chunk. After studying webpack I found the way to fix it.

build: {
    extend(config, ctx) {
        config.output.filename = 'app.js',
        config.output.chunkFilename = '[id].js'; //without changing this, the error with one chunk happens when using maxChunks: 1
        config.optimization.splitChunks.cacheGroups.default = false;
        config.optimization.runtimeChunk = false;
    }
}

@OrkhanAlikhanov
Copy link

This reduced chunk count to 3 per page for me:

build: {
  extend(config, ctx) {
    if (ctx && ctx.isClient) {
      config.optimization.splitChunks.minChunks = 10
      config.optimization.splitChunks.minSize = 240000
    }
  },
}

@looiyk
Copy link

looiyk commented Sep 9, 2021

I just encountered this while working on my project. Somehow it is impossible for me to do it within one chunk. That is because I have codes in the middleware and also server side.

config.plugins.push(new webpack.optimize.LimitChunkCountPlugin({
      maxChunks: 2 // 1 for client and 1 for server
    }))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment