Gatsby: How to disable split chunk in build

Created on 7 May 2019  路  5Comments  路  Source: gatsbyjs/gatsby

How to disable code splitting. I tried but unable to find suitable settings.

question or discussion

Most helpful comment

hi @nsisodiya, would this work for your needs?

exports.onCreateWebpackConfig = () => {
  actions.setWebpackConfig({
    optimization: {
      splitChunks: false,
    },
  });
};

From what I can tell (I _think_) Gatsby creates an entry file for each of your pages, which will create additional JS files in addition to app.js. This is a separate Webpack step from chunking. So if you have more than one page in your src/pages directory, you will end up with more than one js file even with this setting on. If you keep just one index.js file in src/pages, and use the snippet above, this will get you close to one file for your app.

All 5 comments

I tried this one.

gatsby-node.js

const webpack = require(`webpack`);

exports.onCreateWebpackConfig = ({ stage, actions }, options) => {
  console.log('==== Disabling Chunk ====');
  if ((process.env.NODE_ENV === 'production' && stage === 'build-javascript') || options.development) {
    actions.setWebpackConfig({
      plugins: [
        new webpack.optimize.LimitChunkCountPlugin({
          maxChunks: 1
        })
      ]
    });
  }
};

hi @nsisodiya, would this work for your needs?

exports.onCreateWebpackConfig = () => {
  actions.setWebpackConfig({
    optimization: {
      splitChunks: false,
    },
  });
};

From what I can tell (I _think_) Gatsby creates an entry file for each of your pages, which will create additional JS files in addition to app.js. This is a separate Webpack step from chunking. So if you have more than one page in your src/pages directory, you will end up with more than one js file even with this setting on. If you keep just one index.js file in src/pages, and use the snippet above, this will get you close to one file for your app.

@trevorlitsey - I tried, but it didn't worked.

As mentioned in your other issue we don't recommend changing this behavior of Gatsby. We work hard on making Gatsby as fast as possible and this would decrease your speed. Hence I'm closing this, thanks for the issue!

@LekoArts what about actually "enhancing" the gatsby config? Is this not recommended when gatsby config does not totally suit us?

For example see this https://github.com/gatsbyjs/gatsby/issues/10965#issuecomment-453456324

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dustinhorton picture dustinhorton  路  3Comments

ferMartz picture ferMartz  路  3Comments

totsteps picture totsteps  路  3Comments

dustinhorton picture dustinhorton  路  3Comments

signalwerk picture signalwerk  路  3Comments