Deploying-es2015-code-in-production-by Vue-CLI

尤大大涉奇的腳步真是從未停歇~
在這之前有兩篇相關(guān)文章,大家可以參考下剃法。

1.Phillip Walton's excellent post
2.粽子哥

vue-cli v3 加入了'modern build' 特性
摘自vue-cli browser-compatibility BEGIN

Browser Compatibility

browserslist

You will notice a browserslist field in package.json specifying a range of browsers the project is targeting. This value will be used by @babel/preset-env and autoprefixer to automatically determine the JavaScript features that need to be transpiled and the CSS vendor prefixes needed.

See here for how to specify browser ranges.

Polyfills

A default Vue CLI project uses @vue/babel-preset-app, which uses @babel/preset-env and the browserslist config to determine the Polyfills needed for your project.

By default, it passes useBuiltIns: 'usage' to @babel/preset-env which automatically detects the polyfills needed based on the language features used in your source code. This ensures only the minimum amount of polyfills are included in your final bundle. However, this also means if one of your dependencies has specific requirements on polyfills, by default Babel won't be able to detect it.

If one of your dependencies need polyfills, you have a few options:

  1. If the dependency is written in an ES version that your target environments do not support: Add that dependency to the transpileDependencies option in vue.config.js. This would enable both syntax transforms and usage-based polyfill detection for that dependency.

  2. If the dependency ships ES5 code and explicitly lists the polyfills needed: you can pre-include the needed polyfills using the polyfills option for @vue/babel-preset-app. Note that es6.promise is included by default because it's very common for libs to depend on Promises.

    // babel.config.js
    module.exports = {
      presets: [
        ['@vue/app', {
          polyfills: [
            'es6.promise',
            'es6.symbol'
          ]
        }]
      ]
    }
    

    ::: tip
    It's recommended to add polyfills this way instead of directly importing them in your source code, because polyfills listed here can be automatically excluded if your browserslist targets don't need them.
    :::

  3. If the dependency ships ES5 code, but uses ES6+ features without explicitly listing polyfill requirements (e.g. Vuetify): Use useBuiltIns: 'entry' and then add import '@babel/polyfill' to your entry file. This will import ALL polyfills based on your browserslist targets so that you don't need to worry about dependency polyfills anymore, but will likely increase your final bundle size with some unused polyfills.

See @babel-preset/env docs for more details.

Modern Mode

With Babel we are able to leverage all the newest language features in ES2015+, but that also means we have to ship transpiled and polyfilled bundles in order to support older browsers. These transpiled bundles are often more verbose than the original native ES2015+ code, and also parse and run slower. Given that today a good majority of the modern browsers have decent support for native ES2015, it is a waste that we have to ship heavier and less efficient code to those browsers just because we have to support older ones.

Vue CLI offers a "Modern Mode" to help you solve this problem. When building for production with the following command:

vue-cli-service build --modern

Vue CLI will produce two versions of your app: one modern bundle targeting modern browsers that support ES modules, and one legacy bundle targeting older browsers that do not.

The cool part though is that there are no special deployment requirements. The generated HTML file automatically employs the techniques discussed in Phillip Walton's excellent post:

  • The modern bundle is loaded with <script type="module">, in browsers that support it; they are also preloaded using <link rel="modulepreload"> instead.

  • The legacy bundle is loaded with <script nomodule>, which is ignored by browsers that support ES modules.

  • A fix for <script nomodule> in Safari 10 is also automatically injected.

For a Hello World app, the modern bundle is already 16% smaller. In production, the modern bundle will typically result in significantly faster parsing and evaluation, improving your app's loading performance.

摘自vue-cli browser-compatibility END

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末歇盼,一起剝皮案震驚了整個濱河市腔长,隨后出現(xiàn)的幾起案子同欠,更是在濱河造成了極大的恐慌,老刑警劉巖角塑,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異淘讥,居然都是意外死亡圃伶,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門蒲列,熙熙樓的掌柜王于貴愁眉苦臉地迎上來窒朋,“玉大人,你說我怎么就攤上這事蝗岖〗男桑” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵抵赢,是天一觀的道長拭宁。 經(jīng)常有香客問我,道長瓣俯,這世上最難降的妖魔是什么杰标? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮彩匕,結(jié)果婚禮上腔剂,老公的妹妹穿的比我還像新娘。我一直安慰自己驼仪,他們只是感情好掸犬,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布袜漩。 她就那樣靜靜地躺著,像睡著了一般湾碎。 火紅的嫁衣襯著肌膚如雪宙攻。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天介褥,我揣著相機與錄音座掘,去河邊找鬼。 笑死柔滔,一個胖子當著我的面吹牛溢陪,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播睛廊,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼形真,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了超全?” 一聲冷哼從身側(cè)響起咆霜,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嘶朱,沒想到半個月后蛾坯,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡见咒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年偿衰,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片改览。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡下翎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出宝当,到底是詐尸還是另有隱情视事,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布庆揩,位于F島的核電站俐东,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏订晌。R本人自食惡果不足惜虏辫,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望锈拨。 院中可真熱鬧砌庄,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至萌焰,卻和暖如春哺眯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背扒俯。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工奶卓, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人陵珍。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓寝杖,卻偏偏與公主長得像违施,于是被迫代替她去往敵國和親互纯。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354

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