Skip to content

Commit 5c1cc8e

Browse files
committed
fix: make sure only one version of the library is imported
closes #1432
1 parent d2e56ef commit 5c1cc8e

File tree

5 files changed

+11
-13
lines changed

5 files changed

+11
-13
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Material design for React Native",
55
"main": "lib/commonjs/index.js",
66
"module": "lib/module/index.js",
7+
"react-native": "lib/module/index.js",
78
"types": "lib/typescript/src/index.d.ts",
89
"files": [
910
"lib",

scripts/generate-mappings.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ const ast = parser.parse(source, {
2020
],
2121
});
2222

23+
const index = path.dirname(packageJson.module);
2324
const relative = (value /* : string */) =>
24-
path.relative(root, path.resolve(path.dirname(packageJson.module), value));
25+
path.relative(root, path.resolve(index, value));
2526

2627
const mappings = ast.program.body.reduce((acc, declaration, index, self) => {
2728
if (types.isExportNamedDeclaration(declaration)) {
@@ -59,4 +60,4 @@ const mappings = ast.program.body.reduce((acc, declaration, index, self) => {
5960
}, {});
6061

6162
fs.existsSync(path.dirname(output)) || fs.mkdirSync(path.dirname(output));
62-
fs.writeFileSync(output, JSON.stringify(mappings, null, 2));
63+
fs.writeFileSync(output, JSON.stringify({ index, mappings }, null, 2));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ import {
1212
NonExistentSecond as Stuff,
1313
ThemeProvider,
1414
withTheme,
15+
Theme
1516
} from 'react-native-paper';

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import Button from "react-native-paper/lib/module/components/Button";
66
import FAB from "react-native-paper/lib/module/components/FAB/FAB";
77
import Appbar from "react-native-paper/lib/module/components/Appbar/Appbar";
88
import * as Colors from "react-native-paper/lib/module/styles/colors";
9-
import { NonExistent, NonExistentSecond as Stuff } from 'react-native-paper';
9+
import { NonExistent, NonExistentSecond as Stuff, Theme } from "react-native-paper/lib/module";
1010
import { ThemeProvider } from "react-native-paper/lib/module/core/theming";
1111
import { withTheme } from "react-native-paper/lib/module/core/theming";

src/babel/index.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
const mappingsPath =
2-
process.env.NODE_ENV === 'test'
3-
? `../../lib/mappings.json`
4-
: `../../mappings.json`;
5-
const mappings = require(mappingsPath);
1+
const { index, mappings } = require('../../lib/mappings.json');
2+
const { name } = require('../../package.json');
63

74
const SKIP = Symbol('SKIP');
85

@@ -11,19 +8,17 @@ module.exports = function rewire(babel) {
118
return {
129
visitor: {
1310
ImportDeclaration(path) {
14-
if (
15-
path.node.source.value !== 'react-native-paper' ||
16-
path.node[SKIP]
17-
) {
11+
if (path.node.source.value !== name || path.node[SKIP]) {
1812
return;
1913
}
2014

15+
path.node.source.value = `${name}/${index}`;
2116
path.replaceWithMultiple(
2217
path.node.specifiers.reduce((declarations, specifier) => {
2318
const mapping = mappings[specifier.imported.name];
2419

2520
if (mapping) {
26-
const alias = `${path.node.source.value}/${mapping.path}`;
21+
const alias = `${name}/${mapping.path}`;
2722
const identifier = t.identifier(specifier.local.name);
2823

2924
let s;

0 commit comments

Comments
 (0)