Skip to content

Commit

Permalink
feat: dependency.ids
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 27, 2024
1 parent 25669cf commit 88569b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export declare class JsDependency {
get request(): string | undefined
get critical(): boolean
set critical(val: boolean)
get ids(): Array<string> | undefined
}

export declare class JsEntries {
Expand Down
33 changes: 25 additions & 8 deletions crates/rspack_binding_values/src/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cell::RefCell, ptr::NonNull};

use napi::{bindgen_prelude::ToNapiValue, Either};
use napi::{bindgen_prelude::ToNapiValue, Either, Env, JsString};
use napi_derive::napi;
use rspack_core::{Compilation, CompilationId, Dependency, DependencyId};
use rspack_napi::OneShotRef;
Expand All @@ -16,7 +16,7 @@ pub struct JsDependency {
}

impl JsDependency {
fn as_ref(&mut self) -> napi::Result<&dyn Dependency> {
fn as_ref(&mut self) -> napi::Result<(&dyn Dependency, Option<&Compilation>)> {
if let Some(compilation) = self.compilation {
let compilation = unsafe { compilation.as_ref() };
let module_graph = compilation.get_module_graph();
Expand All @@ -25,7 +25,7 @@ impl JsDependency {
#[allow(clippy::unwrap_used)]
NonNull::new(dependency.as_ref() as *const dyn Dependency as *mut dyn Dependency).unwrap()
};
Ok(unsafe { self.dependency.as_ref() })
Ok((unsafe { self.dependency.as_ref() }, Some(compilation)))
} else {
Err(napi::Error::from_reason(format!(
"Unable to access dependency with id = {:?} now. The dependency have been removed on the Rust side.",
Expand All @@ -36,7 +36,7 @@ impl JsDependency {
// SAFETY:
// We need to make users aware in the documentation that values obtained within the JS hook callback should not be used outside the scope of the callback.
// We do not guarantee that the memory pointed to by the pointer remains valid when used outside the scope.
Ok(unsafe { self.dependency.as_ref() })
Ok((unsafe { self.dependency.as_ref() }, None))
}
}

Expand All @@ -52,21 +52,21 @@ impl JsDependency {
impl JsDependency {
#[napi(getter)]
pub fn get_type(&mut self) -> napi::Result<&str> {
let dependency = self.as_ref()?;
let (dependency, _) = self.as_ref()?;

Ok(dependency.dependency_type().as_str())
}

#[napi(getter)]
pub fn category(&mut self) -> napi::Result<&str> {
let dependency = self.as_ref()?;
let (dependency, _) = self.as_ref()?;

Ok(dependency.category().as_str())
}

#[napi(getter)]
pub fn request(&mut self) -> napi::Result<napi::Either<&str, ()>> {
let dependency = self.as_ref()?;
let (dependency, _) = self.as_ref()?;

Ok(match dependency.as_module_dependency() {
Some(dep) => napi::Either::A(dep.request()),
Expand All @@ -76,7 +76,7 @@ impl JsDependency {

#[napi(getter)]
pub fn critical(&mut self) -> napi::Result<bool> {
let dependency = self.as_ref()?;
let (dependency, _) = self.as_ref()?;

Ok(match dependency.as_context_dependency() {
Some(dep) => dep.critical().is_some(),
Expand All @@ -96,6 +96,23 @@ impl JsDependency {
}
Ok(())
}

#[napi(getter)]
pub fn ids(&mut self, env: Env) -> napi::Result<Either<Vec<JsString>, ()>> {
let (dependency, compilation) = self.as_ref()?;
Ok(match compilation {
Some(compilation) => {
let module_graph = compilation.get_module_graph();
let ids = dependency
._get_ids(&module_graph)
.into_iter()
.map(|atom| env.create_string(atom.as_str()))
.collect::<napi::Result<Vec<_>>>()?;
Either::A(ids)
}
None => Either::B(()),
})
}
}

type DependencyInstanceRefs = HashMap<DependencyId, OneShotRef<JsDependency>>;
Expand Down
7 changes: 7 additions & 0 deletions packages/rspack/src/Dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ export class Dependency {
binding.critical = val;
}
}

get ids(): string[] | undefined {
const binding = bindingDependencyFactory.getBinding(this);
if (binding) {
return binding.ids;
}
}
}

0 comments on commit 88569b3

Please sign in to comment.