diff --git a/docs/6.x/docs/guides/getting-started.mdx b/docs/6.x/docs/guides/getting-started.mdx index 317298838d..26464ea026 100644 --- a/docs/6.x/docs/guides/getting-started.mdx +++ b/docs/6.x/docs/guides/getting-started.mdx @@ -119,7 +119,7 @@ module.exports = function (api) { }; ``` -The plugin only works if you are importing the library using ES2015 import statements and not with `require`. +The plugin only works if you are importing the library using ES2015 import statements and not with `require`. It rewrites named imports, such as `import { Button } from 'react-native-paper'`. A namespace import, such as `import * as Paper from 'react-native-paper'`, still loads the whole library. If your `babel.config.js` also lists `react-native-worklets/plugin`, as Reanimated requires for Community CLI projects, keep it in the top-level `plugins`. Babel adds the `env.production` plugins after it, which is fine because `react-native-paper/babel` only rewrites `import` statements. diff --git a/src/babel/__fixtures__/default-and-named-imports/code.js b/src/babel/__fixtures__/default-and-named-imports/code.js new file mode 100644 index 0000000000..100c951a15 --- /dev/null +++ b/src/babel/__fixtures__/default-and-named-imports/code.js @@ -0,0 +1,2 @@ +/* eslint-disable prettier/prettier */ +import Paper, { Button, NonExistent } from 'react-native-paper'; diff --git a/src/babel/__fixtures__/default-and-named-imports/output.js b/src/babel/__fixtures__/default-and-named-imports/output.js new file mode 100644 index 0000000000..35027927c1 --- /dev/null +++ b/src/babel/__fixtures__/default-and-named-imports/output.js @@ -0,0 +1,3 @@ +/* eslint-disable prettier/prettier */ +import Paper, { NonExistent } from "react-native-paper/lib/module/index.js"; +import Button from "react-native-paper/lib/module/components/Button/Button"; diff --git a/src/babel/__fixtures__/default-and-namespace-import/code.js b/src/babel/__fixtures__/default-and-namespace-import/code.js new file mode 100644 index 0000000000..47879602ad --- /dev/null +++ b/src/babel/__fixtures__/default-and-namespace-import/code.js @@ -0,0 +1,2 @@ +/* eslint-disable prettier/prettier */ +import Paper, * as All from 'react-native-paper'; diff --git a/src/babel/__fixtures__/default-and-namespace-import/output.js b/src/babel/__fixtures__/default-and-namespace-import/output.js new file mode 100644 index 0000000000..3deed04bc5 --- /dev/null +++ b/src/babel/__fixtures__/default-and-namespace-import/output.js @@ -0,0 +1,2 @@ +/* eslint-disable prettier/prettier */ +import Paper, * as All from "react-native-paper/lib/module/index.js"; diff --git a/src/babel/__fixtures__/default-import/code.js b/src/babel/__fixtures__/default-import/code.js new file mode 100644 index 0000000000..f51ad4d6e4 --- /dev/null +++ b/src/babel/__fixtures__/default-import/code.js @@ -0,0 +1,2 @@ +/* eslint-disable prettier/prettier */ +import Paper from 'react-native-paper'; diff --git a/src/babel/__fixtures__/default-import/output.js b/src/babel/__fixtures__/default-import/output.js new file mode 100644 index 0000000000..a24c23a5ef --- /dev/null +++ b/src/babel/__fixtures__/default-import/output.js @@ -0,0 +1,2 @@ +/* eslint-disable prettier/prettier */ +import Paper from "react-native-paper/lib/module/index.js"; diff --git a/src/babel/__fixtures__/namespace-import/code.js b/src/babel/__fixtures__/namespace-import/code.js new file mode 100644 index 0000000000..2f2d9e5605 --- /dev/null +++ b/src/babel/__fixtures__/namespace-import/code.js @@ -0,0 +1,2 @@ +/* eslint-disable prettier/prettier */ +import * as Paper from 'react-native-paper'; diff --git a/src/babel/__fixtures__/namespace-import/output.js b/src/babel/__fixtures__/namespace-import/output.js new file mode 100644 index 0000000000..4d345ffe93 --- /dev/null +++ b/src/babel/__fixtures__/namespace-import/output.js @@ -0,0 +1,2 @@ +/* eslint-disable prettier/prettier */ +import * as Paper from "react-native-paper/lib/module/index.js"; diff --git a/src/babel/index.js b/src/babel/index.js index 2fc478bacb..e735026fdc 100644 --- a/src/babel/index.js +++ b/src/babel/index.js @@ -17,7 +17,12 @@ module.exports = function rewire(babel, options) { path.node.source.value = `${name}/${index}`; path.replaceWithMultiple( path.node.specifiers.reduce((declarations, specifier) => { - const mapping = mappings[specifier.imported.name]; + // Only a named import maps to a module. A namespace or default + // import (`import * as Paper`, `import Paper`) has no `imported` + // name, so it stays on the index. + const mapping = t.isImportSpecifier(specifier) + ? mappings[specifier.imported.name] + : undefined; if (mapping) { const alias = `${name}/${mapping.path}`;