babel-plugin-import的配置項

babel-plugin-import

在 Babel 配置中引入該插件棘钞,可以針對 antd, antd-mobile, lodash, material-ui等庫進行按需加載.


為什么需要 babel-plugin-import

如何在項目中添加 babel-plugin-import

例子

{ "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" }

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>);

備注 : 配置 style: true 則在項目編譯階段月而,可以對引入的 antd 樣式文件進行編譯,從而可以壓縮打包尺寸殷勘;而配置style: "css", 則直接引入經過打包后的 antd 樣式文件

使用方式

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

通過 .babelrc 配置文件或者 babel-loader 模塊編程引入.

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

options

options can be object.

{
  "libraryName": "antd",
  "style": true,   // or 'css'
}
{
  "libraryName": "lodash",
  "libraryDirectory": "", //表示從庫的package.json的main入口;否則默認為lib文件夾
  "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 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

使用 customName 來自定義導入文件路徑.

插件默認導入文件的基礎路徑為 lib 目錄最爬,并且默認將引入的組件名轉換為按照"-"連接的結構:

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

設置 camel2DashComponentNamefalse來阻止組件名稱的轉換:

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

在 Babel 配置文件中铃岔,使用 customName 來自定義導入組件路徑:

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

上面編譯后的結果為:

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

transformToDefaultImport

如果打包后的模塊沒有default導出汪疮,則設置 false

備注

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

https://swift.ctolib.com/article/wiki/108814

babel-plugin-import-custom

針對iview做了額外的優(yōu)化,https://github.com/videring/babel-plugin-import-custom/blob/master/README.md

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市智嚷,隨后出現(xiàn)的幾起案子卖丸,更是在濱河造成了極大的恐慌,老刑警劉巖盏道,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件稍浆,死亡現(xiàn)場離奇詭異,居然都是意外死亡猜嘱,警方通過查閱死者的電腦和手機衅枫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來朗伶,“玉大人为鳄,你說我怎么就攤上這事⊥笕茫” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵歧斟,是天一觀的道長纯丸。 經常有香客問我,道長静袖,這世上最難降的妖魔是什么觉鼻? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮队橙,結果婚禮上坠陈,老公的妹妹穿的比我還像新娘。我一直安慰自己捐康,他們只是感情好仇矾,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著解总,像睡著了一般贮匕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上花枫,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天刻盐,我揣著相機與錄音,去河邊找鬼劳翰。 笑死敦锌,一個胖子當著我的面吹牛,可吹牛的內容都是我干的佳簸。 我是一名探鬼主播乙墙,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了伶丐?” 一聲冷哼從身側響起悼做,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎哗魂,沒想到半個月后肛走,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡录别,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年朽色,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片组题。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡葫男,死狀恐怖,靈堂內的尸體忽然破棺而出崔列,到底是詐尸還是另有隱情梢褐,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布赵讯,位于F島的核電站盈咳,受9級特大地震影響,放射性物質發(fā)生泄漏边翼。R本人自食惡果不足惜鱼响,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望组底。 院中可真熱鬧丈积,春花似錦、人聲如沸债鸡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽娘锁。三九已至牙寞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間莫秆,已是汗流浹背间雀。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留镊屎,地道東北人惹挟。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像缝驳,于是被迫代替她去往敵國和親连锯。 傳聞我的和親對象是個殘疾皇子归苍,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內容