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

Support node: prefix for CJS dependencies #9244

Merged
merged 1 commit into from Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/utils/node-resolver-rs/src/lib.rs
Expand Up @@ -2297,6 +2297,18 @@ mod tests {
.0,
Resolution::Builtin("zlib".into())
);
assert_eq!(
test_resolver()
.resolve(
"node:fs/promises",
&root().join("foo.js"),
SpecifierType::Cjs
)
.result
.unwrap()
.0,
Resolution::Builtin("fs/promises".into())
);
}

#[test]
Expand Down
5 changes: 3 additions & 2 deletions packages/utils/node-resolver-rs/src/specifier.rs
Expand Up @@ -136,8 +136,9 @@ impl<'a> Specifier<'a> {
}
}
SpecifierType::Cjs => {
if BUILTINS.contains(&specifier) {
(Specifier::Builtin(Cow::Borrowed(specifier)), None)
let builtin = specifier.strip_prefix("node:").unwrap_or(specifier);
if BUILTINS.contains(&builtin) {
(Specifier::Builtin(Cow::Borrowed(builtin)), None)
Comment on lines +139 to +141

Choose a reason for hiding this comment

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

If specifier starts with "node:" then it should always be treated as a builtin, that was why Node introduced it in the first place (and also parity with ESM):

} else {
#[cfg(windows)]
if !flags.contains(Flags::ABSOLUTE_SPECIFIERS) {
Expand Down