Skip to content

Commit afe5e80

Browse files
authored
feat: add feature package_json_raw_json_api for returning package's raw json (#104)
1 parent 0bdd3f0 commit afe5e80

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,4 @@ jobs:
217217
- uses: actions/checkout@v4
218218
- uses: ./.github/actions/pnpm
219219
- uses: ./.github/actions/rustup
220-
- run: cargo test
220+
- run: cargo test --all-features

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,8 @@ criterion = { version = "0.5.1", default-features = false }
9494
normalize-path = { version = "0.2.1" }
9595

9696
[features]
97+
default = []
98+
# Enable the PackageJson::raw_json API
99+
package_json_raw_json_api = []
100+
# For codspeed benchmark
97101
codspeed = ["codspeed-criterion-compat"]

src/package_json.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//! package.json definitions
22
//!
33
//! 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)
4-
use std::{
5-
path::{Path, PathBuf},
6-
sync::Arc,
7-
};
4+
use std::path::{Path, PathBuf};
85

96
use nodejs_package_json::{BrowserField, ImportExportField, ImportExportMap};
107
use serde::Deserialize;
@@ -21,7 +18,8 @@ pub struct PackageJson {
2118
/// Realpath to `package.json`. Contains the `package.json` filename.
2219
pub realpath: PathBuf,
2320

24-
pub(crate) raw_json: Arc<serde_json::Value>,
21+
#[cfg(feature = "package_json_raw_json_api")]
22+
pub(crate) raw_json: std::sync::Arc<serde_json::Value>,
2523

2624
/// The "name" field defines your package's name.
2725
/// 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 {
7270

7371
if let Some(json_object) = raw_json.as_object_mut() {
7472
// Remove large fields that are useless for pragmatic use.
75-
json_object.remove("description");
76-
json_object.remove("keywords");
77-
json_object.remove("scripts");
78-
json_object.remove("dependencies");
79-
json_object.remove("devDependencies");
80-
json_object.remove("peerDependencies");
81-
json_object.remove("optionalDependencies");
73+
#[cfg(feature = "package_json_raw_json_api")]
74+
{
75+
json_object.remove("description");
76+
json_object.remove("keywords");
77+
json_object.remove("scripts");
78+
json_object.remove("dependencies");
79+
json_object.remove("devDependencies");
80+
json_object.remove("peerDependencies");
81+
json_object.remove("optionalDependencies");
82+
}
8283

8384
// Add name.
8485
package_json.name =
@@ -137,7 +138,10 @@ impl PackageJson {
137138

138139
package_json.path = path;
139140
package_json.realpath = realpath;
140-
package_json.raw_json = Arc::new(raw_json);
141+
#[cfg(feature = "package_json_raw_json_api")]
142+
{
143+
package_json.raw_json = std::sync::Arc::new(raw_json);
144+
}
141145
Ok(package_json)
142146
}
143147

@@ -170,7 +174,8 @@ impl PackageJson {
170174
/// To reduce overall memory consumption, large fields that useless for pragmatic use are removed.
171175
/// They are: `description`, `keywords`, `scripts`,
172176
/// `dependencies` and `devDependencies`, `peerDependencies`, `optionalDependencies`.
173-
pub fn raw_json(&self) -> &Arc<serde_json::Value> {
177+
#[cfg(feature = "package_json_raw_json_api")]
178+
pub fn raw_json(&self) -> &std::sync::Arc<serde_json::Value> {
174179
&self.raw_json
175180
}
176181

tests/integration_test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ fn package_json() {
4040
.is_some_and(|json| json.name.as_ref().is_some_and(|name| name == "name")));
4141
}
4242

43+
#[cfg(feature = "package_json_raw_json_api")]
44+
#[test]
45+
fn package_json_raw_json_api() {
46+
let resolution = resolve("./tests/package.json");
47+
assert!(resolution
48+
.package_json()
49+
.unwrap()
50+
.raw_json()
51+
.get("name")
52+
.is_some_and(|name| name == "name"));
53+
}
54+
4355
#[test]
4456
fn clear_cache() {
4557
let resolver = Resolver::new(ResolveOptions::default());

0 commit comments

Comments
 (0)