Code Splitting?

Code Splitting is an efficient way to reduce your bundle size: it speeds up the loading of your application and reduces the payload size of your application.

Bundling is great, but as your app grows, your bundle will grow too. Especially if you are including large third-party libraries. You need to keep an eye on the code you are including in your bundle so that you don’t accidentally make it so large that your app takes a long time to load.

To avoid winding up with a large bundle, it’s good to get ahead of the problem and start “splitting” your bundle. Code-Splitting is a feature supported by bundlers like Webpack and Browserify (via factor-bundle) which can create multiple bundles that can be dynamically loaded at runtime.

Code-splitting your app can help you “lazy-load” just the things that are currently needed by the user, which can dramatically improve the performance of your app. While you haven’t reduced the overall amount of code in your app, you’ve avoided loading code that the user may never need, and reduced the amount of code needed during the initial load.

import()

The best way to introduce code-splitting into your app is through the dynamic import() syntax.

Before:

import { add } from './math'
console.log(add(16, 26))

After:

import('./math').then(math => {
console.log(math.add(16, 26))
})

Note: The dynamic import() syntax is a ECMAScript (JavaScript) proposal not currently part of the language standard. It is expected to be accepted in the near future.

When Webpack comes across this syntax, it automatically starts code-splitting your app.

If you’re setting up Webpack yourself, you’ll probably want to read Webpack’s guide on code splitting.

When using Babel, you’ll need to make sure that Babel can parse the dynamic import syntax but is not transforming it. For that you will need @babel/plugin-syntax-dynamic-import.

Named import()

Weckpack's default behaviour, is to name them as x.js where x is an incremental number depending on how many dynamic chunks you are importing in your code.

This will give a very poorly view of which file is loading what code.

To fix that, webpack introduced magic comments, with which a chunck can be named as follows (ie: math.js).

import(/* webpackChunkName: "math" */ './math').then(math => {
console.log(math.add(16, 26))
})

NOTE: When using Server Side Rendering, make sure comment and file path are exacly in the same order as above.

Code Splitting + React

React supports code splitting out of the box with React.lazy. However it has some limitations, this is why @loadable/component exists.

In a React application, most of the time you want to split your components. Splitting a component implies to be able to wait for this component to be loaded (showing a fallback during loading) but also to handle errors.

Example of component splitting:

import loadable from '@loadable/component'
const OtherComponent = loadable(() => import('./OtherComponent'))
function MyComponent() {
return (
<div>
<OtherComponent />
</div>
)
}