Skip to content

Commit

Permalink
Merge pull request #52 from ant-design/update-config
Browse files Browse the repository at this point in the history
Update config
  • Loading branch information
sorrycc committed Aug 9, 2016
2 parents fa9560c + e1eb832 commit fa2cb8f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
41 changes: 33 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@ Converts

```javascript
import { Button } from 'antd';
import { Button as ButtonMobile } from 'antd-mobile';

ReactDOM.render(<div>
<Button>xxxx</Button>
<ButtonMobile>xxxx</ButtonMobile>
</div>);
```

(roughly) to

```javascript
var _button = require('antd/lib/button');
var _buttonMobile = require('antd-mobile/lib/button');

ReactDOM.render(<div>
<_button>xxxx</_button>
<_buttonMobile>xxxx</_buttonMobile>
</div>);
```

Expand All @@ -52,8 +48,37 @@ Via `.babelrc` or babel-loader.
}
```

### options.style
### options

- `["antd"]`: import js modularly
- `["antd", { "style": true }]`: import js and css modularly (less source files)
- `["antd", { "style": "css" }]`: import style css modularly (css built files)
`options` can be object. (will include antd library)

```javascript
{
style: true,
libraryDirectory: "component", // default: lib
}
```

`options` can be an array. (won't include antd library)

For Example:

```javascript
[
{
libraryName: "antd",
libraryDirectory: "lib", // default: lib
style: true,
},
{
libraryName: "antd-mobile",
libraryDirectory: "component",
},
]
```

### style

- `["antd", [{ "libraryName": "antd" }]]`: import js modularly
- `["antd", [{ "libraryName": "antd", "style": true }]]`: import js and css modularly (less source files)
- `["antd", [{ "libraryName": "antd", "style": "css" }]]`: import style css modularly (css built files)
9 changes: 6 additions & 3 deletions src/Plugin.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@

export default class AntdPlugin {

constructor(libraryName, types) {
constructor(libraryName, libraryDirectory, style, types) {
this.specified = null;
this.libraryObjs = null;
this.selectedMethods = null;
this.libraryName = libraryName;
this.libraryDirectory = libraryDirectory || 'lib';
this.style = style || false;
this.types = types;
}

importMethod(methodName, file, opts) {
if (!this.selectedMethods[methodName]) {
const { libDir = 'lib', style } = opts;
const path = `${this.libraryName}/${libDir}/${camel2Dash(methodName)}`;
const libraryDirectory = this.libraryDirectory;
const style = this.style;
const path = `${this.libraryName}/${libraryDirectory}/${camel2Dash(methodName)}`;
this.selectedMethods[methodName] = file.addImport(path, 'default');
if (style === true) {
file.addImport(`${path}/style`);
Expand Down
32 changes: 23 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
import Plugin from './Plugin';

export default function ({ types }) {
const instances = [
new Plugin('antd', types),
new Plugin('antd-mobile', types),
];
let plugins = null;

// For test
global.__clearBabelAntdPlugin = () => {
plugins = null;
};

function applyInstance(method, args, context) {
for (const instance of instances) {
if (instance[method]) {
instance[method].apply(instance, [...args, context]);
for (const plugin of plugins) {
if (plugin[method]) {
plugin[method].apply(plugin, [...args, context]);
}
}
}

return {
visitor: {
Program() {
Program(path, { opts }) {
if (!plugins) {
if (Array.isArray(opts)) {
plugins = opts.map(({ libraryName, libraryDirectory, style }) =>
new Plugin(libraryName, libraryDirectory, style, types)
);
} else {
opts = opts || {};
plugins = [
new Plugin(opts.libraryName || 'antd', opts.libraryDirectory || opts.libDir, opts.style, types)
];
}
}
applyInstance('Program', arguments, this);
},
ImportDeclaration(path, node) {
ImportDeclaration() {
applyInstance('ImportDeclaration', arguments, this);
},
CallExpression() {
Expand Down
19 changes: 15 additions & 4 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import expect from 'expect';

describe('index', () => {

afterEach(() => {
global.__clearBabelAntdPlugin();
});

const fixturesDir = join(__dirname, 'fixtures');
let fixtures = readdirSync(fixturesDir);
const onlyFixtures = fixtures.filter(fixture => fixture.indexOf('-only') > -1);
Expand All @@ -20,12 +24,19 @@ describe('index', () => {
const expectedFile = join(fixtureDir, 'expected.js');

it(`should work with ${caseName.split('-').join(' ')}`, () => {

let pluginWithOpts;
caseName = caseName.replace(/-only$/, '');
if (caseName === 'import-css') {
pluginWithOpts = [plugin, {
style: true,
}];
pluginWithOpts = [
plugin, { style: true }
];
} else if (caseName === 'multiple-libraries') {
pluginWithOpts = [
plugin, [
{ libraryName: 'antd' },
{ libraryName: 'antd-mobile' },
]
];
}

const actual = transformFileSync(actualFile, {
Expand Down

0 comments on commit fa2cb8f

Please sign in to comment.