Skip to content

Instantly share code, notes, and snippets.

@rvanzon
Last active September 9, 2021 17:22
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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)

@besnikh
Copy link

besnikh commented Dec 24, 2017

Hi,
While using this code on nuxt I am getting an error stating my webpack is not defined, should I just import the webpack above or what should I do ?

Cheers

@sattvikc
Copy link

sattvikc commented Mar 29, 2018

const webpack = require('webpack')

@power-cut
Copy link

Error: [nuxt] Error while mounting app: TypeError: o.e is not a function

This is the error i am getting

@bwheatley
Copy link

Error: [nuxt] Error while mounting app: TypeError: o.e is not a function

This is the error i am getting

@maverickpravin got the same thing, what did you do to resolve the issue?

@simplenotezy
Copy link

This is how I done it, FYI, and it's working without issues:

	/*
	** You can extend webpack config here
	*/
	extend (config, ctx) {
		if (!this.dev) {
			config.plugins.push(
				new webpack.optimize.LimitChunkCountPlugin({
					maxChunks: 3
				}),
				new webpack.optimize.MinChunkSizePlugin({
					minChunkSize: 10000
				})
			);
		}
	}

@simplenotezy
Copy link

@rvanzon It no longer works, unfortunately, getting error:

ERROR in chunk runtime [entry]
Cannot convert undefined or null to object

ERROR in chunk runtime [entry]
[contenthash:7].js
Path variable [contenthash:7] not implemented in this context: [contenthash:7].js

@AllanOricil
Copy link

I have the same error as @simplenotezy

@Runnea
Copy link

Runnea commented Mar 18, 2021

@AllanOricil @simplenotezy

Here it is the solution:

webpack/webpack#12535

new webpack.optimize.LimitChunkCountPlugin({
					maxChunks: 4
				})

@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