Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/6.x/docs/guides/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions src/babel/__fixtures__/default-and-named-imports/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable prettier/prettier */
import Paper, { Button, NonExistent } from 'react-native-paper';
3 changes: 3 additions & 0 deletions src/babel/__fixtures__/default-and-named-imports/output.js
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 2 additions & 0 deletions src/babel/__fixtures__/default-and-namespace-import/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable prettier/prettier */
import Paper, * as All from 'react-native-paper';
2 changes: 2 additions & 0 deletions src/babel/__fixtures__/default-and-namespace-import/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable prettier/prettier */
import Paper, * as All from "react-native-paper/lib/module/index.js";
2 changes: 2 additions & 0 deletions src/babel/__fixtures__/default-import/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable prettier/prettier */
import Paper from 'react-native-paper';
2 changes: 2 additions & 0 deletions src/babel/__fixtures__/default-import/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable prettier/prettier */
import Paper from "react-native-paper/lib/module/index.js";
2 changes: 2 additions & 0 deletions src/babel/__fixtures__/namespace-import/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable prettier/prettier */
import * as Paper from 'react-native-paper';
2 changes: 2 additions & 0 deletions src/babel/__fixtures__/namespace-import/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable prettier/prettier */
import * as Paper from "react-native-paper/lib/module/index.js";
7 changes: 6 additions & 1 deletion src/babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
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;

Check warning on line 25 in src/babel/index.js

View check run for this annotation

apex-sentinel-staging / Apex Sentinel

minor: Default import of react-native-paper now compiles to an always-undefined binding

`import Paper from 'react-native-paper'` compiles and rewrites to `import Paper from "react-native-paper/lib/module/index.js"`, but neither `src/index.tsx` nor `lib/module/index.js` exports default, so `Paper` is `undefined` at runtime — the `Paper.Button is not a component` render error replaces the previous build-time failure. The new guard treats `ImportDefaultSpecifier` the same as a namespace import and keeps it on the index. The `default-import` and `default-and-namespace-import` fixtures lock this output in as expected behavior.
Comment on lines +20 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Minor · Default import of react-native-paper now compiles to an always-undefined binding

import Paper from 'react-native-paper' compiles and rewrites to import Paper from "react-native-paper/lib/module/index.js", but neither src/index.tsx nor lib/module/index.js exports default, so Paper is undefined at runtime — the Paper.Button is not a component render error replaces the previous build-time failure. The new guard treats ImportDefaultSpecifier the same as a namespace import and keeps it on the index. The default-import and default-and-namespace-import fixtures lock this output in as expected behavior.

Suggested change

Either emit a plugin warning when a default specifier for react-native-paper is seen (e.g. path.buildCodeFrameError), or state in the getting-started note that react-native-paper has no default export so import Paper from 'react-native-paper' yields undefined.

Details

persona: javascript, senior-developer

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.


if (mapping) {
const alias = `${name}/${mapping.path}`;
Expand Down
Loading