From 1b19786cd1a63b4dd5aaf18c607b4b0ab4c2dfbd Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Fri, 27 Dec 2024 19:44:20 +0800 Subject: [PATCH] feat: dependency.ids --- crates/node_binding/binding.d.ts | 1 + .../rspack_binding_values/src/dependency.rs | 58 ++++++++++++++++--- packages/rspack/src/Dependency.ts | 7 +++ 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index b7cfbb52822c..7e71eff37092 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -196,6 +196,7 @@ export declare class JsDependency { get request(): string | undefined get critical(): boolean set critical(val: boolean) + get ids(): Array | undefined } export declare class JsEntries { diff --git a/crates/rspack_binding_values/src/dependency.rs b/crates/rspack_binding_values/src/dependency.rs index abad6bdfdd09..4381cf7b3464 100644 --- a/crates/rspack_binding_values/src/dependency.rs +++ b/crates/rspack_binding_values/src/dependency.rs @@ -1,9 +1,13 @@ 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; +use rspack_plugin_javascript::dependency::{ + CommonJsExportRequireDependency, ESMExportImportedSpecifierDependency, + ESMImportSpecifierDependency, +}; use rustc_hash::FxHashMap as HashMap; // JsDependency allows JS-side access to a Dependency instance that has already @@ -16,7 +20,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(); @@ -25,7 +29,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.", @@ -36,7 +40,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)) } } @@ -52,21 +56,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> { - let dependency = self.as_ref()?; + let (dependency, _) = self.as_ref()?; Ok(match dependency.as_module_dependency() { Some(dep) => napi::Either::A(dep.request()), @@ -76,7 +80,7 @@ impl JsDependency { #[napi(getter)] pub fn critical(&mut self) -> napi::Result { - let dependency = self.as_ref()?; + let (dependency, _) = self.as_ref()?; Ok(match dependency.as_context_dependency() { Some(dep) => dep.critical().is_some(), @@ -96,6 +100,44 @@ impl JsDependency { } Ok(()) } + + #[napi(getter)] + pub fn ids(&mut self, env: Env) -> napi::Result, ()>> { + let (dependency, compilation) = self.as_ref()?; + + Ok(match compilation { + Some(compilation) => { + let module_graph = compilation.get_module_graph(); + if let Some(dependency) = dependency.downcast_ref::() { + let ids = dependency + .get_ids(&module_graph) + .into_iter() + .map(|atom| env.create_string(atom.as_str())) + .collect::>>()?; + Either::A(ids) + } else if let Some(dependency) = + dependency.downcast_ref::() + { + let ids = dependency + .get_ids(&module_graph) + .into_iter() + .map(|atom| env.create_string(atom.as_str())) + .collect::>>()?; + Either::A(ids) + } else if let Some(dependency) = dependency.downcast_ref::() { + let ids = dependency + .get_ids(&module_graph) + .into_iter() + .map(|atom| env.create_string(atom.as_str())) + .collect::>>()?; + Either::A(ids) + } else { + Either::B(()) + } + } + None => Either::B(()), + }) + } } type DependencyInstanceRefs = HashMap>; diff --git a/packages/rspack/src/Dependency.ts b/packages/rspack/src/Dependency.ts index 40eb19a58236..0e86b221d082 100644 --- a/packages/rspack/src/Dependency.ts +++ b/packages/rspack/src/Dependency.ts @@ -63,4 +63,11 @@ export class Dependency { binding.critical = val; } } + + get ids(): string[] | undefined { + const binding = bindingDependencyFactory.getBinding(this); + if (binding) { + return binding.ids; + } + } }