This guide is a small follow-up to Code Splitting. If you have not yet read through that guide, please do so now.
Lazy, or "on demand", loading is a great way to optimize your site or application. This practice essentially involves splitting your code at logical breakpoints, and then loading it once the user has done something that requires, or will require, a new block of code. This speeds up the initial load of the application and lightens its overall weight as some blocks may never even be loaded.
Let's take the example from Code Splitting and tweak it a bit to demonstrate this concept even more. The code there does cause a separate chunk, lodash.bundle.js
, to be generated and technically "lazy-loads" it as soon as the script is run. The trouble is that no user interaction is required to load the bundle -- meaning that every time the page is loaded, the request will fire. This doesn't help us too much and will impact performance negatively.
Let's try something different. We'll add an interaction to log some text to the console when the user clicks a button. However, we'll wait to load that code (print.js
) until the interaction occurs for the first time. To do this we'll go back and rework the final Dynamic Imports example from Code Splitting and leave lodash
in the main chunk.
project
webpack-demo |- package.json |- webpack.config.js |- /dist |- /src |- index.js + |- print.js |- /node_modules
src/print.js
console.log('The print.js module has loaded! See the network tab in dev tools...'); export default () => { console.log('Button Clicked: Here\'s "some text"!'); };
src/index.js
+ import _ from 'lodash'; + - async function getComponent() { + function component() { var element = document.createElement('div'); - const _ = await import(/* webpackChunkName: "lodash" */ 'lodash'); + var button = document.createElement('button'); + var br = document.createElement('br'); + button.innerHTML = 'Click me and look at the console!'; element.innerHTML = _.join(['Hello', 'webpack'], ' '); + element.appendChild(br); + element.appendChild(button); + + // Note that because a network request is involved, some indication + // of loading would need to be shown in a production-level site/app. + button.onclick = e => import(/* webpackChunkName: "print" */ './print').then(module => { + var print = module.default; + + print(); + }); return element; } - getComponent().then(component => { - document.body.appendChild(component); - }); + document.body.appendChild(component());
Note that when usingimport()
on ES6 modules you must reference the.default
property as it's the actualmodule
object that will be returned when the promise is resolved.
Now let's run webpack and check out our new lazy-loading functionality:
Hash: e0f95cc0bda81c2a1340 Version: webpack 3.0.0 Time: 1378ms Asset Size Chunks Chunk Names print.bundle.js 417 bytes 0 [emitted] print index.bundle.js 548 kB 1 [emitted] [big] index index.html 189 bytes [emitted] [0] ./src/index.js 742 bytes {1} [built] [2] (webpack)/buildin/global.js 509 bytes {1} [built] [3] (webpack)/buildin/module.js 517 bytes {1} [built] [4] ./src/print.js 165 bytes {0} [built] + 1 hidden module Child html-webpack-plugin for "index.html": [2] (webpack)/buildin/global.js 509 bytes {0} [built] [3] (webpack)/buildin/module.js 517 bytes {0} [built] + 2 hidden modules
Many frameworks and libraries have their own recommendations on how this should be accomplished within their methodologies. Here are a few examples:
© JS Foundation and other contributors
Licensed under the Creative Commons Attribution License 4.0.
https://webpack.js.org/guides/lazy-loading