-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
--defaultIsModuleExports
and --allowRequireESM
#58526
Closed
andrewbranch
wants to merge
15
commits into
microsoft:main
from
andrewbranch:feature/54102-alternate
Closed
--defaultIsModuleExports
and --allowRequireESM
#58526
andrewbranch
wants to merge
15
commits into
microsoft:main
from
andrewbranch:feature/54102-alternate
Conversation
This file contains 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
…require(esm) error
typescript-bot
added
Author: Team
For Milestone Bug
PRs that fix a bug with a specific milestone
labels
May 13, 2024
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. |
andrewbranch
changed the title
Feature/54102 alternate
May 13, 2024
--defaultIsModuleExports
and --allowRequireESM
- module: bundler
+ module: { defaultIsModuleExports: true, allowRequireESM: true } to allow fine-tuning? |
|
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.
Fixes #54102
Alternate to #58480
This splits the behavior of one flag in #58480 into two:
defaultIsModuleExports
Named for the
@rollup/plugin-commonjs
option, this setting describes the behavior of imports of CommonJS modules in the target runtime/bundler. Note that this flag only takes effect for imports that would, according to emit settings, still be imports in the output JavaScript. It does not affect imports that will be emitted asrequire
statements—that’s whatesModuleInterop
controls. The available values are:node16
/nodenext
: The existing behavior in--module node16
/nodenext
. Imports (in true ESM files) of CommonJS files always come with a synthesized default export; i.e., a default import ismodule.exports
.auto
: The existing behavior in all other module modes. Imports of CommonJS files change types based on heuristics related to the presence/absence of__esModule
.Why
"node16"
/"nodenext"
instead of justtrue
? Imagine a default import of a CommonJS module in a.ts
file with no package.json. In--module nodenext
, that file would emit as a CommonJS.js
file, because it lacks any indicator that it’s ESM. Because of that module format detection policy, such an import is not affected by--defaultIsModuleExports
—it’s effectively arequire
. So when--module
isnodenext
,--defaultIsModuleExports nodenext
is equivalent to saying “default imports are alwaysmodule.exports
,” which sounds liketrue
would be a good value for the setting. However, the same import in the same file, under--module preserve
, is not effectively arequire
. It is a real ESM import that exists in a file that is not explicitly marked as ESM. In esbuild, that import behaves likeauto
(it would conditionally bemodule.exports
ormodule.exports.default
), but if the file were renamed to have a.mts
extension or a package.json with"type": "module"
were added, it would behave likenodenext
(it would always bemodule.exports
). So--defaultIsModuleExports nodenext
doesn’t mean “always”; it means “in files that Node.js would recognize as ESM,” which in--module nodenext
is all files where the setting could apply, but in--module esnext
or--module preserve
is a subset of files where the setting could apply. In fewer words:module
defaultIsModuleExports
nodenext
nodenext
preserve
preserve
allowRequireESM
Describes whether
require
calls are allowed to resolve to ES modules in the target runtime/bundler. The available values arenever
,always
, andnode16
/nodenext
. Again,node16
/nodenext
has the specific meaning of “requires of files that Node.js would recognize as an ES module.” I thought this was what Webpack does, but I misremembered and may simplify/adjust the option values here.