webpack学习系列-lazy-loading (https://webpack.js.org/guides/lazy-loading/)

本文介绍了一种提高网站或应用性能的方法——懒加载。通过按需加载代码块,可以显著提升首屏加载速度并减少不必要的资源加载。文章通过一个具体示例展示了如何实现点击按钮时才加载特定脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Lazy Loading

懒加载,或者说按需加载,是优化网站或者应用的一个非常好的方式。这个实践本质上是将代码在逻辑点切割,当用户做了某些点击或者互动后,将需要的代码或者未来会需要的代码加载。这样做会加快应用的首屏加载,减少整个应用的资源加载量,因为一些代码块可能永远不会被加载。

 

Example

 

project

上节(代码分割)的例子将生成的js文件一分为二,只要引入的js文件运行了,lodash.bundle.js就会被加载,这种技术对我们并没有太大帮助,反而会对性能产生负面影响。下面我们做一些更改,只有用户点击按钮,才会请求print.js。例子如下:

import _ from 'lodash';
function component() {
    var element = document.createElement('div');
    var btn = document.createElement('button');
    element.innerHTML = _.join(['Hello', 'webpack'], ' ');
    btn.innerHTML = 'Click me and check the console!';
    element.appendChild(btn);
    btn.onclick = e => import('./print').then(module => {    //lazy load核心,只有点击按钮时,才会加载print.js文件
        var print = module.default;
        print(); 
    })
    return element;
}
document.body.appendChild(component());

以上代码的构建结果如下图:

打开index.html,会发现,页面打开时,只会加载app1.bundle.js文件,当点击页面上的按钮时,会加载0.bundle.js文件,进而控制台打印出消息。

PS E:\soft\finnal\yuanma2\crm1> npm run build:portable > crm1@0.1.0 build:portable > vue-cli-service build --modern && electron-builder --win portable Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme All browser targets in the browserslist configuration have supported ES module. Therefore we don't build two separate bundles for differential loading. ⠇ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠋ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠙ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠹ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠼ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠹ Building for production...Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest Why you should do it regularly: https://github.com/browserslist/update-db#readme ⠙ Building for production... WARNING Compiled with 4 warnings 08:31:03 warning /js/chunk-vendors.1a0a0613.js is 2.28 MB, and won't be precached. Configure maximumFileSizeToCacheInBytes to change this limit. warning asset size limit: The following asset(s) exceed the recommended size limit (244 KiB). This can impact web performance. Assets: img/0001.bbc23d94.jpg (1.85 MiB) css/chunk-vendors.7d75f85b.css (306 KiB) js/chunk-vendors.1a0a0613.js (2.17 MiB) warning entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance. Entrypoints: app (2.6 MiB) css/chunk-vendors.7d75f85b.css js/chunk-vendors.1a0a0613.js css/app.e3c96718.css js/app.22cb88f3.js warning webpack performance recommendations: You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application. For more info visit https://webpack.js.org/guides/code-splitting/ File Size Gzipped dist\js\chunk-vendors.1a0a0613.js 2222.03 KiB 687.66 KiB dist\js\app.22cb88f3.js 130.49 KiB 17.91 KiB dist\workbox-5b385ed2.js 14.11 KiB 4.93 KiB dist\service-worker.js 1.20 KiB 0.76 KiB dist\css\chunk-vendors.7d75f85b.css 305.86 KiB 42.07 KiB dist\css\app.e3c96718.css 1.40 KiB 0.63 KiB Images and other types of assets omitted. Build at: 2025-07-03T00:31:03.176Z - Hash: 9fc57e568ce674b2 - Time: 40125ms DONE Build complete. The dist directory is ready to be deployed. INFO Check out deployment instructions at https://cli.vuejs.org/guide/deployment.html • electron-builder version=26.0.12 os=10.0.19041 • description is missed in the package.json appPackageFile=E:\soft\finnal\yuanma2\crm1\package.json • author is missed in the package.json appPackageFile=E:\soft\finnal\yuanma2\crm1\package.json • writing effective config file=dist\builder-effective-config.yaml • executing @electron/rebuild electronVersion=37.2.0 arch=x64 buildFromSource=false appDir=./ • installing native dependencies arch=x64 • completed installing native dependencies • packaging platform=win32 arch=x64 electron=37.2.0 appOutDir=dist\win-unpacked
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值