[Webpack 2] Maintain sane file sizes with webpack code splitting
发布日期:2021-08-19 22:17:10 浏览次数:14 分类:技术文章

本文共 1664 字,大约阅读时间需要 5 分钟。

As a Single Page Application grows in size, the size of the payload can become a real problem for performance. In this lesson, learn how to leverage code splitting to easily implement lazy loading for your application to load only the code necessary for a particular feature or functionality.

 

Here we loads facts at the beginning. So this will be bundled into the bundle.js file. This is not good enought if the application becomes large.

import {$on} from './helpers'import * as facts from './facts'const factsList = document.getElementById('facts-list')const factText = document.getElementById('fact-text')$on(factsList, 'click', ({target: {dataset: {fact}}}) => {  if (!facts) {      factText.innerText = facts.defaultFact      return  }   factText.innerText = facts[fact]})

 

So what we want to do is loading the file on demand. And wepack will help to load file on runtime.

import {$on} from './helpers'const factsList = document.getElementById('facts-list')const factText = document.getElementById('fact-text')$on(factsList, 'click', ({target: {dataset: {fact}}}) => {    if (!fact) {        return System.import('./facts/default-fact').then(setFactText)    }    System.import('./facts/' + fact).then(setFactText)    function setFactText({fact: animalFact}) {        factText.innerText = animalFact    }})

To do that, we need to tell Webpack to import file when it needed by using :

System.import('./facts/' + fact)

It returns a promise, then we do parse the stuff:

System.import('./facts/' + fact).then(setFactText)    function setFactText({fact: animalFact}) {        factText.innerText = animalFact    }

 

转载于:https://www.cnblogs.com/Answer1215/p/5608611.html

转载地址:https://blog.csdn.net/weixin_30955617/article/details/95585601 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:[Practical Git] Diagnose which commit broke something with git bisect
下一篇:js代码中的parent,top和self有什么区别

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年12月27日 22时33分42秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章