Skip to content

Experimenting with Webpack 2 code splitting to see how it can work in the context of a universal app

Notifications You must be signed in to change notification settings

jamesfiltness/webpack-2-code-splitting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Weback 2 code splitting with import().

An experiment to see how Webpack's import() module method can be used to split code and how that can affect the bundle size.

Running npm start initialises an isomorphic React app at localhost:3000 (and optionally the Webpack Bundle Analyzer at localhost:8888 - needs to be enabled in webpack config).

Default bundle without any optimisation da2ef00

default-without-optimisation

Basic code splitting using import() c696a0f

The two split bundles combined size is actually larger than the original. See src/universal/app.js for an example of code splitting. The SomeOtherComponent bundle is only requested when the user clicks on the button.

basic-code-split

Common chunks extracted: 845a9e3

Using the CommonsChunkPlugin to extract libraries in to a separate chunk. Starting to see some improvements in bundle sizes:

commons-chunk-plugin

Partial imports

Using the WebpackBundleAnalyzer showed that SomeOtherComponent bundle size was mainly comprised of lodash. Reduce the size of this by only importing what we need from lodash 27ea766

reduce-lodash-footprint

Other improvements on bundle size using common Webpack techniques:

  • Uglification 4fc6cf7

  • Build for production environment 293a509

  • Gzip bundles using Webpack's Compression plugin 39a0c45. Further reducers file sizes but requires the server to be configured to serve gzipped assets. I've used NGINX for this.

(Currently it seems there is an issue with the compression plugin not producing compressed versions of bundles that are dynamically imported. There is an open issue on github here: webpack-contrib/compression-webpack-plugin#79)

Final bundle sizes

final

In a real-world app there are other techniques that can be used to further reduce bundle size. They don't really make any difference in the context of this tiny example app:

About

Experimenting with Webpack 2 code splitting to see how it can work in the context of a universal app

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published