-
-
Notifications
You must be signed in to change notification settings - Fork 611
fix(commonjs): handle node: builtins with strictRequires: auto
#1930
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
Merged
shellscape
merged 1 commit into
rollup:master
from
danielroe:fix/commonjs-strict-requires
Oct 24, 2025
+57
−7
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -196,16 +196,21 @@ export function getRequireResolver(extensions, detectCyclesAndConditional, curre | |
| // Special-case external Node built-ins to be handled via a lazy __require | ||
| // helper instead of hoisted ESM imports when strict wrapping is used. | ||
| const isExternalWrapped = isWrappedId(dependencyId, EXTERNAL_SUFFIX); | ||
| if ( | ||
| parentMeta.initialCommonJSType === IS_WRAPPED_COMMONJS && | ||
| !allowProxy && | ||
| isExternalWrapped | ||
| ) { | ||
| let resolvedDependencyId = dependencyId; | ||
| if (parentMeta.isCommonJS === IS_WRAPPED_COMMONJS && !allowProxy && isExternalWrapped) { | ||
| const actualExternalId = unwrapId(dependencyId, EXTERNAL_SUFFIX); | ||
| if (actualExternalId.startsWith('node:')) { | ||
| isCommonJS = IS_WRAPPED_COMMONJS; | ||
| parentMeta.isRequiredCommonJS[dependencyId] = isCommonJS; | ||
| } | ||
| } else if (isExternalWrapped && !allowProxy) { | ||
| // If the parent is not wrapped but the dependency is a node: builtin external, | ||
| // unwrap the EXTERNAL_SUFFIX so it's treated as a normal external. | ||
| // This avoids trying to load the lazy __require proxy for non-wrapped contexts. | ||
| const actualExternalId = unwrapId(dependencyId, EXTERNAL_SUFFIX); | ||
| if (actualExternalId.startsWith('node:')) { | ||
| resolvedDependencyId = actualExternalId; | ||
| } | ||
|
Comment on lines
+206
to
+213
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
(so in this case we can remove the suffix) |
||
| } | ||
| const isWrappedCommonJS = isCommonJS === IS_WRAPPED_COMMONJS; | ||
| fullyAnalyzedModules[dependencyId] = true; | ||
|
|
@@ -226,8 +231,8 @@ export function getRequireResolver(extensions, detectCyclesAndConditional, curre | |
| wrappedModuleSideEffects, | ||
| source: sources[index].source, | ||
| id: allowProxy | ||
| ? wrapId(dependencyId, isWrappedCommonJS ? WRAPPED_SUFFIX : PROXY_SUFFIX) | ||
| : dependencyId, | ||
| ? wrapId(resolvedDependencyId, isWrappedCommonJS ? WRAPPED_SUFFIX : PROXY_SUFFIX) | ||
| : resolvedDependencyId, | ||
| isCommonJS | ||
| }; | ||
| }); | ||
|
|
||
13 changes: 13 additions & 0 deletions
13
...ges/commonjs/test/fixtures/function/strict-requires-auto-external-node-builtin/_config.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module.exports = { | ||
| description: 'handles node: builtins correctly with strictRequires: auto', | ||
| pluginOptions: { | ||
| strictRequires: 'auto' | ||
| }, | ||
| exports: (exports, t) => { | ||
| // Should be able to access properties of node:stream | ||
| t.truthy(exports.Readable); | ||
| t.is(typeof exports.Readable, 'function'); | ||
| // Should be able to instantiate | ||
| t.truthy(exports.readable); | ||
| } | ||
| }; |
4 changes: 4 additions & 0 deletions
4
packages/commonjs/test/fixtures/function/strict-requires-auto-external-node-builtin/main.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| const stream = require('node:stream'); | ||
| const readable = new stream.Readable({}); | ||
|
|
||
| module.exports = { Readable: stream.Readable, readable }; |
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
Binary file not shown.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parentMeta.initialCommonJSType === IS_WRAPPED_COMMONJSinstead ofparentMeta.isCommonJS === IS_WRAPPED_COMMONJS. With strictRequires: 'auto', I believe the initial type (before cycle detection) can differ from the final type.