Skip to content

Commit

Permalink
fix: fix importing color with babel plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Apr 18, 2018
1 parent db26d6c commit f50fcca
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
15 changes: 10 additions & 5 deletions scripts/generate-mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: '*',
};
}
});
});
Expand Down
1 change: 1 addition & 0 deletions src/babel/__fixtures__/rewrite-imports/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Button,
CardCover,
Toolbar,
Colors,
NonExistent,
NonExistentSecond as Stuff,
} from 'react-native-paper';
1 change: 1 addition & 0 deletions src/babel/__fixtures__/rewrite-imports/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
25 changes: 16 additions & 9 deletions src/babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f50fcca

Please sign in to comment.