Skip to content

Commit 8aa6447

Browse files
author
Deepak kudi
committed
fix: rewrite paper re-exports in babel plugin
1 parent 8b2b1eb commit 8aa6447

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/babel/__fixtures__/rewrite-imports/code.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ import {
1212
withTheme,
1313
LightTheme,
1414
} from 'react-native-paper';
15+
16+
export { TextInput } from 'react-native-paper';

src/babel/__fixtures__/rewrite-imports/output.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ import { Palette } from "react-native-paper/lib/module/theme/tokens";
88
import { NonExistent, NonExistentSecond as Stuff, LightTheme } from "react-native-paper/lib/module/index.js";
99
import { ThemeProvider } from "react-native-paper/lib/module/core/theming";
1010
import { withTheme } from "react-native-paper/lib/module/core/theming";
11+
export { default as TextInput } from "react-native-paper/lib/module/components/TextInput";

src/babel/index.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,70 @@ module.exports = function rewire(babel, options) {
5656
}, [])
5757
);
5858

59+
path.requeue();
60+
},
61+
ExportNamedDeclaration(path) {
62+
if (
63+
!path.node.source ||
64+
path.node.source.value !== name ||
65+
path.node[SKIP]
66+
) {
67+
return;
68+
}
69+
70+
path.node.source.value = `${name}/${index}`;
71+
path.replaceWithMultiple(
72+
path.node.specifiers.reduce((declarations, specifier) => {
73+
const mapping = mappings[specifier.local.name];
74+
75+
if (mapping) {
76+
const alias = `${name}/${mapping.path}`;
77+
let s;
78+
79+
switch (mapping.name) {
80+
case 'default':
81+
s = t.exportSpecifier(
82+
t.identifier('default'),
83+
t.identifier(specifier.exported.name)
84+
);
85+
break;
86+
case '*':
87+
s = t.exportNamespaceSpecifier(
88+
t.identifier(specifier.exported.name)
89+
);
90+
break;
91+
default:
92+
s = t.exportSpecifier(
93+
t.identifier(mapping.name),
94+
t.identifier(specifier.exported.name)
95+
);
96+
}
97+
98+
declarations.push(
99+
t.exportNamedDeclaration(null, [s], t.stringLiteral(alias))
100+
);
101+
} else {
102+
const previous = declarations.find(
103+
(d) => d.source.value === path.node.source.value
104+
);
105+
106+
if (previous) {
107+
previous.specifiers.push(specifier);
108+
} else {
109+
const node = t.exportNamedDeclaration(
110+
null,
111+
[specifier],
112+
path.node.source
113+
);
114+
node[SKIP] = true;
115+
declarations.push(node);
116+
}
117+
}
118+
119+
return declarations;
120+
}, [])
121+
);
122+
59123
path.requeue();
60124
},
61125
},

0 commit comments

Comments
 (0)