diff options
Diffstat (limited to 'ext/node')
-rw-r--r-- | ext/node/02_require.js | 12 | ||||
-rw-r--r-- | ext/node/lib.rs | 9 | ||||
-rw-r--r-- | ext/node/package_json.rs | 14 | ||||
-rw-r--r-- | ext/node/resolution.rs | 7 |
4 files changed, 22 insertions, 20 deletions
diff --git a/ext/node/02_require.js b/ext/node/02_require.js index f3ed266c3..578d8e873 100644 --- a/ext/node/02_require.js +++ b/ext/node/02_require.js @@ -98,7 +98,11 @@ } function tryPackage(requestPath, exts, isMain, originalPath) { - const pkg = core.ops.op_require_read_package_scope(requestPath).main; + const packageJsonPath = pathResolve( + requestPath, + "package.json", + ); + const pkg = core.ops.op_require_read_package_scope(packageJsonPath).main; if (!pkg) { return tryExtensions( pathResolve(requestPath, "index"), @@ -135,12 +139,8 @@ err.requestPath = originalPath; throw err; } else { - const jsonPath = pathResolve( - requestPath, - "package.json", - ); node.globalThis.process.emitWarning( - `Invalid 'main' field in '${jsonPath}' of '${pkg}'. ` + + `Invalid 'main' field in '${packageJsonPath}' of '${pkg}'. ` + "Please either fix that or report it to the module author", "DeprecationWarning", "DEP0128", diff --git a/ext/node/lib.rs b/ext/node/lib.rs index 753a11b5d..db4fe3178 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -604,15 +604,12 @@ where #[op] fn op_require_read_package_scope( state: &mut OpState, - filename: String, + package_json_path: String, ) -> Option<PackageJson> { check_unstable(state); let resolver = state.borrow::<Rc<dyn DenoDirNpmResolver>>().clone(); - resolution::get_package_scope_config( - &Url::from_file_path(filename).unwrap(), - &*resolver, - ) - .ok() + let package_json_path = PathBuf::from(package_json_path); + PackageJson::load(&*resolver, package_json_path).ok() } #[op] diff --git a/ext/node/package_json.rs b/ext/node/package_json.rs index e5f9214fb..ced64a1b4 100644 --- a/ext/node/package_json.rs +++ b/ext/node/package_json.rs @@ -1,5 +1,7 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use crate::NodeModuleKind; + use super::DenoDirNpmResolver; use deno_core::anyhow; use deno_core::anyhow::bail; @@ -17,8 +19,8 @@ pub struct PackageJson { pub exports: Option<Map<String, Value>>, pub imports: Option<Map<String, Value>>, pub bin: Option<Value>, - pub main: Option<String>, - pub module: Option<String>, + main: Option<String>, // use .main(...) + module: Option<String>, // use .main(...) pub name: Option<String>, pub path: PathBuf, pub typ: String, @@ -123,6 +125,14 @@ impl PackageJson { }; Ok(package_json) } + + pub fn main(&self, referrer_kind: NodeModuleKind) -> Option<&String> { + if referrer_kind == NodeModuleKind::Esm && self.typ == "module" { + self.module.as_ref().or(self.main.as_ref()) + } else { + self.main.as_ref() + } + } } fn is_conditional_exports_main_sugar(exports: &Value) -> bool { diff --git a/ext/node/resolution.rs b/ext/node/resolution.rs index d6734066c..a7428fe03 100644 --- a/ext/node/resolution.rs +++ b/ext/node/resolution.rs @@ -707,12 +707,7 @@ pub fn legacy_main_resolve( package_json: &PackageJson, referrer_kind: NodeModuleKind, ) -> Result<PathBuf, AnyError> { - let maybe_main = - if referrer_kind == NodeModuleKind::Esm && package_json.typ == "module" { - package_json.module.as_ref().or(package_json.main.as_ref()) - } else { - package_json.main.as_ref() - }; + let maybe_main = package_json.main(referrer_kind); let mut guess; if let Some(main) = maybe_main { |