Skip to content
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

--moduleFormatInterop (all names bikesheddable) #58480

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

andrewbranch
Copy link
Member

@andrewbranch andrewbranch commented May 8, 2024

Fixes #54102

Adds a flag --moduleFormatInterop to describe how imports of CommonJS modules and requires of ES modules work in the target runtime/bundler. The values are:

  • node16/nodenext: the current behavior in --module node16/nodenext. That is, a default import of a CommonJS module always binds to module.exports.
  • bundler: the current behavior in all other module modes. Default imports of CommonJS modules bind to either module.exports or module.exports.default, depending on heuristics related to the presence/absence of __esModule.
  • bundlernode: a behavior not achievable without this PR, which is required for good compatibility with Webpack and esbuild. For compatibility reasons, those bundlers use Node.js’s logic for importing CommonJS modules when the importing file would be interpreted as ESM by Node.js due to file extension or package.json "type". I explained this in more detail in a demo repository. This mode toggles the typing of imports based on module format detection, but continues to allow require(esm). (Note this is not a perfect fit for Webpack, which also prohibits require(esm) under the same Node.js compatibility conditions.)

An alternate approach splitting import typing and require(esm) behavior is up at #58526.

@typescript-bot
Copy link
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

moduleResolution: bundler diverges from bundlers following node semantics in regards to default import
2 participants