針對iview進行優(yōu)化:babel-plugin-import-custom

babel-plugin-import-custom

customized for iview, and keep the original function unchanged.

主要改動點:

  • import iView from 'iview'铺根,在參數(shù)load設(shè)置成'auto'后乳绕,可自動把iview所有組件按照按需引入的方式全部引入,見下例

  • import { Circle, Table } from 'iview',在參數(shù)load設(shè)置成'auto'后精置,可自動將兩個組件按Vue.component方式引入,見下例

  • 當(dāng)libraryName為iview時艳吠,style設(shè)置成css或true主卫,不能按需引入樣式,只能全量引入

打包大小對比圖(都引入了vue及相關(guān)loader洋幻、template-loader)

全量引入 按需引入(Button, Table) 按需引入(Button) 按需引入全部組件
634 KiB 169 KiB 73.4 KiB 501 KiB

Modular import plugin for babel, compatible with iview antd, antd-mobile, lodash, material-ui, and so on.


Why babel-plugin-import-custom

Where to add babel-plugin-import-custom

Example

{ "libraryName": "iview", "load": 'auto', "style": 'css' }

import { Circle, Table } from 'iview';

      ↓ ↓ ↓ ↓ ↓ ↓
      
import _Table from "iview/src/components/table";
import _Circle from "iview/src/components/circle";
Vue.component("iCircle", _Circle);
Vue.component("iTable", _Table);
Vue.component("Table", _Table);
import "iview/dist/styles/iview.css";

{ "libraryName": "iview", "load": 'auto', "style": true }

import { Circle, Table } from 'iview';

      ↓ ↓ ↓ ↓ ↓ ↓
      
import _Table from "iview/src/components/table";
import _Circle from "iview/src/components/circle";
Vue.component("iCircle", _Circle);
Vue.component("iTable", _Table);
Vue.component("Table", _Table);
import "iview/src/styles/index.less";

{ "libraryName": "iview", "load": 'auto', "style": true }

import iView from 'iview';

      ↓ ↓ ↓ ↓ ↓ ↓
      
// 引入全量的組件
import _Table from "iview/src/components/table";
import _Circle from "iview/src/components/circle";
// ...
import Switch from "iview/src/components/switch";
Vue.component("iCircle", _Circle);
Vue.component("iTable", _Table);
Vue.component("Table", _Table);
// ... 
Vue.component("iSwitch", Switch);
import "iview/src/styles/index.less";

{ "libraryName": "iview", "style": true }

import iView from 'iview'
Vue.use(iView)

      ↓ ↓ ↓ ↓ ↓ ↓
      
import iView from 'iview'
Vue.use(iView)
import "iview/src/styles/index.less";

{ "libraryName": "iview", "style": true }

import { Circle, Table } from 'iview'
Vue.component('iCircle', Circle)

      ↓ ↓ ↓ ↓ ↓ ↓
      
import _Circle from "iview/src/components/circle";
import "iview/src/styles/index.less";\nVue.component(\'iCircle\', _Circle);'

{ "libraryName": "iview", "style": true }

import { Circle, Table } from 'iview'

      ↓ ↓ ↓ ↓ ↓ ↓
      
import "iview/src/styles/index.less";

{ "libraryName": "antd" }

import { Button } from 'antd';
ReactDOM.render(<Button>xxxx</Button>);

      ↓ ↓ ↓ ↓ ↓ ↓
      
var _button = require('antd/lib/button');
ReactDOM.render(<_button>xxxx</_button>);

{ "libraryName": "antd", style: "css" }

當(dāng)libraryName為iview時郁轻,style設(shè)置成css或true,不能按需引入樣式,只能全量引入

import { Button } from 'antd';
ReactDOM.render(<Button>xxxx</Button>);

      ↓ ↓ ↓ ↓ ↓ ↓
      
var _button = require('antd/lib/button');
require('antd/lib/button/style/css');
ReactDOM.render(<_button>xxxx</_button>);

{ "libraryName": "antd", style: true }

import { Button } from 'antd';
ReactDOM.render(<Button>xxxx</Button>);

      ↓ ↓ ↓ ↓ ↓ ↓
      
var _button = require('antd/lib/button');
require('antd/lib/button/style');
ReactDOM.render(<_button>xxxx</_button>);

Note : with style: true css source files are imported and optimizations can be done during compilation time. With style: "css", pre bundled css files are imported as they are.

style: true can reduce the bundle size significantly, depending on your usage of the library.

Usage

npm install babel-plugin-import-custom --save-dev

Via .babelrc or babel-loader.

{
  "plugins": [["import", options]]
}

options

options can be object.

{
  "libraryName": "antd",
  "style": true,   // or 'css'
}
{
  "libraryName": "lodash",
  "libraryDirectory": "",
  "camel2DashComponentName": false,  // default: true
}
{
  "libraryName": "@material-ui/core",
  "libraryDirectory": "components",  // default: lib
  "camel2DashComponentName": false,  // default: true
}

~options can be an array.~ It's not available in babel@7+

For Example:

[
  {
    "libraryName": "antd",
    "libraryDirectory": "lib",   // default: lib
    "style": true
  },
  {
    "libraryName": "antd-mobile"
  },
]

Options can't be an array in babel@7+, but you can add plugins with name to support multiple dependencies.

For Example:

// .babelrc
"plugins": [
  ["import", { "libraryName": "antd", "libraryDirectory": "lib"}, "ant"],
  ["import", { "libraryName": "antd-mobile", "libraryDirectory": "lib"}, "antd-mobile"]
]

style

  • ["import", { "libraryName": "antd" }]: import js modularly
  • ["import", { "libraryName": "antd", "style": true }]: import js and css modularly (LESS/Sass source files)
  • ["import", { "libraryName": "antd", "style": "css" }]: import js and css modularly (css built files)

If option style is a Function, babel-plugin-import-custom will auto import the file which filepath equal to the function return value. This is useful for the components library developers.

e.g.

  • ["import", { "libraryName": "antd", "style": (name) => `${name}/style/2x` }]: import js and css modularly & css file path is ComponentName/style/2x

If a component has no style, you can use the style function to return a false and the style will be ignored.

e.g.

[
  "import", 
    { 
      "libraryName": "antd", 
      "style": (name: string, file: Object) => {
        if(name === 'antd/lib/utils'){
          return false;
        }
        return `${name}/style/2x`;
      }
    }
]

customName

We can use customName to customize import file path.

For example, the default behavior:

import { TimePicker } from "antd"
↓ ↓ ↓ ↓ ↓ ↓
var _button = require('antd/lib/time-picker');

You can set camel2DashComponentName to false to disable transfer from camel to dash:

import { TimePicker } from "antd"
↓ ↓ ↓ ↓ ↓ ↓
var _button = require('antd/lib/TimePicker');

And finally, you can use customName to customize each name parsing:

[
  "import", 
    { 
      "libraryName": "antd", 
      "customName": (name: string) => {
        if (name === 'TimePicker'){
          return 'antd/lib/custom-time-picker';
        }
        return `antd/lib/${name}`;
      }
    }
]

So this result is:

import { TimePicker } from "antd"
↓ ↓ ↓ ↓ ↓ ↓
var _button = require('antd/lib/custom-time-picker');

transformToDefaultImport

Set this option to false if your module does not have a default export.

Note

babel-plugin-import-custom will not work properly if you add the library to the webpack config vendor.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末好唯,一起剝皮案震驚了整個濱河市竭沫,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌骑篙,老刑警劉巖蜕提,帶你破解...
    沈念sama閱讀 206,311評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異靶端,居然都是意外死亡谎势,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評論 2 382
  • 文/潘曉璐 我一進店門杨名,熙熙樓的掌柜王于貴愁眉苦臉地迎上來脏榆,“玉大人,你說我怎么就攤上這事台谍⌒胛梗” “怎么了?”我有些...
    開封第一講書人閱讀 152,671評論 0 342
  • 文/不壞的土叔 我叫張陵趁蕊,是天一觀的道長坞生。 經(jīng)常有香客問我,道長掷伙,這世上最難降的妖魔是什么是己? 我笑而不...
    開封第一講書人閱讀 55,252評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮任柜,結(jié)果婚禮上卒废,老公的妹妹穿的比我還像新娘。我一直安慰自己乘盼,他們只是感情好升熊,可當(dāng)我...
    茶點故事閱讀 64,253評論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著绸栅,像睡著了一般级野。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上粹胯,一...
    開封第一講書人閱讀 49,031評論 1 285
  • 那天蓖柔,我揣著相機與錄音,去河邊找鬼风纠。 笑死况鸣,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的竹观。 我是一名探鬼主播镐捧,決...
    沈念sama閱讀 38,340評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼潜索,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了懂酱?” 一聲冷哼從身側(cè)響起竹习,我...
    開封第一講書人閱讀 36,973評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎列牺,沒想到半個月后整陌,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,466評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡瞎领,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,937評論 2 323
  • 正文 我和宋清朗相戀三年泌辫,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片九默。...
    茶點故事閱讀 38,039評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡震放,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出驼修,到底是詐尸還是另有隱情澜搅,我是刑警寧澤,帶...
    沈念sama閱讀 33,701評論 4 323
  • 正文 年R本政府宣布邪锌,位于F島的核電站,受9級特大地震影響癌瘾,放射性物質(zhì)發(fā)生泄漏觅丰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,254評論 3 307
  • 文/蒙蒙 一妨退、第九天 我趴在偏房一處隱蔽的房頂上張望妇萄。 院中可真熱鬧,春花似錦咬荷、人聲如沸冠句。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,259評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽懦底。三九已至,卻和暖如春罕扎,著一層夾襖步出監(jiān)牢的瞬間聚唐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評論 1 262
  • 我被黑心中介騙來泰國打工腔召, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留杆查,地道東北人。 一個月前我還...
    沈念sama閱讀 45,497評論 2 354
  • 正文 我出身青樓臀蛛,卻偏偏與公主長得像亲桦,于是被迫代替她去往敵國和親崖蜜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,786評論 2 345

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