UNPKG

2.67 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), 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## Example
16
17#### `{ "libraryName": "antd" }`
18
19```javascript
20import { Button } from 'antd';
21ReactDOM.render(<Button>xxxx</Button>);
22
23 ↓ ↓ ↓ ↓ ↓ ↓
24
25var _button = require('antd/lib/button');
26ReactDOM.render(<_button>xxxx</_button>);
27```
28
29#### `{ "libraryName": "antd", style: "css" }`
30
31```javascript
32import { Button } from 'antd';
33ReactDOM.render(<Button>xxxx</Button>);
34
35 ↓ ↓ ↓ ↓ ↓ ↓
36
37var _button = require('antd/lib/button');
38require('antd/lib/button/style/css');
39ReactDOM.render(<_button>xxxx</_button>);
40```
41
42#### `{ "libraryName": "antd", style: true }`
43
44```javascript
45import { Button } from 'antd';
46ReactDOM.render(<Button>xxxx</Button>);
47
48 ↓ ↓ ↓ ↓ ↓ ↓
49
50var _button = require('antd/lib/button');
51require('antd/lib/button/style');
52ReactDOM.render(<_button>xxxx</_button>);
53```
54
55## Usage
56
57```bash
58npm install babel-plugin-import --save-dev
59```
60
61Via `.babelrc` or babel-loader.
62
63```js
64{
65 "plugins": [["import", options]]
66}
67```
68
69### options
70
71`options` can be object.
72
73```javascript
74{
75 "libraryName": "antd",
76 "style": true, // or 'css'
77}
78```
79
80```javascript
81{
82 "libraryName": "material-ui",
83 "libraryDirectory": "components", // default: lib
84 "camel2DashComponentName": false, // default: true
85}
86```
87
88`options` can be an array.
89
90For Example:
91
92```javascript
93[
94 {
95 "libraryName": "antd",
96 "libraryDirectory": "lib", // default: lib
97 "style": true
98 },
99 {
100 "libraryName": "antd-mobile"
101 },
102]
103```
104
105### style
106
107- `["import", { "libraryName": "antd" }]`: import js modularly
108- `["import", { "libraryName": "antd", "style": true }]`: import js and css modularly (LESS/Sass source files)
109- `["import", { "libraryName": "antd", "style": "css" }]`: import js and css modularly (css built files)
110
111### Note
112
113babel-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).