这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ indexmap = { version = "2", features = ["serde"] }
json-strip-comments = "3"
once_cell = "1" # Use `std::sync::OnceLock::get_or_try_init` when it is stable.
papaya = "0.2"
parking_lot = "0.12"
rustc-hash = { version = "2" }
serde = { version = "1", features = ["derive"] } # derive for Deserialize from package.json
serde_json = { version = "1", features = ["preserve_order"] } # preserve_order: package_json.exports requires order such as `["require", "import", "default"]`
Expand Down
60 changes: 38 additions & 22 deletions src/cache/cache_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use cfg_if::cfg_if;
#[cfg(feature = "yarn_pnp")]
use once_cell::sync::OnceCell;
use papaya::{HashMap, HashSet};
use parking_lot::RwLock;
use rustc_hash::FxHasher;

use super::borrowed_path::BorrowedCachedPath;
Expand All @@ -21,11 +22,14 @@ use crate::{
context::ResolveContext as Ctx, path::PathUtil,
};

pub type PackageJsonIndex = usize;

/// Cache implementation used for caching filesystem access.
#[derive(Default)]
pub struct Cache<Fs> {
pub(crate) fs: Fs,
pub(crate) paths: HashSet<CachedPath, BuildHasherDefault<IdentityHasher>>,
pub(crate) package_jsons: RwLock<Vec<Arc<PackageJson>>>,
pub(crate) tsconfigs: HashMap<PathBuf, Arc<TsConfig>, BuildHasherDefault<FxHasher>>,
#[cfg(feature = "yarn_pnp")]
pub(crate) yarn_pnp_manifest: OnceCell<pnp::Manifest>,
Expand All @@ -35,6 +39,7 @@ impl<Fs: FileSystem> Cache<Fs> {
pub fn clear(&self) {
self.paths.pin().clear();
self.tsconfigs.pin().clear();
self.package_jsons.write().clear();
}

#[allow(clippy::cast_possible_truncation)]
Expand Down Expand Up @@ -110,38 +115,48 @@ impl<Fs: FileSystem> Cache<Fs> {
.get_or_try_init(|| {
let package_json_path = path.path.join("package.json");
let Ok(package_json_bytes) = self.fs.read(&package_json_path) else {
if let Some(deps) = &mut ctx.missing_dependencies {
deps.push(package_json_path);
}
return Ok(None);
};

let real_path = if options.symlinks {
self.canonicalize(path)?.join("package.json")
} else {
package_json_path.clone()
};
PackageJson::parse(&self.fs, package_json_path, real_path, package_json_bytes)
.map(|package_json| Some(Arc::new(package_json)))
.map_err(ResolveError::Json)
PackageJson::parse(
&self.fs,
package_json_path.clone(),
real_path,
package_json_bytes,
)
.map(|package_json| {
let arc = Arc::new(package_json);
let index = {
let mut arena = self.package_jsons.write();
let index = arena.len();
arena.push(arc);
index
};
Some(index)
})
.map_err(ResolveError::Json)
// https://github.com/webpack/enhanced-resolve/blob/58464fc7cb56673c9aa849e68e6300239601e615/lib/DescriptionFileUtils.js#L68-L82
.inspect(|_| {
ctx.add_file_dependency(&package_json_path);
})
.inspect_err(|_| {
if let Some(deps) = &mut ctx.file_dependencies {
deps.push(package_json_path.clone());
}
})
})
.cloned();

// https://github.com/webpack/enhanced-resolve/blob/58464fc7cb56673c9aa849e68e6300239601e615/lib/DescriptionFileUtils.js#L68-L82
match &result {
Ok(Some(package_json)) => {
ctx.add_file_dependency(&package_json.path);
}
Ok(None) => {
// Avoid an allocation by making this lazy
if let Some(deps) = &mut ctx.missing_dependencies {
deps.push(path.path.join("package.json"));
}
}
Err(_) => {
if let Some(deps) = &mut ctx.file_dependencies {
deps.push(path.path.join("package.json"));
}
}
}
result
result.map(|option_index| {
option_index.and_then(|index| self.package_jsons.read().get(index).cloned())
})
}

pub(crate) fn get_tsconfig<F: FnOnce(&mut TsConfig) -> Result<(), ResolveError>>(
Expand Down Expand Up @@ -213,6 +228,7 @@ impl<Fs: FileSystem> Cache<Fs> {
.hasher(BuildHasherDefault::default())
.resize_mode(papaya::ResizeMode::Blocking)
.build(),
package_jsons: RwLock::new(Vec::with_capacity(512)),
tsconfigs: HashMap::builder()
.hasher(BuildHasherDefault::default())
.resize_mode(papaya::ResizeMode::Blocking)
Expand Down
3 changes: 2 additions & 1 deletion src/cache/cached_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use cfg_if::cfg_if;
use once_cell::sync::OnceCell as OnceLock;

use super::cache_impl::Cache;
use super::cache_impl::PackageJsonIndex;
use super::thread_local::SCRATCH_PATH;
use crate::{
FileSystem, PackageJson, ResolveError, ResolveOptions, TsConfig, context::ResolveContext as Ctx,
Expand All @@ -28,7 +29,7 @@ pub struct CachedPathImpl {
pub meta: OnceLock<Option<(/* is_file */ bool, /* is_dir */ bool)>>, // None means not found.
pub canonicalized: OnceLock<Weak<CachedPathImpl>>,
pub node_modules: OnceLock<Option<Weak<CachedPathImpl>>>,
pub package_json: OnceLock<Option<Arc<PackageJson>>>,
pub package_json: OnceLock<Option<PackageJsonIndex>>,
pub tsconfig: OnceLock<Option<Arc<TsConfig>>>,
}

Expand Down
11 changes: 8 additions & 3 deletions src/package_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ pub use serde::*;
#[cfg(target_endian = "little")]
pub use simd::*;

use std::{fmt, path::PathBuf};
use std::{fmt, path::Path};

use crate::JSONError;

/// Check if JSON content is empty or contains only whitespace
fn check_if_empty(json_bytes: &[u8], path: PathBuf) -> Result<(), JSONError> {
fn check_if_empty(json_bytes: &[u8], path: &Path) -> Result<(), JSONError> {
// Check if content is empty or whitespace-only
if json_bytes.iter().all(|&b| b.is_ascii_whitespace()) {
return Err(JSONError { path, message: "File is empty".to_string(), line: 0, column: 0 });
return Err(JSONError {
path: path.to_path_buf(),
message: "File is empty".to_string(),
line: 0,
column: 0,
});
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/package_json/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl PackageJson {
let json_bytes = if json.starts_with(b"\xEF\xBB\xBF") { &json[3..] } else { &json[..] };

// Check if empty after BOM stripping
super::check_if_empty(json_bytes, path.clone())?;
super::check_if_empty(json_bytes, &path)?;

// Parse JSON directly from bytes
let value = serde_json::from_slice::<Value>(json_bytes).map_err(|error| JSONError {
Expand Down
2 changes: 1 addition & 1 deletion src/package_json/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl PackageJson {
}

// Check if empty after BOM stripping
super::check_if_empty(&json_bytes, path.clone())?;
super::check_if_empty(&json_bytes, &path)?;

// Create the self-cell with the JSON bytes and parsed BorrowedValue
let cell = PackageJsonCell::try_new(MutBorrow::new(json_bytes), |bytes| {
Expand Down
22 changes: 10 additions & 12 deletions src/tests/dependencies.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! https://github.com/webpack/enhanced-resolve/blob/main/test/dependencies.test.js

#[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows.
mod windows {
mod test {
use std::path::PathBuf;

use super::super::memory_fs::MemoryFS;
Expand All @@ -18,17 +18,6 @@ mod windows {

#[test]
fn test() {
let file_system = file_system();

let resolver = ResolverGeneric::new_with_file_system(
file_system,
ResolveOptions {
extensions: vec![".json".into(), ".js".into()],
modules: vec!["/modules".into(), "node_modules".into()],
..ResolveOptions::default()
},
);

let data = [
(
"middle module request",
Expand Down Expand Up @@ -92,6 +81,15 @@ mod windows {
];

for (name, context, request, result, file_dependencies, missing_dependencies) in data {
let file_system = file_system();
let resolver = ResolverGeneric::new_with_file_system(
file_system,
ResolveOptions {
extensions: vec![".json".into(), ".js".into()],
modules: vec!["/modules".into(), "node_modules".into()],
..ResolveOptions::default()
},
);
let mut ctx = ResolveContext::default();
let path = PathBuf::from(context);
let resolved_path =
Expand Down