Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update config #52

Merged
merged 7 commits into from
Aug 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
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
23 changes: 17 additions & 6 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,17 +24,24 @@ describe('index', () => {
const expectedFile = join(fixtureDir, 'expected.js');

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

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

const actual = transformFileSync(actualFile, {
presets: ['react'],
plugins: [cssPlugin || plugin],
plugins: [pluginWithOpts || plugin],
}).code;

if (onlyFixtures.length) {
Expand Down