diff --git a/crates/parcel-resolver/src/package_json.rs b/crates/parcel-resolver/src/package_json.rs index 268e89fc6f4..b0533400047 100644 --- a/crates/parcel-resolver/src/package_json.rs +++ b/crates/parcel-resolver/src/package_json.rs @@ -41,28 +41,40 @@ bitflags! { #[derive(serde::Deserialize, Debug, Default)] #[serde(rename_all = "camelCase")] struct SerializedPackageJson { - #[serde(default)] + #[serde(default, deserialize_with = "ok_or_default")] pub name: String, - #[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, AliasValue<'static>>, - #[serde(default)] + #[serde(default, deserialize_with = "ok_or_default")] exports: ExportsField, - #[serde(default)] + #[serde(default, deserialize_with = "ok_or_default")] imports: IndexMap, ExportsField>, - #[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(Debug)] pub struct PackageJson { pub path: CachedPath, @@ -1932,5 +1944,7 @@ mod tests { assert_eq!(pkg.module_type, ModuleType::CommonJs); let pkg: SerializedPackageJson = serde_json::from_str(r#"{"name":"foo"}"#).unwrap(); assert_eq!(pkg.module_type, ModuleType::CommonJs); + let pkg: SerializedPackageJson = serde_json::from_str(r#"{"main":false}"#).unwrap(); + assert_eq!(pkg.main, None); } }