From f50fccaadce0d5fa95bccaf8a4a49459294692ad Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Wed, 18 Apr 2018 17:41:03 +0200 Subject: [PATCH] fix: fix importing color with babel plugin --- scripts/generate-mappings.js | 15 +++++++---- .../__fixtures__/rewrite-imports/code.js | 1 + .../__fixtures__/rewrite-imports/output.js | 1 + src/babel/index.js | 25 ++++++++++++------- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/scripts/generate-mappings.js b/scripts/generate-mappings.js index 4105388466..c47db76c47 100644 --- a/scripts/generate-mappings.js +++ b/scripts/generate-mappings.js @@ -27,10 +27,12 @@ const relative = (value /* : string */) => const mappings = ast.program.body.reduce((acc, declaration, index, self) => { if (types.isExportNamedDeclaration(declaration)) { if (declaration.source) { - const name = declaration.specifiers.find( - specifier => specifier.local.name === 'default' - ).exported.name; - acc[name] = relative(declaration.source.value); + declaration.specifiers.forEach(specifier => { + acc[specifier.exported.name] = { + path: relative(declaration.source.value), + name: specifier.local.name, + }; + }); } else { declaration.specifiers.forEach(specifier => { const name = specifier.exported.name; @@ -44,7 +46,10 @@ const mappings = ast.program.body.reduce((acc, declaration, index, self) => { s.local.name === specifier.local.name ) ) { - acc[name] = relative(it.source.value); + acc[name] = { + path: relative(it.source.value), + name: '*', + }; } }); }); diff --git a/src/babel/__fixtures__/rewrite-imports/code.js b/src/babel/__fixtures__/rewrite-imports/code.js index ff8ecfc96f..07b19027ce 100644 --- a/src/babel/__fixtures__/rewrite-imports/code.js +++ b/src/babel/__fixtures__/rewrite-imports/code.js @@ -5,6 +5,7 @@ import { Button, CardCover, Toolbar, + Colors, NonExistent, NonExistentSecond as Stuff, } from 'react-native-paper'; diff --git a/src/babel/__fixtures__/rewrite-imports/output.js b/src/babel/__fixtures__/rewrite-imports/output.js index 5e86c81182..9c7c73ddbf 100644 --- a/src/babel/__fixtures__/rewrite-imports/output.js +++ b/src/babel/__fixtures__/rewrite-imports/output.js @@ -4,4 +4,5 @@ import BottomNavigation from 'react-native-paper/src/components/BottomNavigation import Button from 'react-native-paper/src/components/Button'; import CardCover from 'react-native-paper/src/components/Card/CardCover'; import Toolbar from 'react-native-paper/src/components/Toolbar/Toolbar'; +import * as Colors from 'react-native-paper/src/styles/colors'; import { NonExistent, NonExistentSecond as Stuff } from 'react-native-paper'; diff --git a/src/babel/index.js b/src/babel/index.js index 6a1c857f42..844db40d2b 100644 --- a/src/babel/index.js +++ b/src/babel/index.js @@ -20,17 +20,24 @@ module.exports = function rewire(babel) { const mapping = mappings[specifier.imported.name]; if (mapping) { - const alias = `${path.node.source.value}/${mapping}`; + const alias = `${path.node.source.value}/${mapping.path}`; + const identifier = t.identifier(specifier.local.name); + + let s; + + switch (mapping.name) { + case 'default': + s = t.importDefaultSpecifier(identifier); + break; + case '*': + s = t.importNamespaceSpecifier(identifier); + break; + default: + s = t.importSpecifier(identifier, t.identifier(mapping.name)); + } declarations.push( - t.importDeclaration( - [ - t.importDefaultSpecifier( - t.identifier(specifier.local.name) - ), - ], - t.stringLiteral(alias) - ) + t.importDeclaration([s], t.stringLiteral(alias)) ); } else { const previous = declarations.find(