From 6284a4d322d8cfce2cc48c6158abe6c45a043617 Mon Sep 17 00:00:00 2001 From: sorrycc Date: Mon, 8 Aug 2016 18:07:51 +0800 Subject: [PATCH 1/6] update config --- src/Plugin.js | 9 ++++++--- src/index.js | 19 +++++++++++++------ test/index-test.js | 27 ++++++++++++++++++++------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/Plugin.js b/src/Plugin.js index 82412297..56939495 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -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`); diff --git a/src/index.js b/src/index.js index 7549d5cf..020a6687 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,12 @@ import Plugin from './Plugin'; export default function ({ types }) { - const instances = [ - new Plugin('antd', types), - new Plugin('antd-mobile', types), - ]; + let instances = null; + + // For test + global.__clearBabelAntdPlugin = () => { + instances = null; + }; function applyInstance(method, args, context) { for (const instance of instances) { @@ -16,10 +18,15 @@ export default function ({ types }) { return { visitor: { - Program() { + Program(path, { opts }) { + if (!instances) { + instances = opts.map(({ libraryName, libraryDirectory, style }) => + new Plugin(libraryName, libraryDirectory, style, types) + ); + } applyInstance('Program', arguments, this); }, - ImportDeclaration(path, node) { + ImportDeclaration() { applyInstance('ImportDeclaration', arguments, this); }, CallExpression() { diff --git a/test/index-test.js b/test/index-test.js index 4c415d65..1fc5aeae 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -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); @@ -20,20 +24,29 @@ describe('index', () => { const expectedFile = join(fixtureDir, 'expected.js'); it(`should work with ${caseName.split('-').join(' ')}`, () => { - - let pluginWithOpts; + let pluginWithOpts = [ + plugin, [{ libraryName: 'antd' }] + ]; + caseName = caseName.replace(/-only$/, ''); if (caseName === 'import-css') { - pluginWithOpts = [plugin, { - style: true, - }]; + pluginWithOpts = [ + plugin, [{ libraryName: 'antd', style:true }] + ]; + } else if (caseName === 'multiple-libraries') { + pluginWithOpts = [ + plugin, [ + { libraryName: 'antd' }, + { libraryName: 'antd-mobile' }, + ] + ]; } const actual = transformFileSync(actualFile, { presets: ['react'], - plugins: [pluginWithOpts || plugin], + plugins: [pluginWithOpts], }).code; - if (onlyFixtures.length) { + if (onlyFixtures.length || caseName === 'multiple-libraries') { console.warn(); console.warn(actual); } From f91788ba5b39613ad250fe9f5f7abf5158145988 Mon Sep 17 00:00:00 2001 From: sorrycc Date: Mon, 8 Aug 2016 18:11:52 +0800 Subject: [PATCH 2/6] update README.md --- README.md | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0cc56b84..110c8e8a 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,28 @@ 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` is an array. + +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) From 54e20eaa761bfa62a4f112c9b3d645dcb32f1c54 Mon Sep 17 00:00:00 2001 From: sorrycc Date: Mon, 8 Aug 2016 18:27:16 +0800 Subject: [PATCH 3/6] compatible with older versions --- src/index.js | 13 ++++++++++--- test/index-test.js | 10 ++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 020a6687..17c0d84c 100644 --- a/src/index.js +++ b/src/index.js @@ -20,9 +20,16 @@ export default function ({ types }) { visitor: { Program(path, { opts }) { if (!instances) { - instances = opts.map(({ libraryName, libraryDirectory, style }) => - new Plugin(libraryName, libraryDirectory, style, types) - ); + if (Array.isArray(opts)) { + instances = opts.map(({ libraryName, libraryDirectory, style }) => + new Plugin(libraryName, libraryDirectory, style, types) + ); + } else { + opts = opts || {}; + instances = [ + new Plugin('antd', opts.libraryDirectory || opts.libDir, opts.style, types) + ]; + } } applyInstance('Program', arguments, this); }, diff --git a/test/index-test.js b/test/index-test.js index 1fc5aeae..35062837 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -24,13 +24,11 @@ describe('index', () => { const expectedFile = join(fixtureDir, 'expected.js'); it(`should work with ${caseName.split('-').join(' ')}`, () => { - let pluginWithOpts = [ - plugin, [{ libraryName: 'antd' }] - ]; + let pluginWithOpts; caseName = caseName.replace(/-only$/, ''); if (caseName === 'import-css') { pluginWithOpts = [ - plugin, [{ libraryName: 'antd', style:true }] + plugin, { style: true } ]; } else if (caseName === 'multiple-libraries') { pluginWithOpts = [ @@ -43,10 +41,10 @@ describe('index', () => { const actual = transformFileSync(actualFile, { presets: ['react'], - plugins: [pluginWithOpts], + plugins: [pluginWithOpts || plugin], }).code; - if (onlyFixtures.length || caseName === 'multiple-libraries') { + if (onlyFixtures.length) { console.warn(); console.warn(actual); } From 4e59db6fab474465d461f3be038feb65b6c548b2 Mon Sep 17 00:00:00 2001 From: sorrycc Date: Mon, 8 Aug 2016 18:32:50 +0800 Subject: [PATCH 4/6] update README.md --- README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 110c8e8a..51be646f 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,9 @@ Converts ```javascript import { Button } from 'antd'; -import { Button as ButtonMobile } from 'antd-mobile'; ReactDOM.render(
- xxxx
); ``` @@ -30,11 +28,9 @@ ReactDOM.render(
```javascript var _button = require('antd/lib/button'); -var _buttonMobile = require('antd-mobile/lib/button'); ReactDOM.render(
<_button>xxxx - <_buttonMobile>xxxx
); ``` @@ -54,7 +50,16 @@ Via `.babelrc` or babel-loader. ### options -`options` is an array. +`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: From 9361d9eb41e59847c12a7e5a90bed926135dc0d2 Mon Sep 17 00:00:00 2001 From: sorrycc Date: Tue, 9 Aug 2016 07:15:17 +0800 Subject: [PATCH 5/6] support opts.libraryName --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 17c0d84c..6ed1c858 100644 --- a/src/index.js +++ b/src/index.js @@ -27,7 +27,7 @@ export default function ({ types }) { } else { opts = opts || {}; instances = [ - new Plugin('antd', opts.libraryDirectory || opts.libDir, opts.style, types) + new Plugin(opts.libraryName || 'antd', opts.libraryDirectory || opts.libDir, opts.style, types) ]; } } From e1eb8320daadbe6b204daa770174684f09310dd2 Mon Sep 17 00:00:00 2001 From: sorrycc Date: Tue, 9 Aug 2016 07:51:30 +0800 Subject: [PATCH 6/6] rename --- src/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 6ed1c858..336ad2e7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,17 @@ import Plugin from './Plugin'; export default function ({ types }) { - let instances = null; + let plugins = null; // For test global.__clearBabelAntdPlugin = () => { - instances = null; + 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]); } } } @@ -19,14 +19,14 @@ export default function ({ types }) { return { visitor: { Program(path, { opts }) { - if (!instances) { + if (!plugins) { if (Array.isArray(opts)) { - instances = opts.map(({ libraryName, libraryDirectory, style }) => + plugins = opts.map(({ libraryName, libraryDirectory, style }) => new Plugin(libraryName, libraryDirectory, style, types) ); } else { opts = opts || {}; - instances = [ + plugins = [ new Plugin(opts.libraryName || 'antd', opts.libraryDirectory || opts.libDir, opts.style, types) ]; }