Skip to content

fix: rewrite paper re-exports in babel plugin - #4969

Open
puneetdixit200 wants to merge 1 commit into
callstack:mainfrom
puneetdixit200:fix-babel-reexport-rewrite
Open

puneetdixit200 wants to merge 1 commit into
callstack:mainfrom
puneetdixit200:fix-babel-reexport-rewrite

Conversation

@puneetdixit200

Copy link
Copy Markdown

Motivation

The Babel plugin rewrites imports from react-native-paper to component-specific paths, but it currently leaves re-exports unchanged. That means a barrel like:

export { TextInput } from 'react-native-paper';

still points at the package root instead of the mapped component path.

This adds the same mapping behavior for named re-exports while preserving unmapped symbols on the generated index path.

Related issue

Fixes #4874

Test plan

  • yarn test src/babel/__tests__/index.js --runInBand
  • yarn eslint src/babel/index.js src/babel/__tests__/index.js --max-warnings=0
  • git diff --check
  • Pre-commit hook full Jest run: 55 suites passed, 738 tests passed, 1 skipped

Copilot AI review requested due to automatic review settings June 4, 2026 04:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support to the Babel “rewire” transform to rewrite export { ... } from '<pkg>' re-exports (not just import ... from '<pkg>') into the appropriate deep export paths based on the existing mappings.

Changes:

  • Add an ExportNamedDeclaration visitor that rewrites re-exports from the configured package to mapped deep paths or the package index.
  • Preserve/export-alias semantics for mapped exports (including default / namespace cases).
  • Update rewrite-imports fixtures to cover a re-export case.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
src/babel/index.js Adds re-export rewriting logic for ExportNamedDeclaration nodes.
src/babel/fixtures/rewrite-imports/code.js Adds an input example that re-exports TextInput from the package.
src/babel/fixtures/rewrite-imports/output.js Updates expected output to a deep re-export (default as TextInput).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/babel/index.js
Comment on lines +72 to +73
path.node.specifiers.reduce((declarations, specifier) => {
const mapping = mappings[specifier.local.name];
Comment thread src/babel/index.js
path.node.source.value = `${name}/${index}`;
path.replaceWithMultiple(
path.node.specifiers.reduce((declarations, specifier) => {
const mapping = mappings[specifier.local.name];
Comment thread src/babel/index.js
case 'default':
s = t.exportSpecifier(
t.identifier('default'),
t.identifier(specifier.exported.name)
Comment thread src/babel/index.js
default:
s = t.exportSpecifier(
t.identifier(mapping.name),
t.identifier(specifier.exported.name)
Comment thread src/babel/index.js
Comment on lines +71 to +121
path.replaceWithMultiple(
path.node.specifiers.reduce((declarations, specifier) => {
const mapping = mappings[specifier.local.name];

if (mapping) {
const alias = `${name}/${mapping.path}`;
let s;

switch (mapping.name) {
case 'default':
s = t.exportSpecifier(
t.identifier('default'),
t.identifier(specifier.exported.name)
);
break;
case '*':
s = t.exportNamespaceSpecifier(
t.identifier(specifier.exported.name)
);
break;
default:
s = t.exportSpecifier(
t.identifier(mapping.name),
t.identifier(specifier.exported.name)
);
}

declarations.push(
t.exportNamedDeclaration(null, [s], t.stringLiteral(alias))
);
} else {
const previous = declarations.find(
(d) => d.source.value === path.node.source.value
);

if (previous) {
previous.specifiers.push(specifier);
} else {
const node = t.exportNamedDeclaration(
null,
[specifier],
path.node.source
);
node[SKIP] = true;
declarations.push(node);
}
}

return declarations;
}, [])
);

@xdimota7x xdimota7x left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for picking this up! We're fixing the related import crash in #5227 and reviewed this in the same pass. Some blockers:

  1. export { Avatar } (also others like List, Drawer) is rewritten to export * as …, and with @react-native/babel-preset the build then fails with "Export namespace should be first transformed by @babel/plugin-transform-export-namespace-from". Emitting an import plus a named export instead, or keeping those names on the index, avoids it.
  2. export * as Paper from 'react-native-paper' and string-named exports (export { Button as 'paper-button' }) crash, as Copilot noted. Only ExportSpecifier should be mapped, with names read through t.isIdentifier(n) ? n.name : n.value.
  3. export type { … } and inline type specifiers lose their type, so a type-only re-export becomes a runtime require (of the whole index for unmapped types). Consider skipping exportKind === 'type'.
  4. Please put the re-export cases in their own fixture folders. rewrite-imports/ is also changed by #5006.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

babel plugin does not handle re-exports, causes incorrect theming

3 participants