Code Splitting

Code splitting is one of the most compelling features of webpack. It allows you to split your code into various bundles which you can then load on demand — like when a user navigates to a matching route, or on an event from the user. This allows for smaller bundles, and allows you to control resource load prioritization, which if used correctly, can have a major impact on your application load time.

There are mainly two kinds of code splitting that can be accomplished with webpack:

Resource splitting for caching and parallel loads

Vendor code splitting

A typical application can depend on many third party libraries for framework/functionality needs. Unlike application code, code present in these libraries does not change often.

If we keep code from these libraries in its own bundle, separate from the application code, we can leverage the browser's caching mechanism to cache these files for longer durations on the end user's machine.

For this to work, the[hash] portion in the vendor filename must remain constant, regardless of application code changes. Learn how to split vendor/library code using the CommonsChunkPlugin

CSS splitting

You might also want to split your styles into a separate bundle, independent from application logic.This enhances cacheability of your styles and allows the browser to load the styles in-parallel with your application code, thus preventing a FOUC (flash of unstyled content).

Learn how to split CSS using the ExtractTextWebpackPlugin
.

On demand code-splitting

While resource splitting of the previous kind requires the user to specify the split points upfront in the configuration, one can also create dynamic split points in the application code.

This can be used for more granular chunking of code, for example, per our application routes or as per predicted user behaviour. This allows the user to load non-essential assets on demand.

  • Code Splitting - Usingimport() – proposal for ECMAScript
  • Code Splitting - Usingrequire.ensure – CommonJS way

.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末淌哟,一起剝皮案震驚了整個(gè)濱河市嚎朽,隨后出現(xiàn)的幾起案子虱岂,更是在濱河造成了極大的恐慌聂使,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,265評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡造壮,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門骂束,熙熙樓的掌柜王于貴愁眉苦臉地迎上來耳璧,“玉大人,你說我怎么就攤上這事展箱±懵眨” “怎么了?”我有些...
    開封第一講書人閱讀 156,852評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵析藕,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我凳厢,道長(zhǎng)账胧,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,408評(píng)論 1 283
  • 正文 為了忘掉前任先紫,我火速辦了婚禮治泥,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘遮精。我一直安慰自己居夹,他們只是感情好败潦,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評(píng)論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著准脂,像睡著了一般劫扒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上狸膏,一...
    開封第一講書人閱讀 49,772評(píng)論 1 290
  • 那天沟饥,我揣著相機(jī)與錄音,去河邊找鬼湾戳。 笑死贤旷,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的砾脑。 我是一名探鬼主播幼驶,決...
    沈念sama閱讀 38,921評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼韧衣!你這毒婦竟也來了盅藻?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,688評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤汹族,失蹤者是張志新(化名)和其女友劉穎萧求,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體顶瞒,經(jīng)...
    沈念sama閱讀 44,130評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡夸政,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了榴徐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片守问。...
    茶點(diǎn)故事閱讀 38,617評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖坑资,靈堂內(nèi)的尸體忽然破棺而出耗帕,到底是詐尸還是另有隱情,我是刑警寧澤袱贮,帶...
    沈念sama閱讀 34,276評(píng)論 4 329
  • 正文 年R本政府宣布仿便,位于F島的核電站,受9級(jí)特大地震影響攒巍,放射性物質(zhì)發(fā)生泄漏嗽仪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評(píng)論 3 312
  • 文/蒙蒙 一柒莉、第九天 我趴在偏房一處隱蔽的房頂上張望闻坚。 院中可真熱鬧,春花似錦兢孝、人聲如沸窿凤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽雳殊。三九已至橘沥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間相种,已是汗流浹背威恼。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評(píng)論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留寝并,地道東北人箫措。 一個(gè)月前我還...
    沈念sama閱讀 46,315評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像衬潦,于是被迫代替她去往敵國和親斤蔓。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容