WebPack 懒加载
WebPack 懒加载
懒加载或者按需加载,是一种很好的优化网页或应用的方式。这种方式实际上是先把你的在一些逻辑断点处分离开,然后在一些块中完成某些操作后,立即引用或即将引用另外一些新的块。这样加快了应用的初始加载速度,减轻了它的总体体积,因为某些块可能永远不会被加载。
示例
我们在分离中的例子基础上,进一步做些调整来说明这个概念。那里的确实会在脚本运行的时候产生分离的块 lodash.bundle.js ,在技术概念上“懒加载”它。问题是加载这个包并不需要的交互 -- 意思是每次加载的时候都会请求它。这样做并没有对我们有很多帮助,还会对产生负面影响。
我们试试不同的做法。我们交互,当点击按钮的时候用 console 打印一些。但是会等到第一次交互的时候再加载那个块(print.js)。为此,我们返回到分离的例子中,把 lodash 放到主块中,重新运行分离中的 final Dynamic Imports example。
project
webpack-demo |- package.json |- webpack.con.js |- /dist |- /src |- index.js + |- print.js |- /node_modules@H__13@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"!'); }@H__13@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());@H__13@注意当 ES6 模块的 import() (引入模块)时,必须指向模块的 .default 值,因为它才是 promise 被处理后返回的实际的 module 对象。
现在运行 webpack 来验证一下我们的懒加载:
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@H__13@框架
许多框架和类库对于如何用它们自己的方式来实现(懒加载)都有自己的建议。这里有一些例子:
React:
Vue:
AngularJS: by
进一步阅读
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"!'); }@H__13@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());@H__13@注意当 ES6 模块的 import() (引入模块)时,必须指向模块的 .default 值,因为它才是 promise 被处理后返回的实际的 module 对象。
现在运行 webpack 来验证一下我们的懒加载:
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@H__13@框架
许多框架和类库对于如何用它们自己的方式来实现(懒加载)都有自己的建议。这里有一些例子:
React:
Vue:
AngularJS: by
进一步阅读
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());@H__13@注意当 ES6 模块的 import() (引入模块)时,必须指向模块的 .default 值,因为它才是 promise 被处理后返回的实际的 module 对象。
现在运行 webpack 来验证一下我们的懒加载:
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@H__13@框架
许多框架和类库对于如何用它们自己的方式来实现(懒加载)都有自己的建议。这里有一些例子:
React:
Vue:
AngularJS: by
进一步阅读
注意当 ES6 模块的 import() (引入模块)时,必须指向模块的 .default 值,因为它才是 promise 被处理后返回的实际的 module 对象。
现在运行 webpack 来验证一下我们的懒加载:
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@H__13@框架
许多框架和类库对于如何用它们自己的方式来实现(懒加载)都有自己的建议。这里有一些例子:
React:
Vue:
AngularJS: by
进一步阅读
许多框架和类库对于如何用它们自己的方式来实现(懒加载)都有自己的建议。这里有一些例子:
React:
Vue:
AngularJS: by