mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 20:29:11 +00:00
fix(ext/node): make conditional exports work in require()
(#29640)
This commit fixes passing conditional exports specified with `--unstable-node-conditions` flag to `require()` calls. Fixes scenario from https://github.com/denoland/deno/issues/23757#issuecomment-2949344042 --------- Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
This commit is contained in:
parent
d5f0dd7ca2
commit
ccc30edcb5
8 changed files with 29 additions and 4 deletions
|
@ -26,7 +26,6 @@ use node_resolver::NpmPackageFolderResolver;
|
||||||
use node_resolver::ResolutionMode;
|
use node_resolver::ResolutionMode;
|
||||||
use node_resolver::UrlOrPath;
|
use node_resolver::UrlOrPath;
|
||||||
use node_resolver::UrlOrPathRef;
|
use node_resolver::UrlOrPathRef;
|
||||||
use node_resolver::REQUIRE_CONDITIONS;
|
|
||||||
use sys_traits::FsCanonicalize;
|
use sys_traits::FsCanonicalize;
|
||||||
use sys_traits::FsMetadata;
|
use sys_traits::FsMetadata;
|
||||||
use sys_traits::FsMetadataValue;
|
use sys_traits::FsMetadataValue;
|
||||||
|
@ -534,7 +533,7 @@ pub fn op_require_try_self<
|
||||||
exports,
|
exports,
|
||||||
Some(&referrer),
|
Some(&referrer),
|
||||||
ResolutionMode::Require,
|
ResolutionMode::Require,
|
||||||
REQUIRE_CONDITIONS,
|
node_resolver.require_conditions(),
|
||||||
NodeResolutionKind::Execution,
|
NodeResolutionKind::Execution,
|
||||||
)?;
|
)?;
|
||||||
Ok(Some(url_or_path_to_string(r)?))
|
Ok(Some(url_or_path_to_string(r)?))
|
||||||
|
@ -641,7 +640,7 @@ pub fn op_require_resolve_exports<
|
||||||
.map(|r| UrlOrPathRef::from_path(r))
|
.map(|r| UrlOrPathRef::from_path(r))
|
||||||
.as_ref(),
|
.as_ref(),
|
||||||
ResolutionMode::Require,
|
ResolutionMode::Require,
|
||||||
REQUIRE_CONDITIONS,
|
node_resolver.require_conditions(),
|
||||||
NodeResolutionKind::Execution,
|
NodeResolutionKind::Execution,
|
||||||
)?;
|
)?;
|
||||||
Ok(Some(url_or_path_to_string(r)?))
|
Ok(Some(url_or_path_to_string(r)?))
|
||||||
|
@ -720,7 +719,7 @@ pub fn op_require_package_imports_resolve<
|
||||||
Some(&UrlOrPathRef::from_path(&referrer_path)),
|
Some(&UrlOrPathRef::from_path(&referrer_path)),
|
||||||
ResolutionMode::Require,
|
ResolutionMode::Require,
|
||||||
Some(&pkg),
|
Some(&pkg),
|
||||||
REQUIRE_CONDITIONS,
|
node_resolver.require_conditions(),
|
||||||
NodeResolutionKind::Execution,
|
NodeResolutionKind::Execution,
|
||||||
)?;
|
)?;
|
||||||
Ok(Some(url_or_path_to_string(url)?))
|
Ok(Some(url_or_path_to_string(url)?))
|
||||||
|
|
|
@ -137,6 +137,10 @@ impl ConditionResolver {
|
||||||
ResolutionMode::Require => &self.require_conditions,
|
ResolutionMode::Require => &self.require_conditions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn require_conditions(&self) -> &[Cow<'static, str>] {
|
||||||
|
&self.require_conditions
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
|
@ -310,6 +314,10 @@ impl<
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn require_conditions(&self) -> &[Cow<'static, str>] {
|
||||||
|
self.condition_resolver.require_conditions()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn in_npm_package(&self, specifier: &Url) -> bool {
|
pub fn in_npm_package(&self, specifier: &Url) -> bool {
|
||||||
self.in_npm_pkg_checker.in_npm_package(specifier)
|
self.in_npm_pkg_checker.in_npm_package(specifier)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"args": "run --unstable-node-conditions some-condition main.cjs",
|
||||||
|
"output": "good\n"
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"nodeModulesDir": "manual"
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
require("foo");
|
1
tests/specs/run/conditional_exports_from_require/node_modules/foo/bad.js
generated
vendored
Normal file
1
tests/specs/run/conditional_exports_from_require/node_modules/foo/bad.js
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
console.log("bad");
|
1
tests/specs/run/conditional_exports_from_require/node_modules/foo/good.js
generated
vendored
Normal file
1
tests/specs/run/conditional_exports_from_require/node_modules/foo/good.js
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
console.log("good");
|
8
tests/specs/run/conditional_exports_from_require/node_modules/foo/package.json
generated
vendored
Normal file
8
tests/specs/run/conditional_exports_from_require/node_modules/foo/package.json
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"some-condition": "./good.js",
|
||||||
|
"default": "./bad.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue