项目作者: les1502

项目描述 :
babel-plugin-import 是一款 babel 插件,它会在编译过程中将 import 的写法自动转换为按需引入的方式
高级语言: JavaScript
项目地址: git://github.com/les1502/babel-plugin-import.git
创建时间: 2019-03-20T01:42:04Z
项目社区:https://github.com/les1502/babel-plugin-import

开源协议:

下载


babel-plugin-import

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

NPM version
Build Status


Why babel-plugin-import

Where to add babel-plugin-import

Example

{ "libraryName": "antd" }

  1. import { Button } from 'antd';
  2. ReactDOM.render(<Button>xxxx</Button>);
  3. var _button = require('antd/lib/button');
  4. ReactDOM.render(<_button>xxxx</_button>);

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

  1. import { Button } from 'antd';
  2. ReactDOM.render(<Button>xxxx</Button>);
  3. var _button = require('antd/lib/button');
  4. require('antd/lib/button/style/css');
  5. ReactDOM.render(<_button>xxxx</_button>);

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

  1. import { Button } from 'antd';
  2. ReactDOM.render(<Button>xxxx</Button>);
  3. var _button = require('antd/lib/button');
  4. require('antd/lib/button/style');
  5. 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

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

Via .babelrc or babel-loader.

  1. {
  2. "plugins": [["import", options]]
  3. }

options

options can be object.

  1. {
  2. "libraryName": "antd",
  3. "style": true, // or 'css'
  4. }
  1. {
  2. "libraryName": "lodash",
  3. "libraryDirectory": "",
  4. "camel2DashComponentName": false, // default: true
  5. }
  1. {
  2. "libraryName": "material-ui",
  3. "libraryDirectory": "components", // default: lib
  4. "camel2DashComponentName": false, // default: true
  5. }

~options can be an array.~ It’s not available in bable@7+

For Example:

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

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

For Example:

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

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

Note

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