diff --git a/.gitignore b/.gitignore index fc3495b2..885756bd 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ coverage lib *.log *.log.* +.vscode diff --git a/src/Plugin.js b/src/Plugin.js index 95259b13..1bf31d2b 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -52,11 +52,33 @@ export default class Plugin { this.pluginStateKey = `importPluginState${index}`; } - getPluginState(state) { - if (!state[this.pluginStateKey]) { - state[this.pluginStateKey] = {}; // eslint-disable-line + transformFilename(methodName) { + // eslint-disable-next-line no-nested-ternary + return this.camel2UnderlineComponentName + ? transCamel(methodName, '_') + : this.camel2DashComponentName + ? transCamel(methodName, '-') + : methodName; + } + + getInjectStylePath(path, file, transformedMethodName) { + let stylePath; + const { style } = this; + + if (this.customStyleName) { + stylePath = winPath(this.customStyleName(transformedMethodName)); + } else if (this.styleLibraryDirectory) { + stylePath = winPath( + join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName), + ); + } else if (style === true) { + stylePath = `${path}/style`; + } else if (style === 'css') { + stylePath = `${path}/style/css`; + } else if (typeof style === 'function') { + stylePath = style(path, file); } - return state[this.pluginStateKey]; + return stylePath; } importMethod(methodName, file, pluginState) { @@ -97,6 +119,13 @@ export default class Plugin { return { ...pluginState.selectedMethods[methodName] }; } + getPluginState(state) { + if (!state[this.pluginStateKey]) { + state[this.pluginStateKey] = {}; // eslint-disable-line + } + return state[this.pluginStateKey]; + } + buildExpressionHandler(node, props, path, state) { const file = (path && path.hub && path.hub.file) || (state && state.file); const { types } = this; @@ -145,45 +174,55 @@ export default class Plugin { if (!node) return; const { value } = node.source; - const { libraryName } = this; - const { types } = this; + const { libraryName, libraryDirectory, types } = this; const pluginState = this.getPluginState(state); if (value === libraryName) { - node.specifiers.forEach(spec => { - if (types.isImportSpecifier(spec)) { - pluginState.specified[spec.local.name] = spec.imported.name; - } else { - pluginState.libraryObjs[spec.local.name] = true; - } - }); - pluginState.pathsToRemove.push(path); - } - } - - CallExpression(path, state) { - const { node } = path; - const file = (path && path.hub && path.hub.file) || (state && state.file); - const { name } = node.callee; - const { types } = this; - const pluginState = this.getPluginState(state); - - if (types.isIdentifier(node.callee)) { - if (pluginState.specified[name]) { - node.callee = this.importMethod(pluginState.specified[name], file, pluginState); - } + const newImports = node.specifiers + .map(item => { + /** + * LibraryObjs for @MemberExpression shaking, like + * import antd from 'antd' + * + * + * to + * + * var Button = require("antd/lib/button") + *