1 | # babel-plugin-import
|
2 |
|
3 | Modular import plugin for babel, compatible with [antd](https://github.com/ant-design/ant-design), [antd-mobile](https://github.com/ant-design/ant-design-mobile), lodash, [material-ui](http://material-ui.com/), and so on.
|
4 |
|
5 | [![NPM version](https://img.shields.io/npm/v/babel-plugin-import.svg?style=flat)](https://npmjs.org/package/babel-plugin-import)
|
6 | [![Build Status](https://img.shields.io/travis/ant-design/babel-plugin-import.svg?style=flat)](https://travis-ci.org/ant-design/babel-plugin-import)
|
7 |
|
8 | ----
|
9 |
|
10 | ## Why babel-plugin-import
|
11 |
|
12 | - [English Instruction](https://ant.design/docs/react/getting-started#Import-on-Demand)
|
13 | - [中文说明](https://ant.design/docs/react/getting-started-cn#%E6%8C%89%E9%9C%80%E5%8A%A0%E8%BD%BD)
|
14 |
|
15 | ## Where to add babel-plugin-import
|
16 |
|
17 | - [babelrc](https://babeljs.io/docs/usage/babelrc/)
|
18 | - [babel-loader](https://github.com/babel/babel-loader)
|
19 |
|
20 | ## Example
|
21 |
|
22 | #### `{ "libraryName": "antd" }`
|
23 |
|
24 | ```javascript
|
25 | import { Button } from 'antd';
|
26 | ReactDOM.render(<Button>xxxx</Button>);
|
27 |
|
28 | ↓ ↓ ↓ ↓ ↓ ↓
|
29 |
|
30 | var _button = require('antd/lib/button');
|
31 | ReactDOM.render(<_button>xxxx</_button>);
|
32 | ```
|
33 |
|
34 | #### `{ "libraryName": "antd", style: "css" }`
|
35 |
|
36 | ```javascript
|
37 | import { Button } from 'antd';
|
38 | ReactDOM.render(<Button>xxxx</Button>);
|
39 |
|
40 | ↓ ↓ ↓ ↓ ↓ ↓
|
41 |
|
42 | var _button = require('antd/lib/button');
|
43 | require('antd/lib/button/style/css');
|
44 | ReactDOM.render(<_button>xxxx</_button>);
|
45 | ```
|
46 |
|
47 | #### `{ "libraryName": "antd", style: true }`
|
48 |
|
49 | ```javascript
|
50 | import { Button } from 'antd';
|
51 | ReactDOM.render(<Button>xxxx</Button>);
|
52 |
|
53 | ↓ ↓ ↓ ↓ ↓ ↓
|
54 |
|
55 | var _button = require('antd/lib/button');
|
56 | require('antd/lib/button/style');
|
57 | ReactDOM.render(<_button>xxxx</_button>);
|
58 | ```
|
59 |
|
60 | 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.
|
61 |
|
62 | `style: true` can reduce the bundle size significantly, depending on your usage of the library.
|
63 |
|
64 | ## Usage
|
65 |
|
66 | ```bash
|
67 | npm install babel-plugin-import --save-dev
|
68 | ```
|
69 |
|
70 | Via `.babelrc` or babel-loader.
|
71 |
|
72 | ```js
|
73 | {
|
74 | "plugins": [["import", options]]
|
75 | }
|
76 | ```
|
77 |
|
78 | ### options
|
79 |
|
80 | `options` can be object.
|
81 |
|
82 | ```javascript
|
83 | {
|
84 | "libraryName": "antd",
|
85 | "style": true, // or 'css'
|
86 | }
|
87 | ```
|
88 |
|
89 | ```javascript
|
90 | {
|
91 | "libraryName": "lodash",
|
92 | "libraryDirectory": "",
|
93 | "camel2DashComponentName": false, // default: true
|
94 | }
|
95 | ```
|
96 |
|
97 | ```javascript
|
98 | {
|
99 | "libraryName": "@material-ui/core",
|
100 | "libraryDirectory": "components", // default: lib
|
101 | "camel2DashComponentName": false, // default: true
|
102 | }
|
103 | ```
|
104 |
|
105 | ~`options` can be an array.~ It's not available in babel@7+
|
106 |
|
107 | For Example:
|
108 |
|
109 | ```javascript
|
110 | [
|
111 | {
|
112 | "libraryName": "antd",
|
113 | "libraryDirectory": "lib", // default: lib
|
114 | "style": true
|
115 | },
|
116 | {
|
117 | "libraryName": "antd-mobile"
|
118 | },
|
119 | ]
|
120 | ```
|
121 | `Options` can't be an array in babel@7+, but you can add plugins with name to support multiple dependencies.
|
122 |
|
123 | For Example:
|
124 |
|
125 | ```javascrit
|
126 | // .babelrc
|
127 | "plugins": [
|
128 | ["import", { "libraryName": "antd", "libraryDirectory": "lib"}, "ant"],
|
129 | ["import", { "libraryName": "antd-mobile", "libraryDirectory": "lib"}, "antd-mobile"]
|
130 | ]
|
131 | ```
|
132 |
|
133 | #### style
|
134 |
|
135 | - `["import", { "libraryName": "antd" }]`: import js modularly
|
136 | - `["import", { "libraryName": "antd", "style": true }]`: import js and css modularly (LESS/Sass source files)
|
137 | - `["import", { "libraryName": "antd", "style": "css" }]`: import js and css modularly (css built files)
|
138 |
|
139 | 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.
|
140 |
|
141 | e.g.
|
142 | - ``["import", { "libraryName": "antd", "style": (name) => `${name}/style/2x` }]``: import js and css modularly & css file path is `ComponentName/style/2x`
|
143 |
|
144 | If a component has no style, you can use the `style` function to return a `false` and the style will be ignored.
|
145 |
|
146 | e.g.
|
147 | ```js
|
148 | [
|
149 | "import",
|
150 | {
|
151 | "libraryName": "antd",
|
152 | "style": (name: string, file: Object) => {
|
153 | if(name === 'antd/lib/utils'){
|
154 | return false;
|
155 | }
|
156 | return `${name}/style/2x`;
|
157 | }
|
158 | }
|
159 | ]
|
160 | ```
|
161 |
|
162 | #### customName
|
163 |
|
164 | We can use `customName` to customize import file path.
|
165 |
|
166 | For example, the default behavior:
|
167 |
|
168 | ```typescript
|
169 | import { TimePicker } from "antd"
|
170 | ↓ ↓ ↓ ↓ ↓ ↓
|
171 | var _button = require('antd/lib/time-picker');
|
172 | ```
|
173 |
|
174 | You can set `camel2DashComponentName` to `false` to disable transfer from camel to dash:
|
175 |
|
176 | ```typescript
|
177 | import { TimePicker } from "antd"
|
178 | ↓ ↓ ↓ ↓ ↓ ↓
|
179 | var _button = require('antd/lib/TimePicker');
|
180 | ```
|
181 |
|
182 | And finally, you can use `customName` to customize each name parsing:
|
183 |
|
184 | ```js
|
185 | [
|
186 | "import",
|
187 | {
|
188 | "libraryName": "antd",
|
189 | "customName": (name: string) => {
|
190 | if (name === 'TimePicker'){
|
191 | return 'antd/lib/custom-time-picker';
|
192 | }
|
193 | return `antd/lib/${name}`;
|
194 | }
|
195 | }
|
196 | ]
|
197 | ```
|
198 |
|
199 | So this result is:
|
200 |
|
201 | ```typescript
|
202 | import { TimePicker } from "antd"
|
203 | ↓ ↓ ↓ ↓ ↓ ↓
|
204 | var _button = require('antd/lib/custom-time-picker');
|
205 | ```
|
206 |
|
207 | In some cases, the transformer may serialize the configuration object. If we set the `customName` to a function, it will lost after the serialization.
|
208 |
|
209 | So we also support specifying the customName with a JavaScript source file path:
|
210 |
|
211 | ```js
|
212 | [
|
213 | "import",
|
214 | {
|
215 | "libraryName": "antd",
|
216 | "customName": require('path').resolve(__dirname, './customName.js')
|
217 | }
|
218 | ]
|
219 | ```
|
220 |
|
221 | The `customName.js` looks like this:
|
222 |
|
223 | ```js
|
224 | module.exports = function customName(name) {
|
225 | return `antd/lib/${name}`;
|
226 | };
|
227 | ```
|
228 |
|
229 | #### transformToDefaultImport
|
230 |
|
231 | Set this option to `false` if your module does not have a `default` export.
|
232 |
|
233 | ### Note
|
234 |
|
235 | babel-plugin-import will not work properly if you add the library to the webpack config [vendor](https://webpack.github.io/docs/code-splitting.html#split-app-and-vendor-code).
|