@talend/babel-plugin-import-from-index
Advanced tools
Comparing version 0.1.2 to 0.1.3
{ | ||
"name": "@talend/babel-plugin-import-from-index", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Transform default imports from specific path to named import from index", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -11,3 +11,3 @@ const PACKAGE_NAME = '@talend/react-components'; | ||
Program(path) { | ||
const imports = []; | ||
const importSpecifiers = []; | ||
let lastImport; | ||
@@ -19,7 +19,15 @@ | ||
if (matches(importDeclarationPath)) { | ||
imports.push( | ||
...importDeclarationPath.node.specifiers.map(({ local, imported }) => ({ | ||
local: local.name, | ||
imported: imported ? imported.name : local.name, | ||
})), | ||
importSpecifiers.push( | ||
...importDeclarationPath.node.specifiers.map(({ local, imported, type }) => { | ||
const localName = local.name; | ||
if (type === 'ImportNamespaceSpecifier') { | ||
return types.importNamespaceSpecifier(types.identifier(localName)); | ||
} | ||
const importedName = imported ? imported.name : localName; | ||
return types.importSpecifier( | ||
types.identifier(localName), | ||
types.identifier(importedName), | ||
); | ||
}), | ||
); | ||
@@ -33,8 +41,5 @@ importDeclarationPath.remove(); | ||
if (imports.length && lastImport) { | ||
if (importSpecifiers.length && lastImport) { | ||
const source = types.stringLiteral(PACKAGE_NAME); | ||
const specifiers = imports.map(({ local, imported }) => | ||
types.importSpecifier(types.identifier(local), types.identifier(imported)), | ||
); | ||
const imp = types.importDeclaration(specifiers, source); | ||
const imp = types.importDeclaration(importSpecifiers, source); | ||
lastImport.insertAfter(imp); | ||
@@ -41,0 +46,0 @@ } |
@@ -31,3 +31,17 @@ import pluginTester from 'babel-plugin-tester'; | ||
}, | ||
{ | ||
code: | ||
"import { RecordsViewer as RecordsViewerComponent, TooltipTrigger } from '@talend/react-components';", | ||
output: | ||
"import { RecordsViewer as RecordsViewerComponent, TooltipTrigger } from '@talend/react-components';", | ||
}, | ||
{ | ||
code: "import * as allComponents from '@talend/react-components';", | ||
output: "import * as allComponents from '@talend/react-components';", | ||
}, | ||
{ | ||
code: "import * as allComponents from '@talend/react-components';", | ||
output: "import * as allComponents from '@talend/react-components';", | ||
}, | ||
], | ||
}); |
4671
87