Repository: oxc-project/oxc-resolver
Pull request #104 (2024-03-07 08:54:11)

feat: add feature `package_json_raw_json_api` for returning package's raw json

Boshen: * **#104** <a href="https://app.graphite.dev/github/pr/oxc-project/oxc-resolver/104?utm_source=stack-comment-icon" target="_blank"><img src="https://static.graphite.dev/graphite-32x32-black.png" alt="Graphite" width="10px" height="10px"/></a> 👈
* `main`

This stack of pull requests is managed by Graphite. <a href="https://stacking.dev/?utm_source=stack-comment">Learn more about stacking.</a>
<h2></h2>

 Join @Boshen and the rest of your teammates on <a href="https://graphite.dev?utm-source=stack-comment"><img src="https://static.graphite.dev/graphite-32x32-black.png" alt="Graphite" width="11px" height="11px"/> <b>Graphite</b></a>
  <!-- Current dependencies on/for this PR: -->
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/oxc-project/oxc-resolver/pull/104?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 95.23%. Comparing base [(`0bdd3f0`)](https://app.codecov.io/gh/oxc-project/oxc-resolver/commit/0bdd3f0aeb4662707b23f3574354bffd4cb74ceb?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=oxc-project) to head [(`488286a`)](https://app.codecov.io/gh/oxc-project/oxc-resolver/pull/104?src=pr&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     #104      +/-   ##
==========================================
+ Coverage   95.11%   95.23%   +0.11%     
==========================================
  Files          11       11              
  Lines        2192     2183       -9     
==========================================
- Hits         2085     2079       -6     
+ Misses        107      104       -3     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/oxc-project/oxc-resolver/pull/104?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/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 41f0045..8a00ec0 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -217,4 +217,4 @@ jobs:
       - uses: actions/checkout@v4
       - uses: ./.github/actions/pnpm
       - uses: ./.github/actions/rustup
-      - run: cargo test
+      - run: cargo test --all-features
diff --git a/Cargo.toml b/Cargo.toml
index c1f8b9e..614d34b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -94,4 +94,8 @@ criterion      = { version = "0.5.1", default-features = false }
 normalize-path = { version = "0.2.1" }
 
 [features]
+default = []
+# Enable the PackageJson::raw_json API
+package_json_raw_json_api = []
+# For codspeed benchmark
 codspeed = ["codspeed-criterion-compat"]
diff --git a/src/package_json.rs b/src/package_json.rs
index 436515d..357e698 100644
--- a/src/package_json.rs
+++ b/src/package_json.rs
@@ -1,10 +1,7 @@
 //! package.json definitions
 //!
 //! Code related to export field are copied from [Parcel's resolver](https://github.com/parcel-bundler/parcel/blob/v2/packages/utils/node-resolver-rs/src/package_json.rs)
-use std::{
-    path::{Path, PathBuf},
-    sync::Arc,
-};
+use std::path::{Path, PathBuf};
 
 use nodejs_package_json::{BrowserField, ImportExportField, ImportExportMap};
 use serde::Deserialize;
@@ -21,7 +18,8 @@ pub struct PackageJson {
     /// Realpath to `package.json`. Contains the `package.json` filename.
     pub realpath: PathBuf,
 
-    pub(crate) raw_json: Arc<serde_json::Value>,
+    #[cfg(feature = "package_json_raw_json_api")]
+    pub(crate) raw_json: std::sync::Arc<serde_json::Value>,
 
     /// The "name" field defines your package's name.
     /// The "name" field can be used in addition to the "exports" field to self-reference a package using its name.
@@ -72,13 +70,16 @@ impl PackageJson {
 
         if let Some(json_object) = raw_json.as_object_mut() {
             // Remove large fields that are useless for pragmatic use.
-            json_object.remove("description");
-            json_object.remove("keywords");
-            json_object.remove("scripts");
-            json_object.remove("dependencies");
-            json_object.remove("devDependencies");
-            json_object.remove("peerDependencies");
-            json_object.remove("optionalDependencies");
+            #[cfg(feature = "package_json_raw_json_api")]
+            {
+                json_object.remove("description");
+                json_object.remove("keywords");
+                json_object.remove("scripts");
+                json_object.remove("dependencies");
+                json_object.remove("devDependencies");
+                json_object.remove("peerDependencies");
+                json_object.remove("optionalDependencies");
+            }
 
             // Add name.
             package_json.name =
@@ -137,7 +138,10 @@ impl PackageJson {
 
         package_json.path = path;
         package_json.realpath = realpath;
-        package_json.raw_json = Arc::new(raw_json);
+        #[cfg(feature = "package_json_raw_json_api")]
+        {
+            package_json.raw_json = std::sync::Arc::new(raw_json);
+        }
         Ok(package_json)
     }
 
@@ -170,7 +174,8 @@ impl PackageJson {
     /// To reduce overall memory consumption, large fields that useless for pragmatic use are removed.
     /// They are: `description`, `keywords`, `scripts`,
     /// `dependencies` and `devDependencies`, `peerDependencies`, `optionalDependencies`.
-    pub fn raw_json(&self) -> &Arc<serde_json::Value> {
+    #[cfg(feature = "package_json_raw_json_api")]
+    pub fn raw_json(&self) -> &std::sync::Arc<serde_json::Value> {
         &self.raw_json
     }
 


Test patch:
diff --git a/tests/integration_test.rs b/tests/integration_test.rs
index ba8fb82..c48c3da 100644
--- a/tests/integration_test.rs
+++ b/tests/integration_test.rs
@@ -40,6 +40,18 @@ fn package_json() {
         .is_some_and(|json| json.name.as_ref().is_some_and(|name| name == "name")));
 }
 
+#[cfg(feature = "package_json_raw_json_api")]
+#[test]
+fn package_json_raw_json_api() {
+    let resolution = resolve("./tests/package.json");
+    assert!(resolution
+        .package_json()
+        .unwrap()
+        .raw_json()
+        .get("name")
+        .is_some_and(|name| name == "name"));
+}
+
 #[test]
 fn clear_cache() {
     let resolver = Resolver::new(ResolveOptions::default());