diff --git a/packages/utils/node-resolver-rs/src/package_json.rs b/packages/utils/node-resolver-rs/src/package_json.rs index 175424939..3bea9d276 100644 --- a/packages/utils/node-resolver-rs/src/package_json.rs +++ b/packages/utils/node-resolver-rs/src/package_json.rs @@ -36,29 +36,42 @@ bitflags! { pub struct PackageJson { #[serde(skip)] pub path: PathBuf, - #[serde(default)] + #[serde(default, deserialize_with = "ok_or_default")] pub name: String, + #[serde(default, deserialize_with = "ok_or_default")] pub version: Option, - #[serde(rename = "type", default)] + #[serde(rename = "type", default, deserialize_with = "ok_or_default")] pub module_type: ModuleType, + #[serde(default, deserialize_with = "ok_or_default")] main: Option, + #[serde(default, deserialize_with = "ok_or_default")] module: Option, + #[serde(default, deserialize_with = "ok_or_default")] tsconfig: Option, + #[serde(default, deserialize_with = "ok_or_default")] types: Option, - #[serde(default)] + #[serde(default, deserialize_with = "ok_or_default")] pub source: SourceField, - #[serde(default)] + #[serde(default, deserialize_with = "ok_or_default")] browser: BrowserField, - #[serde(default)] + #[serde(default, deserialize_with = "ok_or_default")] alias: IndexMap, - #[serde(default)] + #[serde(default, deserialize_with = "ok_or_default")] exports: ExportsField, - #[serde(default)] + #[serde(default, deserialize_with = "ok_or_default")] imports: IndexMap, - #[serde(default)] + #[serde(default, deserialize_with = "ok_or_default")] side_effects: SideEffects, } +fn ok_or_default<'de, T, D>(deserializer: D) -> Result +where + T: serde::Deserialize<'de> + Default, + D: serde::Deserializer<'de>, +{ + Ok(T::deserialize(deserializer).unwrap_or_default()) +} + #[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, Default, PartialEq)] #[serde(rename_all = "lowercase")] pub enum ModuleType { @@ -1595,5 +1608,7 @@ mod tests { assert_eq!(pkg.module_type, ModuleType::CommonJs); let pkg: PackageJson = serde_json::from_str(r#"{"name":"foo"}"#).unwrap(); assert_eq!(pkg.module_type, ModuleType::CommonJs); + let pkg: PackageJson = serde_json::from_str(r#"{"main":false}"#).unwrap(); + assert_eq!(pkg.main, None); } }