Skip to content

feat(compartment-mapper): support treating top-level return in CJS as a way to export from module #2762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

naugtur
Copy link
Member

@naugtur naugtur commented Apr 8, 2025

Description

One of the rarely used ways to export from a CJS module is to return a reference instead of setting module.exports to that reference. This PR adds support for that behavior and a tiny fixture in tests to ensure it works in all scenarios.

Security Considerations

N/A

Scaling Considerations

N/A

Documentation Considerations

It's part of CJS and also something very few people know. Documenting it would lead to more confusion than not.

Testing Considerations

Covered with a case in existing test coverage.

Compatibility Considerations

Increases ecosystem compatibility by the few unfortunate packages that use this way of exporting.

Upgrade Considerations

None

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

@naugtur naugtur force-pushed the naugtur/cjs-top-level-return-exports branch from 2a54916 to 81e0c45 Compare April 8, 2025 11:29
@endojs endojs deleted a comment from Copilot AI Apr 8, 2025
@naugtur naugtur requested a review from Copilot April 8, 2025 14:02
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

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

Comments suppressed due to low confidence (3)

packages/compartment-mapper/src/parse-pre-cjs.js:51

  • Ensure that the test suite includes scenarios where functor.call returns undefined, so that the fallback to module.exports is properly validated.
const returnValue = functor.call(

packages/compartment-mapper/src/parse-cjs.js:58

  • Verify that tests cover cases when functor.call returns a defined value versus undefined, ensuring consistency in export behavior.
const returnValue = functor.call(

packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js:187

  • Confirm that tests validate both branches of the conditional that selects returnValue or module.exports, ensuring that shared export logic behaves as expected.
const afterExecute = returnValue => {

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.

1 participant