Skip to content

Commit

Permalink
Ignore invalid package.json fields (#10053)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored Dec 16, 2024
1 parent 4d27ec8 commit 86ca555
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions crates/parcel-resolver/src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
#[serde(default, deserialize_with = "ok_or_default")]
module: Option<PathBuf>,
#[serde(default, deserialize_with = "ok_or_default")]
tsconfig: Option<PathBuf>,
#[serde(default, deserialize_with = "ok_or_default")]
types: Option<PathBuf>,
#[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<Specifier<'static>, AliasValue<'static>>,
#[serde(default)]
#[serde(default, deserialize_with = "ok_or_default")]
exports: ExportsField,
#[serde(default)]
#[serde(default, deserialize_with = "ok_or_default")]
imports: IndexMap<ExportsKey<'static>, ExportsField>,
#[serde(default)]
#[serde(default, deserialize_with = "ok_or_default")]
side_effects: SideEffects,
}

fn ok_or_default<'de, T, D>(deserializer: D) -> Result<T, D::Error>
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,
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 86ca555

Please sign in to comment.