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

Support multiple libraries #50

Merged
merged 2 commits into from
Aug 8, 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
147 changes: 147 additions & 0 deletions src/Plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@

export default class AntdPlugin {

constructor(libraryName, types) {
this.specified = null;
this.libraryObjs = null;
this.selectedMethods = null;
this.libraryName = libraryName;
this.types = types;
}

importMethod(methodName, file, opts) {
if (!this.selectedMethods[methodName]) {
const { libDir = 'lib', style } = opts;
const path = `${this.libraryName}/${libDir}/${camel2Dash(methodName)}`;
this.selectedMethods[methodName] = file.addImport(path, 'default');
if (style === true) {
file.addImport(`${path}/style`);
} else if(style === 'css') {
file.addImport(`${path}/style/css`);
}
}
return this.selectedMethods[methodName];
}

buildExpressionHandler(node, props, path, opts) {
const { file } = path.hub;
const types = this.types;
props.forEach(prop => {
if (!types.isIdentifier(node[prop])) return;
if (this.specified[node[prop].name]) {
node[prop] = this.importMethod(node[prop].name, file, opts);
}
});
}

buildDeclaratorHandler(node, prop, path, opts) {
const { file } = path.hub;
const types = this.types;
if (!types.isIdentifier(node[prop])) return;
if (this.specified[node[prop].name]) {
node[prop] = this.importMethod(node[prop].name, file, opts);
}
}

Program() {
this.specified = Object.create(null);
this.libraryObjs = Object.create(null);
this.selectedMethods = Object.create(null);
}

ImportDeclaration(path, { opts }) {
const { node } = path;

// path maybe removed by prev instances.
if (!node) return;

const { value } = node.source;
const libraryName = this.libraryName;
const types = this.types;
if (value === libraryName) {
node.specifiers.forEach(spec => {
if (types.isImportSpecifier(spec)) {
this.specified[spec.local.name] = spec.imported.name;
} else {
this.libraryObjs[spec.local.name] = true;
}
});
path.remove();
}
}

CallExpression(path, { opts }) {
const { node } = path;
const { file } = path.hub;
const { name, object, property } = node.callee;
const types = this.types;

if (types.isIdentifier(node.callee)) {
if (this.specified[name]) {
node.callee = this.importMethod(this.specified[name], file, opts);
}
} else {
// React.createElement(Button) -> React.createElement(_Button)
// if (object && object.name === 'React' && property && property.name === 'createElement' && node.arguments) {
node.arguments = node.arguments.map(arg => {
const { name: argName } = arg;
if (this.specified[argName] &&
path.scope.hasBinding(argName) &&
path.scope.getBinding(argName).path.type === 'ImportSpecifier') {
return this.importMethod(this.specified[argName], file, opts);
}
return arg;
});
// }
}
}

MemberExpression(path, { opts }) {
const { node } = path;
const { file } = path.hub;

// multiple instance check.
if (!node.object || !node.object.name) return;

if (this.libraryObjs[node.object.name]) {
// antd.Button -> _Button
path.replaceWith(this.importMethod(node.property.name, file, opts));
} else if (this.specified[node.object.name]) {
node.object = this.importMethod(this.specified[node.object.name], file, opts);
}
}

Property(path, {opts}) {
const { node } = path;
this.buildDeclaratorHandler(node, 'value', path, opts);
}

VariableDeclarator(path, {opts}) {
const { node } = path;
this.buildDeclaratorHandler(node, 'init', path, opts);
}

LogicalExpression(path, {opts}) {
const { node } = path;
this.buildExpressionHandler(node, ['left', 'right'], path, opts);
}

ConditionalExpression(path, {opts}) {
const { node } = path;
this.buildExpressionHandler(node, ['test', 'consequent', 'alternate'], path, opts);
}

IfStatement(path, {opts}) {
const { node } = path;
this.buildExpressionHandler(node, ['test'], path, opts);
this.buildExpressionHandler(node.test, ['left', 'right'], path, opts);
}
}


function camel2Dash(_str) {
const str = _str[0].toLowerCase() + _str.substr(1);
return str.replace(/([A-Z])/g, function camel2DashReplace($1) {
return '-' + $1.toLowerCase();
});
}
137 changes: 0 additions & 137 deletions src/core.js

This file was deleted.

49 changes: 48 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
import Plugin from './Plugin';

export default require('./core')('antd');
export default function ({ types }) {
const instances = [
new Plugin('antd', types),
new Plugin('antd-mobile', types),
];

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

return {
visitor: {
Program() {
applyInstance('Program', arguments, this);
},
ImportDeclaration(path, node) {
applyInstance('ImportDeclaration', arguments, this);
},
CallExpression() {
applyInstance('CallExpression', arguments, this);
},
MemberExpression() {
applyInstance('MemberExpression', arguments, this);
},
Property() {
applyInstance('Property', arguments, this);
},
VariableDeclarator() {
applyInstance('VariableDeclarator', arguments, this);
},
LogicalExpression() {
applyInstance('LogicalExpression', arguments, this);
},
ConditionalExpression() {
applyInstance('ConditionalExpression', arguments, this);
},
IfStatement() {
applyInstance('IfStatement', arguments, this);
},
},
};

}
5 changes: 5 additions & 0 deletions test/fixtures/multiple-libraries/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Select } from 'antd';
import { Select as SelectMobile } from 'antd-mobile';

if (Select) {}
if (SelectMobile) {}
14 changes: 14 additions & 0 deletions test/fixtures/multiple-libraries/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var _selectMobile = require('antd-mobile/lib/select-mobile');

var _selectMobile2 = _interopRequireDefault(_selectMobile);

var _select = require('antd/lib/select');

var _select2 = _interopRequireDefault(_select);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

if (_select2.default) {}
if (_selectMobile2.default) {}