Repository: oxc-project/oxc-resolver
Pull request #196 (2024-06-28 10:21:15)

feat: export package.json `type` and `sideEffects` field by default for bundlers
The fields are pretty small in size, but we can try and remove the clones if necessary.

codspeed-hq[bot]: ## [CodSpeed Performance Report](https://codspeed.io/oxc-project/oxc-resolver/branches/package-json-exports)

### Merging #196 will **not alter performance**

<sub>Comparing <code>package-json-exports</code> (42966d3) with <code>main</code> (9b9e97f)</sub>



### Summary

`✅ 2` untouched benchmarks





codecov[bot]: ## [Codecov](https://app.codecov.io/gh/oxc-project/oxc-resolver/pull/196?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 96.89%. Comparing base [(`9b9e97f`)](https://app.codecov.io/gh/oxc-project/oxc-resolver/commit/9b9e97ff1a4978c6e121c7be0e3976d3518494fc?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project) to head [(`42966d3`)](https://app.codecov.io/gh/oxc-project/oxc-resolver/commit/42966d3b9b3a650507b8cacab818a8dd850f0c0a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main     #196   +/-   ##
=======================================
  Coverage   96.88%   96.89%           
=======================================
  Files          11       11           
  Lines        2281     2283    +2     
=======================================
+ Hits         2210     2212    +2     
  Misses         71       71           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/oxc-project/oxc-resolver/pull/196?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project).


Patch:
diff --git a/src/package_json.rs b/src/package_json.rs
index 73901fa..7e05d20 100644
--- a/src/package_json.rs
+++ b/src/package_json.rs
@@ -24,6 +24,16 @@ pub struct PackageJson {
     /// <https://nodejs.org/api/packages.html#name>
     pub name: Option<String>,
 
+    /// The "type" field.
+    ///
+    /// <https://nodejs.org/api/packages.html#type>
+    pub r#type: Option<JSONValue>,
+
+    /// The "sideEffects" field.
+    ///
+    /// <https://webpack.js.org/guides/tree-shaking>
+    pub side_effects: Option<JSONValue>,
+
     raw_json: std::sync::Arc<JSONValue>,
 }
 
@@ -51,9 +61,11 @@ impl PackageJson {
                 json_object.remove("optionalDependencies");
             }
 
-            // Add name.
+            // Add name, type and sideEffects.
             package_json.name =
                 json_object.get("name").and_then(|field| field.as_str()).map(ToString::to_string);
+            package_json.r#type = json_object.get("type").cloned();
+            package_json.side_effects = json_object.get("sideEffects").cloned();
         }
 
         package_json.path = path;


Test patch:
diff --git a/tests/integration_test.rs b/tests/integration_test.rs
index d369413..0fc5288 100644
--- a/tests/integration_test.rs
+++ b/tests/integration_test.rs
@@ -35,9 +35,10 @@ fn eq() {
 #[test]
 fn package_json() {
     let resolution = resolve("./tests/package.json");
-    assert!(resolution
-        .package_json()
-        .is_some_and(|json| json.name.as_ref().is_some_and(|name| name == "name")));
+    let package_json = resolution.package_json().unwrap();
+    assert_eq!(package_json.name.as_ref().unwrap(), "name");
+    assert_eq!(package_json.r#type.as_ref().unwrap().as_str(), "module".into());
+    assert!(package_json.side_effects.as_ref().unwrap().is_object());
 }
 
 #[cfg(feature = "package_json_raw_json_api")]
diff --git a/tests/package.json b/tests/package.json
index f099adc..54f58b8 100644
--- a/tests/package.json
+++ b/tests/package.json
@@ -1,3 +1,5 @@
 {
-  "name": "name"
+  "name": "name",
+  "type": "module",
+  "sideEffects": {}
 }