Skip to content

fix: Properly handle reexports when detecting CJS exports#69

Merged
Mrtenz merged 2 commits intomainfrom
fb/handle-reexports
Dec 1, 2024
Merged

fix: Properly handle reexports when detecting CJS exports#69
Mrtenz merged 2 commits intomainfrom
fb/handle-reexports

Conversation

@FrederikBolding
Copy link
Copy Markdown
Contributor

@FrederikBolding FrederikBolding commented Dec 1, 2024

Fixes a bug where reexports were not properly traversed and therefore not included in the return value from getImports. This would cause issues with importing from packages that use the __exportStar pattern. For these packages cjs-module-lexer returns relative paths to the exports that must be traversed recursively to fully uncover the exports (at least this is what I gathered from https://github.com/nodejs/node/blob/3fb2ea8689250a3d39d9c66afd7301c7caa644c2/lib/internal/modules/esm/translators.js#L313-L346).

Sanity check welcome!

packageSpecifier,
parentUrl,
system,
// We assume that if the packageSpecifier is relative, we are traversing a specific file looking for CJS imports
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We aren't interested in TypeScript files since those aren't marked as commonjs and won't be parsable

const resolvedReexports = reexports.flatMap((reexport: string) =>
getCommonJsExports(reexport, system, path),
);
return [...new Set([...exports, ...resolvedReexports])];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it necessary to turn this back into an array? Can we just return a Set?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Definitely, I was just keeping the existing API, but we can change the return type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fine to change it since it's an internal API.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

return [...exports, ...reexports];

// Re-exports are paths to exports that must be resolved themselves
const resolvedReexports = reexports.flatMap((reexport: string) =>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should we account for export cycles here? Not sure if those realistically will occur

@FrederikBolding FrederikBolding marked this pull request as ready for review December 1, 2024 16:41
@FrederikBolding FrederikBolding requested a review from a team as a code owner December 1, 2024 16:41
@Mrtenz Mrtenz merged commit ce33946 into main Dec 1, 2024
@Mrtenz Mrtenz deleted the fb/handle-reexports branch December 1, 2024 20:44
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.

2 participants