Babel 中文文档 Babel 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 指南
    • Babel 是什么?
    • 使用指南
    • 配置 Babel
    • 学习 ES2015
    • 升级到 Babel 7
  • 配置

  • 预设环境

  • 杂项

  • 集成

  • 工具

  • 辅助

babel-plugin-import


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

Why babel-plugin-import


English Instruction
中文说明

Where to add babel-plugin-import


babelrc
babel-loader

Example


{ "libraryName": "antd" }


  1. ``` js
  2. import { Button } from 'antd';
  3. ReactDOM.render(<Button>xxxx</Button>);

  4.       ↓ ↓ ↓ ↓ ↓ ↓

  5. var _button = require('antd/lib/button');
  6. ReactDOM.render(<_button>xxxx</_button>);
  7. ```

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


  1. ``` js
  2. import { Button } from 'antd';
  3. ReactDOM.render(<Button>xxxx</Button>);

  4.       ↓ ↓ ↓ ↓ ↓ ↓

  5. var _button = require('antd/lib/button');
  6. require('antd/lib/button/style/css');
  7. ReactDOM.render(<_button>xxxx</_button>);
  8. ```

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


  1. ``` js
  2. import { Button } from 'antd';
  3. ReactDOM.render(<Button>xxxx</Button>);

  4.       ↓ ↓ ↓ ↓ ↓ ↓

  5. var _button = require('antd/lib/button');
  6. require('antd/lib/button/style');
  7. ReactDOM.render(<_button>xxxx</_button>);
  8. ```

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


  1. ``` shell
  2. npm install babel-plugin-import --save-dev
  3. ```

Via .babelrc or babel-loader.

  1. ``` js
  2. {
  3.   "plugins": [["import", options]]
  4. }
  5. ```

options


options can be object.

  1. ``` js
  2. {
  3.   "libraryName": "antd",
  4.   "style": true,   // or 'css'
  5. }
  6. ```

  1. ``` js
  2. {
  3.   "libraryName": "lodash",
  4.   "libraryDirectory": "",
  5.   "camel2DashComponentName": false,  // default: true
  6. }
  7. ```

  1. ``` js
  2. {
  3.   "libraryName": "@material-ui/core",
  4.   "libraryDirectory": "components",  // default: lib
  5.   "camel2DashComponentName": false,  // default: true
  6. }
  7. ```

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

For Example:

  1. ``` js
  2. [
  3.   {
  4.     "libraryName": "antd",
  5.     "libraryDirectory": "lib",   // default: lib
  6.     "style": true
  7.   },
  8.   {
  9.     "libraryName": "antd-mobile"
  10.   },
  11. ]
  12. ```

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

For Example:

  1. ``` sh
  2. // .babelrc
  3. "plugins": [
  4.   ["import", { "libraryName": "antd", "libraryDirectory": "lib"}, "antd"],
  5.   ["import", { "libraryName": "antd-mobile", "libraryDirectory": "lib"}, "antd-mobile"]
  6. ]

  7. ```

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.

  1. ``` js
  2. [
  3.   "import",
  4.     {
  5.       "libraryName": "antd",
  6.       "style": (name: string, file: Object) => {
  7.         if(name === 'antd/lib/utils'){
  8.           return false;
  9.         }
  10.         return `${name}/style/2x`;
  11.       }
  12.     }
  13. ]
  14. ```

styleLibraryDirectory


["import", { "libraryName": "element-ui", "styleLibraryDirectory": "lib/theme-chalk" }] : import js and css modularly

If styleLibraryDirectory is provided (default null ), it will be used to form style file path, style will be ignored then. e.g.

  1. ``` js
  2. {
  3.   "libraryName": "element-ui",
  4.   "styleLibraryDirectory": "lib/theme-chalk",
  5. }

  6. import { Button } from 'element-ui';

  7.       ↓ ↓ ↓ ↓ ↓ ↓

  8. var _button = require('element-ui/lib/button');
  9. require('element-ui/lib/theme-chalk/button');
  10. ```

customName


We can use customName to customize import file path.

For example, the default behavior:

  1. ``` ts
  2. import { TimePicker } from "antd"
  3. ↓ ↓ ↓ ↓ ↓ ↓
  4. var _button = require('antd/lib/time-picker');
  5. ```

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

  1. ``` ts
  2. import { TimePicker } from "antd"
  3. ↓ ↓ ↓ ↓ ↓ ↓
  4. var _button = require('antd/lib/TimePicker');
  5. ```

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

  1. ``` js
  2. [
  3.   "import",
  4.     {
  5.       "libraryName": "antd",
  6.       "customName": (name: string, file: object) => {
  7.         const filename = file.opts.filename;
  8.         if (name === 'TimePicker'){
  9.           return 'antd/lib/custom-time-picker';
  10.         }
  11.         if (filename.indexOf('/path/to/my/different.js') >= 0) {
  12.           return 'antd/lib/custom-name';
  13.         }
  14.         return `antd/lib/${name}`;
  15.       }
  16.     }
  17. ]
  18. ```

So this result is:

  1. ``` ts
  2. import { TimePicker } from "antd"
  3. ↓ ↓ ↓ ↓ ↓ ↓
  4. var _button = require('antd/lib/custom-time-picker');
  5. ```

In some cases, the transformer may serialize the configuration object. If we set the customName to a function, it will lost after the serialization.

So we also support specifying the customName with a JavaScript source file path:

  1. ``` js
  2. [
  3.   "import",
  4.     {
  5.       "libraryName": "antd",
  6.       "customName": require('path').resolve(__dirname, './customName.js')
  7.     }
  8. ]
  9. ```

The customName.js looks like this:

  1. ``` js
  2. module.exports = function customName(name) {
  3.   return `antd/lib/${name}`;
  4. };
  5. ```

customStyleName


customStyleName works exactly the same as customName, except that it deals with style file path.

transformToDefaultImport


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

Note


babel-plugin-import will not work properly if you add the library to the webpack config vendor.
Last Updated: 2023-09-03 17:10:52