fix: rewrite paper re-exports in babel plugin#4969
Open
puneetdixit200 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
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
ExportNamedDeclarationvisitor 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 on lines
+72
to
+73
| path.node.specifiers.reduce((declarations, specifier) => { | ||
| const mapping = mappings[specifier.local.name]; |
| path.node.source.value = `${name}/${index}`; | ||
| path.replaceWithMultiple( | ||
| path.node.specifiers.reduce((declarations, specifier) => { | ||
| const mapping = mappings[specifier.local.name]; |
| case 'default': | ||
| s = t.exportSpecifier( | ||
| t.identifier('default'), | ||
| t.identifier(specifier.exported.name) |
| default: | ||
| s = t.exportSpecifier( | ||
| t.identifier(mapping.name), | ||
| t.identifier(specifier.exported.name) |
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; | ||
| }, []) | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The Babel plugin rewrites imports from
react-native-paperto component-specific paths, but it currently leaves re-exports unchanged. That means a barrel like: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 --runInBandyarn eslint src/babel/index.js src/babel/__tests__/index.js --max-warnings=0git diff --check