Back to articles
January, 16th 2018

Disabling code-splitting in Preact CLI default template

Some small and light projects do not need code splitting at all. Moreover sometimes code-splitting may lead to unwanted behavior on low speed connection or no connection at all.

Since splitting code in chunks is often used for splitting app routes that are not a part of the main app shell, without connection the page won't load if the chunk wasn't loaded and cached before. User will get old page without any changes or white screen.

That's why for some small apps I recommend disabling code-splitting. In preact-cli template it can be done by creating preact.config.js in the root of our project:

/* js */
/**
* Function that mutates original webpack config.
 * Supports asynchronous changes when promise is returned.
 *
 * @param {object} config - original webpack config.
 * @param {object} env - options passed to CLI.
 * @param {WebpackConfigHelpers} helpers - object with useful helpers when working with config.
 **/

export default (config, env, helpers) => {

};

Then, we need to use webpack LimitChunkCountPlugin. By passing configuration object we can restrict a number of dynamic chunks created on build.

/* js */
/**
* Function that mutates original webpack config.
 * Supports asynchronous changes when promise is returned.
 *
 * @param {object} config - original webpack config.
 * @param {object} env - options passed to CLI.
 * @param {WebpackConfigHelpers} helpers - object with useful helpers when working with config.
 **/

export default (config, env, helpers) => {
  config.plugins.push(
    new helpers.webpack.optimize.LimitChunkCountPlugin({
      maxChunks: 1
    })
  );
};

In this example maxChunks restricts our build to only one bundle.js file.


Other Posts
July, 28th 2022 Background-aware swiper pagination Some parts of user interface are not background aware, however are meant to be. It’s especially noticeable when working with user-generated content that is different in colors, exposure and size. Take..
March, 31st 2022 Tree-shaking Vuex State in Nuxt Document response The way Nuxt delivers state data from server to client can play a bad joke with your loading performance. All of your Vuex state data on the server will be printed in the response Document as JS objec..