summaryrefslogtreecommitdiff
path: root/ext/node/package_json.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2022-09-12 15:47:54 -0400
committerGitHub <noreply@github.com>2022-09-12 15:47:54 -0400
commit98454c1eb802b91a8c77dd97888a8994c85dfa46 (patch)
tree851f7590f14a2f54db5ef306e884cc2c64bf6f07 /ext/node/package_json.rs
parenta3a4760831e14307a9499d4e410cf1653b416dc1 (diff)
fix(npm): support cjs resolution of package subpath with package.json (#15855)
Diffstat (limited to 'ext/node/package_json.rs')
-rw-r--r--ext/node/package_json.rs14
1 files changed, 12 insertions, 2 deletions
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 {