UNPKG

6.52 kBMarkdownView Raw
1# babel-plugin-import
2
3Modular 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
25import { Button } from 'antd';
26ReactDOM.render(<Button>xxxx</Button>);
27
28 ↓ ↓ ↓ ↓ ↓ ↓
29
30var _button = require('antd/lib/button');
31ReactDOM.render(<_button>xxxx</_button>);
32```
33
34#### `{ "libraryName": "antd", style: "css" }`
35
36```javascript
37import { Button } from 'antd';
38ReactDOM.render(<Button>xxxx</Button>);
39
40 ↓ ↓ ↓ ↓ ↓ ↓
41
42var _button = require('antd/lib/button');
43require('antd/lib/button/style/css');
44ReactDOM.render(<_button>xxxx</_button>);
45```
46
47#### `{ "libraryName": "antd", style: true }`
48
49```javascript
50import { Button } from 'antd';
51ReactDOM.render(<Button>xxxx</Button>);
52
53 ↓ ↓ ↓ ↓ ↓ ↓
54
55var _button = require('antd/lib/button');
56require('antd/lib/button/style');
57ReactDOM.render(<_button>xxxx</_button>);
58```
59
60Note : 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
67npm install babel-plugin-import --save-dev
68```
69
70Via `.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
107For 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
123For 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
139If 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
141e.g.
142- ``["import", { "libraryName": "antd", "style": (name) => `${name}/style/2x` }]``: import js and css modularly & css file path is `ComponentName/style/2x`
143
144If a component has no style, you can use the `style` function to return a `false` and the style will be ignored.
145
146e.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#### styleLibraryDirectory
163
164- `["import", { "libraryName": "element-ui", "styleLibraryDirectory": "lib/theme-chalk" }]`: import js and css modularly
165
166If `styleLibraryDirectory` is provided (default `null`), it will be used to form style file path,
167`style` will be ignored then. e.g.
168
169```javascript
170{
171 "libraryName": "element-ui",
172 "styleLibraryDirectory": "lib/theme-chalk",
173}
174
175import { Button } from 'element-ui';
176
177 ↓ ↓ ↓ ↓ ↓ ↓
178
179var _button = require('element-ui/lib/button');
180require('element-ui/lib/theme-chalk/button');
181```
182
183#### customName
184
185We can use `customName` to customize import file path.
186
187For example, the default behavior:
188
189```typescript
190import { TimePicker } from "antd"
191↓ ↓ ↓ ↓ ↓ ↓
192var _button = require('antd/lib/time-picker');
193```
194
195You can set `camel2DashComponentName` to `false` to disable transfer from camel to dash:
196
197```typescript
198import { TimePicker } from "antd"
199↓ ↓ ↓ ↓ ↓ ↓
200var _button = require('antd/lib/TimePicker');
201```
202
203And finally, you can use `customName` to customize each name parsing:
204
205```js
206[
207 "import",
208 {
209 "libraryName": "antd",
210 "customName": (name: string) => {
211 if (name === 'TimePicker'){
212 return 'antd/lib/custom-time-picker';
213 }
214 return `antd/lib/${name}`;
215 }
216 }
217]
218```
219
220So this result is:
221
222```typescript
223import { TimePicker } from "antd"
224↓ ↓ ↓ ↓ ↓ ↓
225var _button = require('antd/lib/custom-time-picker');
226```
227
228In some cases, the transformer may serialize the configuration object. If we set the `customName` to a function, it will lost after the serialization.
229
230So we also support specifying the customName with a JavaScript source file path:
231
232```js
233[
234 "import",
235 {
236 "libraryName": "antd",
237 "customName": require('path').resolve(__dirname, './customName.js')
238 }
239]
240```
241
242The `customName.js` looks like this:
243
244```js
245module.exports = function customName(name) {
246 return `antd/lib/${name}`;
247};
248```
249
250#### customStyleName
251
252`customStyleName` works exactly the same as customName, except that it deals with style file path.
253
254#### transformToDefaultImport
255
256Set this option to `false` if your module does not have a `default` export.
257
258### Note
259
260babel-plugin-import will not work properly if you add the library to the webpack config [vendor](https://webpack.js.org/concepts/entry-points/#separate-app-and-vendor-entries).