From 6e87fee8867652a91b2ade21b995ff782854b8f6 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Mon, 23 Sep 2024 14:34:23 +0800 Subject: [PATCH 1/9] feat: dependencies in context module factory hook --- crates/node_binding/binding.d.ts | 33 ++++-- .../src/compilation/entries.rs | 18 +-- .../src/context_module_factory.rs | 12 +- .../rspack_binding_values/src/dependency.rs | 106 ++++++++++++++---- crates/rspack_binding_values/src/lib.rs | 2 +- crates/rspack_binding_values/src/module.rs | 6 +- packages/rspack/src/DependenciesBlock.ts | 2 +- packages/rspack/src/Dependency.ts | 25 ++++- packages/rspack/src/Module.ts | 5 +- 9 files changed, 153 insertions(+), 56 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index ade54a26eb3..2b39aa7414f 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -20,7 +20,7 @@ export class ExternalObject { } } export class DependenciesBlockDto { - get dependencies(): Array + get dependencies(): Array get blocks(): Array } export type DependenciesBlockDTO = DependenciesBlockDto @@ -41,16 +41,9 @@ export class DependenciesDto { } export type DependenciesDTO = DependenciesDto -export class DependencyDto { - get type(): string - get category(): string - get request(): string | undefined -} -export type DependencyDTO = DependencyDto - export class EntryDataDto { - get dependencies(): Array - get includeDependencies(): Array + get dependencies(): Array + get includeDependencies(): Array get options(): EntryOptionsDto } export type EntryDataDTO = EntryDataDto @@ -120,6 +113,13 @@ export class JsCompilation { addRuntimeModule(chunkUkey: number, runtimeModule: JsAddingRuntimeModule): void } +export class JsCompiledDependency { + get type(): string + get category(): string + get request(): string | undefined + get critical(): boolean | undefined +} + export class JsContextModuleFactoryAfterResolveData { get resource(): string set resource(resource: string) @@ -131,6 +131,7 @@ export class JsContextModuleFactoryAfterResolveData { set regExp(rawRegExp: RawRegex | undefined) get recursive(): boolean set recursive(recursive: boolean) + get dependencies(): Array } export class JsContextModuleFactoryBeforeResolveData { @@ -144,6 +145,14 @@ export class JsContextModuleFactoryBeforeResolveData { set recursive(recursive: boolean) } +export class JsDependency { + get type(): string + get category(): string + get request(): string | undefined + get critical(): boolean | undefined + set critical(val: boolean) +} + export class JsEntries { clear(): void get size(): number @@ -525,8 +534,8 @@ export interface JsDiagnosticLocation { } export interface JsEntryData { - dependencies: Array - includeDependencies: Array + dependencies: Array + includeDependencies: Array options: JsEntryOptions } diff --git a/crates/rspack_binding_values/src/compilation/entries.rs b/crates/rspack_binding_values/src/compilation/entries.rs index 3ae57f39501..a970d28816a 100644 --- a/crates/rspack_binding_values/src/compilation/entries.rs +++ b/crates/rspack_binding_values/src/compilation/entries.rs @@ -2,7 +2,7 @@ use napi_derive::napi; use rspack_core::{ChunkLoading, Compilation, EntryData, EntryOptions, EntryRuntime}; use rspack_napi::napi::bindgen_prelude::*; -use crate::{dependency::DependencyDTO, entry::JsEntryOptions, library::JsLibraryOptions}; +use crate::{dependency::JsCompiledDependency, entry::JsEntryOptions, library::JsLibraryOptions}; #[napi] pub struct EntryOptionsDTO(EntryOptions); @@ -152,8 +152,8 @@ impl EntryOptionsDTO { #[napi(object, object_to_js = false)] pub struct JsEntryData { - pub dependencies: Vec>, - pub include_dependencies: Vec>, + pub dependencies: Vec>, + pub include_dependencies: Vec>, pub options: JsEntryOptions, } @@ -184,36 +184,36 @@ pub struct EntryDataDTO { #[napi] impl EntryDataDTO { #[napi(getter)] - pub fn dependencies(&'static self, env: Env) -> Result>> { + pub fn dependencies(&'static self, env: Env) -> Result>> { self .entry_data .dependencies .clone() .into_iter() .map(|id| { - let js_dep = DependencyDTO::new(id, self.compilation); + let js_dep = JsCompiledDependency::new(id, self.compilation); let instance = js_dep.into_instance(env)?; Ok(instance) }) - .collect::>>>() + .collect::>>>() } #[napi(getter)] pub fn include_dependencies( &'static self, env: Env, - ) -> Result>> { + ) -> Result>> { self .entry_data .include_dependencies .clone() .into_iter() .map(|id| { - let js_dep = DependencyDTO::new(id, self.compilation); + let js_dep = JsCompiledDependency::new(id, self.compilation); let instance = js_dep.into_instance(env)?; Ok(instance) }) - .collect::>>>() + .collect::>>>() } #[napi(getter)] diff --git a/crates/rspack_binding_values/src/context_module_factory.rs b/crates/rspack_binding_values/src/context_module_factory.rs index a8e52868970..6f20e1c0125 100644 --- a/crates/rspack_binding_values/src/context_module_factory.rs +++ b/crates/rspack_binding_values/src/context_module_factory.rs @@ -4,7 +4,7 @@ use napi::bindgen_prelude::{ use napi_derive::napi; use rspack_core::{AfterResolveData, BeforeResolveData}; -use crate::RawRegex; +use crate::{JsDependency, RawRegex}; #[napi] pub struct JsContextModuleFactoryBeforeResolveData(Box); @@ -173,6 +173,16 @@ impl JsContextModuleFactoryAfterResolveData { pub fn set_recursive(&mut self, recursive: bool) { self.0.recursive = recursive; } + + #[napi(getter)] + pub fn dependencies(&self) -> Vec { + self + .0 + .dependencies + .iter() + .map(|dep| JsDependency::new(dep.clone())) + .collect::>() + } } pub struct JsContextModuleFactoryAfterResolveDataWrapper(Box); diff --git a/crates/rspack_binding_values/src/dependency.rs b/crates/rspack_binding_values/src/dependency.rs index 6427449f2af..b61fb8e2182 100644 --- a/crates/rspack_binding_values/src/dependency.rs +++ b/crates/rspack_binding_values/src/dependency.rs @@ -1,57 +1,117 @@ use napi_derive::napi; -use rspack_core::{Compilation, Dependency, DependencyId, ModuleDependency, ModuleGraph}; +use rspack_core::{ + BoxDependency, Compilation, ContextDependency, Dependency, DependencyId, ModuleDependency, + ModuleGraph, +}; #[napi] -pub struct DependencyDTO { +pub struct JsCompiledDependency { pub(crate) dependency_id: DependencyId, - pub(crate) compilation: &'static Compilation, + pub(crate) module_graph: ModuleGraph<'static>, } -impl DependencyDTO { +impl JsCompiledDependency { pub(crate) fn new(dependency_id: DependencyId, compilation: &'static Compilation) -> Self { + let module_graph = compilation.get_module_graph(); Self { dependency_id, - compilation, + module_graph, } } - fn dependency<'a>(&self, module_graph: &'a ModuleGraph) -> &'a dyn Dependency { - module_graph + fn dependency<'a>(&self) -> &dyn Dependency { + self + .module_graph .dependency_by_id(&self.dependency_id) .unwrap_or_else(|| panic!("Failed to get dependency by id = {:?}", &self.dependency_id)) .as_ref() } - fn module_dependency<'a>( - &self, - module_graph: &'a ModuleGraph, - ) -> Option<&'a dyn ModuleDependency> { - self.dependency(module_graph).as_module_dependency() + fn module_dependency<'a>(&self) -> Option<&dyn ModuleDependency> { + self.dependency().as_module_dependency() + } + + fn context_dependency<'a>(&self) -> Option<&dyn ContextDependency> { + self.dependency().as_context_dependency() } } #[napi] -impl DependencyDTO { +impl JsCompiledDependency { #[napi(getter)] pub fn get_type(&self) -> &str { - let module_graph = self.compilation.get_module_graph(); - let dep = self.dependency(&module_graph); - dep.dependency_type().as_str() + self.dependency().dependency_type().as_str() } #[napi(getter)] pub fn category(&self) -> &str { - let module_graph = self.compilation.get_module_graph(); - let dep = self.dependency(&module_graph); - dep.category().as_str() + self.dependency().category().as_str() } #[napi(getter)] - pub fn request(&self) -> napi::Either { - let module_graph = self.compilation.get_module_graph(); - match self.module_dependency(&module_graph) { - Some(dep) => napi::Either::A(dep.request().to_string()), + pub fn request(&self) -> napi::Either<&str, ()> { + match self.module_dependency() { + Some(dep) => napi::Either::A(dep.request()), None => napi::Either::B(()), } } + + #[napi(getter)] + pub fn critical(&self) -> napi::Either { + match self.context_dependency() { + Some(dep) => napi::Either::A(dep.critical().is_some()), + None => napi::Either::B(()), + } + } +} + +#[napi] +pub struct JsDependency(BoxDependency); + +impl JsDependency { + pub(crate) fn new(dependency: BoxDependency) -> Self { + Self(dependency) + } +} + +#[napi] +impl JsDependency { + #[napi(getter)] + pub fn get_type(&self) -> &str { + self.0.dependency_type().as_str() + } + + #[napi(getter)] + pub fn category(&self) -> &str { + self.0.category().as_str() + } + + #[napi(getter)] + pub fn request(&self) -> napi::Either<&str, ()> { + match self.0.as_module_dependency() { + Some(dep) => napi::Either::A(dep.request()), + None => napi::Either::B(()), + } + } + + #[napi(getter)] + pub fn critical(&self) -> napi::Either { + match self.0.as_context_dependency() { + Some(dep) => napi::Either::A(dep.critical().is_some()), + None => napi::Either::B(()), + } + } + + #[napi(setter)] + pub fn set_critical(&mut self, val: bool) { + match self.0.as_context_dependency_mut() { + Some(dep) => { + let critical = dep.critical_mut(); + if !val { + *critical = None; + } + } + None => (), + } + } } diff --git a/crates/rspack_binding_values/src/lib.rs b/crates/rspack_binding_values/src/lib.rs index ba4a9518373..deeeea82032 100644 --- a/crates/rspack_binding_values/src/lib.rs +++ b/crates/rspack_binding_values/src/lib.rs @@ -32,7 +32,7 @@ pub use chunk_group::*; pub use codegen_result::*; pub use compilation::*; pub use context_module_factory::*; -pub use dependency::DependencyDTO; +pub use dependency::*; pub use filename::*; pub use html::*; pub use module::*; diff --git a/crates/rspack_binding_values/src/module.rs b/crates/rspack_binding_values/src/module.rs index 51c269db853..a9bee043681 100644 --- a/crates/rspack_binding_values/src/module.rs +++ b/crates/rspack_binding_values/src/module.rs @@ -14,7 +14,7 @@ use rustc_hash::FxHashMap as HashMap; use sys::napi_env; use super::{JsCompatSource, ToJsCompatSource}; -use crate::{DependencyDTO, JsChunk, JsCodegenerationResults}; +use crate::{JsChunk, JsCodegenerationResults, JsCompiledDependency}; #[derive(Default)] #[napi(object)] @@ -52,14 +52,14 @@ impl DependenciesBlockDTO { #[napi] impl DependenciesBlockDTO { #[napi(getter)] - pub fn dependencies(&self) -> Vec { + pub fn dependencies(&self) -> Vec { let module_graph = self.compilation.get_module_graph(); let block = self.block(&module_graph); block .get_dependencies() .iter() .cloned() - .map(|dep_id| DependencyDTO::new(dep_id, self.compilation)) + .map(|dep_id| JsCompiledDependency::new(dep_id, self.compilation)) .collect::>() } diff --git a/packages/rspack/src/DependenciesBlock.ts b/packages/rspack/src/DependenciesBlock.ts index 452d63bd62c..7879c43ebd7 100644 --- a/packages/rspack/src/DependenciesBlock.ts +++ b/packages/rspack/src/DependenciesBlock.ts @@ -9,7 +9,7 @@ export class DependenciesBlock { } get dependencies(): Dependency[] { - return this.#binding.dependencies.map(d => new Dependency(d)); + return this.#binding.dependencies.map(d => Dependency.__from_binding(d)); } get blocks(): DependenciesBlock[] { diff --git a/packages/rspack/src/Dependency.ts b/packages/rspack/src/Dependency.ts index d0aacf04701..1ff255af68a 100644 --- a/packages/rspack/src/Dependency.ts +++ b/packages/rspack/src/Dependency.ts @@ -1,9 +1,15 @@ -import type { DependencyDTO } from "@rspack/binding"; +import { JsDependency, JsCompiledDependency } from "@rspack/binding"; export class Dependency { - #binding: DependencyDTO; + #binding: JsDependency | JsCompiledDependency; - constructor(binding: DependencyDTO) { + static __from_binding( + binding: JsDependency | JsCompiledDependency + ): Dependency { + return new Dependency(binding); + } + + private constructor(binding: JsDependency | JsCompiledDependency) { this.#binding = binding; } @@ -18,4 +24,17 @@ export class Dependency { get request(): string | undefined { return this.#binding.request; } + + get critital(): boolean | undefined { + return this.#binding.critical; + } + + set critital(critital: boolean | undefined) { + if ( + typeof critital === "boolean" && + this.#binding instanceof JsDependency + ) { + this.#binding.critical = critital; + } + } } diff --git a/packages/rspack/src/Module.ts b/packages/rspack/src/Module.ts index a83e3dbf2ec..774ac5a3073 100644 --- a/packages/rspack/src/Module.ts +++ b/packages/rspack/src/Module.ts @@ -10,7 +10,7 @@ import type { Source } from "webpack-sources"; import type { Compilation } from "./Compilation"; import { DependenciesBlock } from "./DependenciesBlock"; -import type { Dependency } from "./Dependency"; +import { Dependency } from "./Dependency"; import { JsSource } from "./util/source"; export type ResourceData = { @@ -110,8 +110,7 @@ export class ContextModuleFactoryAfterResolveData { } get dependencies(): Dependency[] { - // TODO: Dependencies are not fully supported yet; this is a placeholder to prevent errors in moment-locales-webpack-plugin. - return []; + return this.#inner.dependencies.map(dep => Dependency.__from_binding(dep)); } } From 9cc09f40b74fc24d14811ea644f8bce3d7f0d7f2 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Mon, 23 Sep 2024 16:04:33 +0800 Subject: [PATCH 2/9] feat: ContextModuleFactoryBeforeResolveData --- .../src/context_module_factory.rs | 6 +- .../rspack_binding_values/src/dependency.rs | 14 +- packages/rspack/src/Compiler.ts | 24 +++- packages/rspack/src/Dependency.ts | 18 +++ packages/rspack/src/Module.ts | 131 +++++++++++++++++- 5 files changed, 176 insertions(+), 17 deletions(-) diff --git a/crates/rspack_binding_values/src/context_module_factory.rs b/crates/rspack_binding_values/src/context_module_factory.rs index 6f20e1c0125..3f980798ce0 100644 --- a/crates/rspack_binding_values/src/context_module_factory.rs +++ b/crates/rspack_binding_values/src/context_module_factory.rs @@ -175,12 +175,12 @@ impl JsContextModuleFactoryAfterResolveData { } #[napi(getter)] - pub fn dependencies(&self) -> Vec { + pub fn dependencies(&mut self) -> Vec { self .0 .dependencies - .iter() - .map(|dep| JsDependency::new(dep.clone())) + .iter_mut() + .map(|dep| JsDependency::new(dep)) .collect::>() } } diff --git a/crates/rspack_binding_values/src/dependency.rs b/crates/rspack_binding_values/src/dependency.rs index b61fb8e2182..ae84d2bc218 100644 --- a/crates/rspack_binding_values/src/dependency.rs +++ b/crates/rspack_binding_values/src/dependency.rs @@ -4,6 +4,8 @@ use rspack_core::{ ModuleGraph, }; +// JsCompiledDependency allows JS-side access to a Dependency instance that has already +// been processed and stored in the Compilation. #[napi] pub struct JsCompiledDependency { pub(crate) dependency_id: DependencyId, @@ -65,11 +67,19 @@ impl JsCompiledDependency { } } +// JsDependency represents a Dependency instance that is currently being processed. +// It is in the make stage and has not yet been added to the Compilation. #[napi] -pub struct JsDependency(BoxDependency); +pub struct JsDependency(&'static mut BoxDependency); impl JsDependency { - pub(crate) fn new(dependency: BoxDependency) -> Self { + pub(crate) fn new(dependency: &mut BoxDependency) -> Self { + // SAFETY: + // The lifetime of the &mut BoxDependency reference is extended to 'static. + // This is safe because the JS side will guarantee that the JsDependency instance's + // lifetime is properly managed and restricted. + let dependency = + unsafe { std::mem::transmute::<&mut BoxDependency, &'static mut BoxDependency>(dependency) }; Self(dependency) } } diff --git a/packages/rspack/src/Compiler.ts b/packages/rspack/src/Compiler.ts index 8400d8e09f2..8e38aefb59d 100644 --- a/packages/rspack/src/Compiler.ts +++ b/packages/rspack/src/Compiler.ts @@ -33,6 +33,7 @@ import { ThreadsafeWritableNodeFS } from "./FileSystem"; import { CodeGenerationResult, ContextModuleFactoryAfterResolveData, + ContextModuleFactoryBeforeResolveData, Module } from "./Module"; import { NormalModuleFactory } from "./NormalModuleFactory"; @@ -1159,7 +1160,18 @@ class Compiler { | false | binding.JsContextModuleFactoryBeforeResolveData ) => { - return queried.promise(bindingData); + const data = bindingData + ? ContextModuleFactoryBeforeResolveData.__from_binding( + bindingData + ) + : false; + const result = await queried.promise(data); + if (data) { + ContextModuleFactoryBeforeResolveData.__drop(data); + } + return result + ? ContextModuleFactoryBeforeResolveData.__to_binding(result) + : false; } ), registerContextModuleFactoryAfterResolveTaps: @@ -1178,11 +1190,13 @@ class Compiler { bindingData ) : false; - const ret = await queried.promise(data); - const result = ret - ? ContextModuleFactoryAfterResolveData.__to_binding(ret) + const result = await queried.promise(data); + if (data) { + ContextModuleFactoryAfterResolveData.__drop(data); + } + return result + ? ContextModuleFactoryAfterResolveData.__to_binding(result) : false; - return result; } ), registerJavascriptModulesChunkHashTaps: this.#createHookRegisterTaps( diff --git a/packages/rspack/src/Dependency.ts b/packages/rspack/src/Dependency.ts index 1ff255af68a..2c037d147e8 100644 --- a/packages/rspack/src/Dependency.ts +++ b/packages/rspack/src/Dependency.ts @@ -2,6 +2,7 @@ import { JsDependency, JsCompiledDependency } from "@rspack/binding"; export class Dependency { #binding: JsDependency | JsCompiledDependency; + #dropped = false; static __from_binding( binding: JsDependency | JsCompiledDependency @@ -9,27 +10,44 @@ export class Dependency { return new Dependency(binding); } + static __drop(dependency: Dependency) { + dependency.#dropped = true; + } + + private ensureValidLifecycle() { + if (this.#dropped) { + throw new Error( + "The Dependency has exceeded its lifecycle and has been dropped by Rust." + ); + } + } + private constructor(binding: JsDependency | JsCompiledDependency) { this.#binding = binding; } get type(): string { + this.ensureValidLifecycle(); return this.#binding.type; } get category(): string { + this.ensureValidLifecycle(); return this.#binding.category; } get request(): string | undefined { + this.ensureValidLifecycle(); return this.#binding.request; } get critital(): boolean | undefined { + this.ensureValidLifecycle(); return this.#binding.critical; } set critital(critital: boolean | undefined) { + this.ensureValidLifecycle(); if ( typeof critital === "boolean" && this.#binding instanceof JsDependency diff --git a/packages/rspack/src/Module.ts b/packages/rspack/src/Module.ts index 774ac5a3073..8b38314d547 100644 --- a/packages/rspack/src/Module.ts +++ b/packages/rspack/src/Module.ts @@ -1,6 +1,7 @@ import type { JsCodegenerationResult, JsContextModuleFactoryAfterResolveData, + JsContextModuleFactoryBeforeResolveData, JsCreateData, JsFactoryMeta, JsModule, @@ -36,45 +37,151 @@ export type ResolveData = { createData?: CreateData; }; +export class ContextModuleFactoryBeforeResolveData { + #inner: JsContextModuleFactoryBeforeResolveData; + #dropped = false; + + static __from_binding(binding: JsContextModuleFactoryBeforeResolveData) { + return new ContextModuleFactoryBeforeResolveData(binding); + } + + static __to_binding( + data: ContextModuleFactoryBeforeResolveData + ): JsContextModuleFactoryBeforeResolveData { + return data.#inner; + } + + static __drop(data: ContextModuleFactoryBeforeResolveData) { + data.#dropped = true; + } + + private constructor(binding: JsContextModuleFactoryBeforeResolveData) { + this.#inner = binding; + } + + private ensureValidLifecycle() { + if (this.#dropped) { + throw new Error( + "The ContextModuleFactoryBeforeResolveData has exceeded its lifecycle and has been dropped by Rust." + ); + } + } + + get context(): string { + this.ensureValidLifecycle(); + return this.#inner.context; + } + + set context(val: string) { + this.ensureValidLifecycle(); + this.#inner.context = val; + } + + get request(): string { + this.ensureValidLifecycle(); + return this.#inner.request; + } + + set request(val: string) { + this.#inner.request = val; + } + + get regExp(): RegExp | undefined { + this.ensureValidLifecycle(); + if (!this.#inner.regExp) { + return undefined; + } + const { source, flags } = this.#inner.regExp; + return new RegExp(source, flags); + } + + set regExp(val: RegExp | undefined) { + this.ensureValidLifecycle(); + if (!val) { + this.#inner.regExp = undefined; + return; + } + this.#inner.regExp = { + source: val.source, + flags: val.flags + }; + } + + get recursive(): boolean { + this.ensureValidLifecycle(); + return this.#inner.recursive; + } + + set recursive(val: boolean) { + this.ensureValidLifecycle(); + this.#inner.recursive = val; + } +} + export type ContextModuleFactoryBeforeResolveResult = | false - | { - context: string; - request?: string; - }; + | ContextModuleFactoryBeforeResolveData; export class ContextModuleFactoryAfterResolveData { #inner: JsContextModuleFactoryAfterResolveData; + #resolvedDependencies?: Dependency[]; + #dropped = false; + #dropWarningMessage?: string; static __from_binding(binding: JsContextModuleFactoryAfterResolveData) { return new ContextModuleFactoryAfterResolveData(binding); } - static __to_binding(data: ContextModuleFactoryAfterResolveData) { + static __to_binding( + data: ContextModuleFactoryAfterResolveData + ): JsContextModuleFactoryAfterResolveData { return data.#inner; } - constructor(data: JsContextModuleFactoryAfterResolveData) { + static __drop(data: ContextModuleFactoryAfterResolveData) { + data.#dropped = true; + if (data.#resolvedDependencies) { + data.#resolvedDependencies.forEach(dependency => + Dependency.__drop(dependency) + ); + } + } + + private ensureValidLifecycle() { + if (this.#dropped) { + throw new Error( + this.#dropWarningMessage ?? + "The ContextModuleFactoryAfterResolveData has exceeded its lifecycle and has been dropped by Rust." + ); + } + } + + private constructor(data: JsContextModuleFactoryAfterResolveData) { this.#inner = data; } get resource(): string { + this.ensureValidLifecycle(); return this.#inner.resource; } set resource(val: string) { + this.ensureValidLifecycle(); this.#inner.resource = val; } get context(): string { + this.ensureValidLifecycle(); return this.#inner.context; } set context(val: string) { + this.ensureValidLifecycle(); this.#inner.context = val; } get request(): string { + this.ensureValidLifecycle(); return this.#inner.request; } @@ -83,6 +190,7 @@ export class ContextModuleFactoryAfterResolveData { } get regExp(): RegExp | undefined { + this.ensureValidLifecycle(); if (!this.#inner.regExp) { return undefined; } @@ -91,6 +199,7 @@ export class ContextModuleFactoryAfterResolveData { } set regExp(val: RegExp | undefined) { + this.ensureValidLifecycle(); if (!val) { this.#inner.regExp = undefined; return; @@ -102,15 +211,23 @@ export class ContextModuleFactoryAfterResolveData { } get recursive(): boolean { + this.ensureValidLifecycle(); return this.#inner.recursive; } set recursive(val: boolean) { + this.ensureValidLifecycle(); this.#inner.recursive = val; } get dependencies(): Dependency[] { - return this.#inner.dependencies.map(dep => Dependency.__from_binding(dep)); + this.ensureValidLifecycle(); + if (!this.#resolvedDependencies) { + this.#resolvedDependencies = this.#inner.dependencies.map(dep => + Dependency.__from_binding(dep) + ); + } + return this.#resolvedDependencies; } } From c69a36177d884f56675d54b9fce5a4aa6eed0da1 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Mon, 23 Sep 2024 16:09:44 +0800 Subject: [PATCH 3/9] feat: update test case --- crates/node_binding/binding.d.ts | 2 +- .../rspack_binding_values/src/dependency.rs | 16 +++---- .../rspack.config.js | 4 ++ packages/rspack/etc/api.md | 42 +++++++++++++++---- packages/rspack/src/Dependency.ts | 2 +- packages/rspack/src/Module.ts | 10 ++--- 6 files changed, 54 insertions(+), 22 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 2b39aa7414f..244d776acb5 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -150,7 +150,7 @@ export class JsDependency { get category(): string get request(): string | undefined get critical(): boolean | undefined - set critical(val: boolean) + set critical(val?: boolean | undefined | null) } export class JsEntries { diff --git a/crates/rspack_binding_values/src/dependency.rs b/crates/rspack_binding_values/src/dependency.rs index ae84d2bc218..97c62d0a3ca 100644 --- a/crates/rspack_binding_values/src/dependency.rs +++ b/crates/rspack_binding_values/src/dependency.rs @@ -113,15 +113,17 @@ impl JsDependency { } #[napi(setter)] - pub fn set_critical(&mut self, val: bool) { - match self.0.as_context_dependency_mut() { - Some(dep) => { - let critical = dep.critical_mut(); - if !val { - *critical = None; + pub fn set_critical(&mut self, val: Option) { + if let Some(val) = val { + match self.0.as_context_dependency_mut() { + Some(dep) => { + let critical = dep.critical_mut(); + if !val { + *critical = None; + } } + None => (), } - None => (), } } } diff --git a/packages/rspack-test-tools/tests/configCases/hooks/context-module-before-resolve/rspack.config.js b/packages/rspack-test-tools/tests/configCases/hooks/context-module-before-resolve/rspack.config.js index dfb8bfdac3f..52848a17dd5 100644 --- a/packages/rspack-test-tools/tests/configCases/hooks/context-module-before-resolve/rspack.config.js +++ b/packages/rspack-test-tools/tests/configCases/hooks/context-module-before-resolve/rspack.config.js @@ -10,11 +10,15 @@ class Plugin { resolveData.regExp = /[/\\](en(\.js)?|zh(\.js)?)$/; return resolveData; } + for (const d of resolveData.dependencies) { + if (d.critical) d.critical = false; + } }); } ); } } + /**@type {import("@rspack/core").Configuration}*/ module.exports = { context: __dirname, diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index ba62178c52a..ba0eaf630db 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -18,7 +18,6 @@ import { cleanupGlobalTrace } from '@rspack/binding'; import { Compiler as Compiler_2 } from '../Compiler'; import { default as default_2 } from './util/hash'; import type { DependenciesBlockDTO } from '@rspack/binding'; -import type { DependencyDTO } from '@rspack/binding'; import { RawEvalDevToolModulePluginOptions as EvalDevToolModulePluginOptions } from '@rspack/binding'; import { EventEmitter } from 'events'; import { ExternalObject } from '@rspack/binding'; @@ -39,8 +38,11 @@ import { JsChunkGroup } from '@rspack/binding'; import { JsChunkGroupOrigin } from '@rspack/binding'; import type { JsCodegenerationResult } from '@rspack/binding'; import { JsCompilation } from '@rspack/binding'; +import { JsCompiledDependency } from '@rspack/binding'; import type { JsContextModuleFactoryAfterResolveData } from '@rspack/binding'; +import type { JsContextModuleFactoryBeforeResolveData } from '@rspack/binding'; import type { JsCreateData } from '@rspack/binding'; +import { JsDependency } from '@rspack/binding'; import type { JsFactoryMeta } from '@rspack/binding'; import { JsHtmlPluginTag } from '@rspack/binding'; import { JsLibraryOptions } from '@rspack/binding'; @@ -1577,7 +1579,8 @@ class ContextModuleFactory { // @public (undocumented) class ContextModuleFactoryAfterResolveData { - constructor(data: JsContextModuleFactoryAfterResolveData); + // (undocumented) + static __drop(data: ContextModuleFactoryAfterResolveData): void; // (undocumented) static __from_binding(binding: JsContextModuleFactoryAfterResolveData): ContextModuleFactoryAfterResolveData; // (undocumented) @@ -1605,10 +1608,29 @@ class ContextModuleFactoryAfterResolveData { type ContextModuleFactoryAfterResolveResult = false | ContextModuleFactoryAfterResolveData; // @public (undocumented) -type ContextModuleFactoryBeforeResolveResult = false | { - context: string; - request?: string; -}; +class ContextModuleFactoryBeforeResolveData { + // (undocumented) + static __drop(data: ContextModuleFactoryBeforeResolveData): void; + // (undocumented) + static __from_binding(binding: JsContextModuleFactoryBeforeResolveData): ContextModuleFactoryBeforeResolveData; + // (undocumented) + static __to_binding(data: ContextModuleFactoryBeforeResolveData): JsContextModuleFactoryBeforeResolveData; + // (undocumented) + get context(): string; + set context(val: string); + // (undocumented) + get recursive(): boolean; + set recursive(val: boolean); + // (undocumented) + get regExp(): RegExp | undefined; + set regExp(val: RegExp | undefined); + // (undocumented) + get request(): string; + set request(val: string); +} + +// @public (undocumented) +type ContextModuleFactoryBeforeResolveResult = false | ContextModuleFactoryBeforeResolveData; // @public (undocumented) export const ContextReplacementPlugin: { @@ -1866,10 +1888,16 @@ class DependenciesBlock { // @public (undocumented) class Dependency { - constructor(binding: DependencyDTO); + // (undocumented) + static __drop(dependency: Dependency): void; + // (undocumented) + static __from_binding(binding: JsDependency | JsCompiledDependency): Dependency; // (undocumented) get category(): string; // (undocumented) + get critital(): boolean | undefined; + set critital(critital: boolean | undefined); + // (undocumented) get request(): string | undefined; // (undocumented) get type(): string; diff --git a/packages/rspack/src/Dependency.ts b/packages/rspack/src/Dependency.ts index 2c037d147e8..3eb8be9860d 100644 --- a/packages/rspack/src/Dependency.ts +++ b/packages/rspack/src/Dependency.ts @@ -1,4 +1,4 @@ -import { JsDependency, JsCompiledDependency } from "@rspack/binding"; +import { type JsCompiledDependency, JsDependency } from "@rspack/binding"; export class Dependency { #binding: JsDependency | JsCompiledDependency; diff --git a/packages/rspack/src/Module.ts b/packages/rspack/src/Module.ts index 8b38314d547..20e782d366d 100644 --- a/packages/rspack/src/Module.ts +++ b/packages/rspack/src/Module.ts @@ -126,7 +126,6 @@ export class ContextModuleFactoryAfterResolveData { #inner: JsContextModuleFactoryAfterResolveData; #resolvedDependencies?: Dependency[]; #dropped = false; - #dropWarningMessage?: string; static __from_binding(binding: JsContextModuleFactoryAfterResolveData) { return new ContextModuleFactoryAfterResolveData(binding); @@ -141,17 +140,16 @@ export class ContextModuleFactoryAfterResolveData { static __drop(data: ContextModuleFactoryAfterResolveData) { data.#dropped = true; if (data.#resolvedDependencies) { - data.#resolvedDependencies.forEach(dependency => - Dependency.__drop(dependency) - ); + for (const dependency of data.#resolvedDependencies) { + Dependency.__drop(dependency); + } } } private ensureValidLifecycle() { if (this.#dropped) { throw new Error( - this.#dropWarningMessage ?? - "The ContextModuleFactoryAfterResolveData has exceeded its lifecycle and has been dropped by Rust." + "The ContextModuleFactoryAfterResolveData has exceeded its lifecycle and has been dropped by Rust." ); } } From 7d6519c4ef313cabbee7c50e3d6e722c28f148e5 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Mon, 23 Sep 2024 16:32:44 +0800 Subject: [PATCH 4/9] fix: cargo lint --- crates/node_binding/binding.d.ts | 6 ++-- .../src/context_module_factory.rs | 2 +- .../rspack_binding_values/src/dependency.rs | 33 ++++++++----------- packages/rspack/src/Dependency.ts | 4 +-- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 244d776acb5..d04d7b9e6c5 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -117,7 +117,7 @@ export class JsCompiledDependency { get type(): string get category(): string get request(): string | undefined - get critical(): boolean | undefined + get critical(): boolean } export class JsContextModuleFactoryAfterResolveData { @@ -149,8 +149,8 @@ export class JsDependency { get type(): string get category(): string get request(): string | undefined - get critical(): boolean | undefined - set critical(val?: boolean | undefined | null) + get critical(): boolean + set critical(val: boolean) } export class JsEntries { diff --git a/crates/rspack_binding_values/src/context_module_factory.rs b/crates/rspack_binding_values/src/context_module_factory.rs index 3f980798ce0..9951ff2add3 100644 --- a/crates/rspack_binding_values/src/context_module_factory.rs +++ b/crates/rspack_binding_values/src/context_module_factory.rs @@ -180,7 +180,7 @@ impl JsContextModuleFactoryAfterResolveData { .0 .dependencies .iter_mut() - .map(|dep| JsDependency::new(dep)) + .map(JsDependency::new) .collect::>() } } diff --git a/crates/rspack_binding_values/src/dependency.rs b/crates/rspack_binding_values/src/dependency.rs index 97c62d0a3ca..68542e5e812 100644 --- a/crates/rspack_binding_values/src/dependency.rs +++ b/crates/rspack_binding_values/src/dependency.rs @@ -21,7 +21,7 @@ impl JsCompiledDependency { } } - fn dependency<'a>(&self) -> &dyn Dependency { + fn dependency(&self) -> &dyn Dependency { self .module_graph .dependency_by_id(&self.dependency_id) @@ -29,11 +29,11 @@ impl JsCompiledDependency { .as_ref() } - fn module_dependency<'a>(&self) -> Option<&dyn ModuleDependency> { + fn module_dependency(&self) -> Option<&dyn ModuleDependency> { self.dependency().as_module_dependency() } - fn context_dependency<'a>(&self) -> Option<&dyn ContextDependency> { + fn context_dependency(&self) -> Option<&dyn ContextDependency> { self.dependency().as_context_dependency() } } @@ -59,10 +59,10 @@ impl JsCompiledDependency { } #[napi(getter)] - pub fn critical(&self) -> napi::Either { + pub fn critical(&self) -> bool { match self.context_dependency() { - Some(dep) => napi::Either::A(dep.critical().is_some()), - None => napi::Either::B(()), + Some(dep) => dep.critical().is_some(), + None => false, } } } @@ -105,24 +105,19 @@ impl JsDependency { } #[napi(getter)] - pub fn critical(&self) -> napi::Either { + pub fn critical(&self) -> bool { match self.0.as_context_dependency() { - Some(dep) => napi::Either::A(dep.critical().is_some()), - None => napi::Either::B(()), + Some(dep) => dep.critical().is_some(), + None => false, } } #[napi(setter)] - pub fn set_critical(&mut self, val: Option) { - if let Some(val) = val { - match self.0.as_context_dependency_mut() { - Some(dep) => { - let critical = dep.critical_mut(); - if !val { - *critical = None; - } - } - None => (), + pub fn set_critical(&mut self, val: bool) { + if let Some(dep) = self.0.as_context_dependency_mut() { + let critical = dep.critical_mut(); + if !val { + *critical = None; } } } diff --git a/packages/rspack/src/Dependency.ts b/packages/rspack/src/Dependency.ts index 3eb8be9860d..9864ee2155b 100644 --- a/packages/rspack/src/Dependency.ts +++ b/packages/rspack/src/Dependency.ts @@ -41,12 +41,12 @@ export class Dependency { return this.#binding.request; } - get critital(): boolean | undefined { + get critital(): boolean { this.ensureValidLifecycle(); return this.#binding.critical; } - set critital(critital: boolean | undefined) { + set critital(critital: boolean) { this.ensureValidLifecycle(); if ( typeof critital === "boolean" && From 261c8fcf06844bf3f9b7d87c3541bc67fcc4efd9 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Mon, 23 Sep 2024 17:01:57 +0800 Subject: [PATCH 5/9] chore: update api docs --- packages/rspack/etc/api.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index ba0eaf630db..3f82642a563 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -1895,8 +1895,8 @@ class Dependency { // (undocumented) get category(): string; // (undocumented) - get critital(): boolean | undefined; - set critital(critital: boolean | undefined); + get critital(): boolean; + set critital(critital: boolean); // (undocumented) get request(): string | undefined; // (undocumented) From c1f8f8e79fb725251580be733d9a1101071e936e Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Mon, 23 Sep 2024 20:30:31 +0800 Subject: [PATCH 6/9] feat: JsDependency and JsDependencyMut --- crates/node_binding/binding.d.ts | 26 ++++---- .../src/compilation/entries.rs | 45 +++++++------- .../src/context_module_factory.rs | 6 +- .../rspack_binding_values/src/dependency.rs | 60 +++++++------------ crates/rspack_binding_values/src/module.rs | 11 ++-- packages/rspack/etc/api.md | 4 +- packages/rspack/src/Dependency.ts | 12 ++-- 7 files changed, 73 insertions(+), 91 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index d04d7b9e6c5..bf888a9bd9a 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -20,7 +20,7 @@ export class ExternalObject { } } export class DependenciesBlockDto { - get dependencies(): Array + get dependencies(): Array get blocks(): Array } export type DependenciesBlockDTO = DependenciesBlockDto @@ -42,8 +42,8 @@ export class DependenciesDto { export type DependenciesDTO = DependenciesDto export class EntryDataDto { - get dependencies(): Array - get includeDependencies(): Array + get dependencies(): Array + get includeDependencies(): Array get options(): EntryOptionsDto } export type EntryDataDTO = EntryDataDto @@ -113,13 +113,6 @@ export class JsCompilation { addRuntimeModule(chunkUkey: number, runtimeModule: JsAddingRuntimeModule): void } -export class JsCompiledDependency { - get type(): string - get category(): string - get request(): string | undefined - get critical(): boolean -} - export class JsContextModuleFactoryAfterResolveData { get resource(): string set resource(resource: string) @@ -131,7 +124,7 @@ export class JsContextModuleFactoryAfterResolveData { set regExp(rawRegExp: RawRegex | undefined) get recursive(): boolean set recursive(recursive: boolean) - get dependencies(): Array + get dependencies(): Array } export class JsContextModuleFactoryBeforeResolveData { @@ -150,6 +143,13 @@ export class JsDependency { get category(): string get request(): string | undefined get critical(): boolean +} + +export class JsDependencyMut { + get type(): string + get category(): string + get request(): string | undefined + get critical(): boolean set critical(val: boolean) } @@ -534,8 +534,8 @@ export interface JsDiagnosticLocation { } export interface JsEntryData { - dependencies: Array - includeDependencies: Array + dependencies: Array + includeDependencies: Array options: JsEntryOptions } diff --git a/crates/rspack_binding_values/src/compilation/entries.rs b/crates/rspack_binding_values/src/compilation/entries.rs index a970d28816a..03be0ca4ca4 100644 --- a/crates/rspack_binding_values/src/compilation/entries.rs +++ b/crates/rspack_binding_values/src/compilation/entries.rs @@ -2,7 +2,7 @@ use napi_derive::napi; use rspack_core::{ChunkLoading, Compilation, EntryData, EntryOptions, EntryRuntime}; use rspack_napi::napi::bindgen_prelude::*; -use crate::{dependency::JsCompiledDependency, entry::JsEntryOptions, library::JsLibraryOptions}; +use crate::{dependency::JsDependency, entry::JsEntryOptions, library::JsLibraryOptions}; #[napi] pub struct EntryOptionsDTO(EntryOptions); @@ -152,8 +152,8 @@ impl EntryOptionsDTO { #[napi(object, object_to_js = false)] pub struct JsEntryData { - pub dependencies: Vec>, - pub include_dependencies: Vec>, + pub dependencies: Vec>, + pub include_dependencies: Vec>, pub options: JsEntryOptions, } @@ -163,12 +163,12 @@ impl From for EntryData { dependencies: value .dependencies .into_iter() - .map(|dep| dep.dependency_id) + .map(|dep| *dep.id()) .collect::>(), include_dependencies: value .include_dependencies .into_iter() - .map(|dep| dep.dependency_id) + .map(|dep| *dep.id()) .collect::>(), options: value.options.into(), } @@ -184,36 +184,33 @@ pub struct EntryDataDTO { #[napi] impl EntryDataDTO { #[napi(getter)] - pub fn dependencies(&'static self, env: Env) -> Result>> { + pub fn dependencies(&'static self) -> Vec { + let module_graph = self.compilation.get_module_graph(); self .entry_data .dependencies - .clone() - .into_iter() - .map(|id| { - let js_dep = JsCompiledDependency::new(id, self.compilation); - let instance = js_dep.into_instance(env)?; - Ok(instance) + .iter() + .map(|dependency_id| { + #[allow(clippy::unwrap_used)] + let dep = module_graph.dependency_by_id(dependency_id).unwrap(); + JsDependency::new(dep) }) - .collect::>>>() + .collect::>() } #[napi(getter)] - pub fn include_dependencies( - &'static self, - env: Env, - ) -> Result>> { + pub fn include_dependencies(&'static self) -> Vec { + let module_graph = self.compilation.get_module_graph(); self .entry_data .include_dependencies - .clone() - .into_iter() - .map(|id| { - let js_dep = JsCompiledDependency::new(id, self.compilation); - let instance = js_dep.into_instance(env)?; - Ok(instance) + .iter() + .map(|dependency_id| { + #[allow(clippy::unwrap_used)] + let dep = module_graph.dependency_by_id(dependency_id).unwrap(); + JsDependency::new(dep) }) - .collect::>>>() + .collect::>() } #[napi(getter)] diff --git a/crates/rspack_binding_values/src/context_module_factory.rs b/crates/rspack_binding_values/src/context_module_factory.rs index 9951ff2add3..3dbff18b36c 100644 --- a/crates/rspack_binding_values/src/context_module_factory.rs +++ b/crates/rspack_binding_values/src/context_module_factory.rs @@ -4,7 +4,7 @@ use napi::bindgen_prelude::{ use napi_derive::napi; use rspack_core::{AfterResolveData, BeforeResolveData}; -use crate::{JsDependency, RawRegex}; +use crate::{JsDependencyMut, RawRegex}; #[napi] pub struct JsContextModuleFactoryBeforeResolveData(Box); @@ -175,12 +175,12 @@ impl JsContextModuleFactoryAfterResolveData { } #[napi(getter)] - pub fn dependencies(&mut self) -> Vec { + pub fn dependencies(&mut self) -> Vec { self .0 .dependencies .iter_mut() - .map(JsDependency::new) + .map(JsDependencyMut::new) .collect::>() } } diff --git a/crates/rspack_binding_values/src/dependency.rs b/crates/rspack_binding_values/src/dependency.rs index 68542e5e812..7e177a35b56 100644 --- a/crates/rspack_binding_values/src/dependency.rs +++ b/crates/rspack_binding_values/src/dependency.rs @@ -1,58 +1,42 @@ use napi_derive::napi; -use rspack_core::{ - BoxDependency, Compilation, ContextDependency, Dependency, DependencyId, ModuleDependency, - ModuleGraph, -}; +use rspack_core::{BoxDependency, DependencyId}; -// JsCompiledDependency allows JS-side access to a Dependency instance that has already +// JsDependency allows JS-side access to a Dependency instance that has already // been processed and stored in the Compilation. #[napi] -pub struct JsCompiledDependency { - pub(crate) dependency_id: DependencyId, - pub(crate) module_graph: ModuleGraph<'static>, -} - -impl JsCompiledDependency { - pub(crate) fn new(dependency_id: DependencyId, compilation: &'static Compilation) -> Self { - let module_graph = compilation.get_module_graph(); - Self { - dependency_id, - module_graph, - } - } - - fn dependency(&self) -> &dyn Dependency { - self - .module_graph - .dependency_by_id(&self.dependency_id) - .unwrap_or_else(|| panic!("Failed to get dependency by id = {:?}", &self.dependency_id)) - .as_ref() - } +pub struct JsDependency(&'static BoxDependency); - fn module_dependency(&self) -> Option<&dyn ModuleDependency> { - self.dependency().as_module_dependency() +impl JsDependency { + pub(crate) fn new(dependency: &BoxDependency) -> Self { + // SAFETY: + // The lifetime of the &mut BoxDependency reference is extended to 'static. + // This is safe because the JS side will guarantee that the JsDependency instance's + // lifetime is properly managed and restricted. + let dependency = + unsafe { std::mem::transmute::<&BoxDependency, &'static BoxDependency>(dependency) }; + Self(dependency) } - fn context_dependency(&self) -> Option<&dyn ContextDependency> { - self.dependency().as_context_dependency() + pub(crate) fn id(&self) -> &DependencyId { + self.0.id() } } #[napi] -impl JsCompiledDependency { +impl JsDependency { #[napi(getter)] pub fn get_type(&self) -> &str { - self.dependency().dependency_type().as_str() + self.0.dependency_type().as_str() } #[napi(getter)] pub fn category(&self) -> &str { - self.dependency().category().as_str() + self.0.category().as_str() } #[napi(getter)] pub fn request(&self) -> napi::Either<&str, ()> { - match self.module_dependency() { + match self.0.as_module_dependency() { Some(dep) => napi::Either::A(dep.request()), None => napi::Either::B(()), } @@ -60,7 +44,7 @@ impl JsCompiledDependency { #[napi(getter)] pub fn critical(&self) -> bool { - match self.context_dependency() { + match self.0.as_context_dependency() { Some(dep) => dep.critical().is_some(), None => false, } @@ -70,9 +54,9 @@ impl JsCompiledDependency { // JsDependency represents a Dependency instance that is currently being processed. // It is in the make stage and has not yet been added to the Compilation. #[napi] -pub struct JsDependency(&'static mut BoxDependency); +pub struct JsDependencyMut(&'static mut BoxDependency); -impl JsDependency { +impl JsDependencyMut { pub(crate) fn new(dependency: &mut BoxDependency) -> Self { // SAFETY: // The lifetime of the &mut BoxDependency reference is extended to 'static. @@ -85,7 +69,7 @@ impl JsDependency { } #[napi] -impl JsDependency { +impl JsDependencyMut { #[napi(getter)] pub fn get_type(&self) -> &str { self.0.dependency_type().as_str() diff --git a/crates/rspack_binding_values/src/module.rs b/crates/rspack_binding_values/src/module.rs index a9bee043681..600e91d4f9d 100644 --- a/crates/rspack_binding_values/src/module.rs +++ b/crates/rspack_binding_values/src/module.rs @@ -14,7 +14,7 @@ use rustc_hash::FxHashMap as HashMap; use sys::napi_env; use super::{JsCompatSource, ToJsCompatSource}; -use crate::{JsChunk, JsCodegenerationResults, JsCompiledDependency}; +use crate::{JsChunk, JsCodegenerationResults, JsDependency}; #[derive(Default)] #[napi(object)] @@ -52,14 +52,17 @@ impl DependenciesBlockDTO { #[napi] impl DependenciesBlockDTO { #[napi(getter)] - pub fn dependencies(&self) -> Vec { + pub fn dependencies(&self) -> Vec { let module_graph = self.compilation.get_module_graph(); let block = self.block(&module_graph); block .get_dependencies() .iter() - .cloned() - .map(|dep_id| JsCompiledDependency::new(dep_id, self.compilation)) + .map(|dependency_id| { + #[allow(clippy::unwrap_used)] + let dep = module_graph.dependency_by_id(dependency_id).unwrap(); + JsDependency::new(dep) + }) .collect::>() } diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index 3f82642a563..deab01cd8f8 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -38,11 +38,11 @@ import { JsChunkGroup } from '@rspack/binding'; import { JsChunkGroupOrigin } from '@rspack/binding'; import type { JsCodegenerationResult } from '@rspack/binding'; import { JsCompilation } from '@rspack/binding'; -import { JsCompiledDependency } from '@rspack/binding'; import type { JsContextModuleFactoryAfterResolveData } from '@rspack/binding'; import type { JsContextModuleFactoryBeforeResolveData } from '@rspack/binding'; import type { JsCreateData } from '@rspack/binding'; import { JsDependency } from '@rspack/binding'; +import { JsDependencyMut } from '@rspack/binding'; import type { JsFactoryMeta } from '@rspack/binding'; import { JsHtmlPluginTag } from '@rspack/binding'; import { JsLibraryOptions } from '@rspack/binding'; @@ -1891,7 +1891,7 @@ class Dependency { // (undocumented) static __drop(dependency: Dependency): void; // (undocumented) - static __from_binding(binding: JsDependency | JsCompiledDependency): Dependency; + static __from_binding(binding: JsDependencyMut | JsDependency): Dependency; // (undocumented) get category(): string; // (undocumented) diff --git a/packages/rspack/src/Dependency.ts b/packages/rspack/src/Dependency.ts index 9864ee2155b..8379aa30576 100644 --- a/packages/rspack/src/Dependency.ts +++ b/packages/rspack/src/Dependency.ts @@ -1,12 +1,10 @@ -import { type JsCompiledDependency, JsDependency } from "@rspack/binding"; +import { type JsDependency, JsDependencyMut } from "@rspack/binding"; export class Dependency { - #binding: JsDependency | JsCompiledDependency; + #binding: JsDependencyMut | JsDependency; #dropped = false; - static __from_binding( - binding: JsDependency | JsCompiledDependency - ): Dependency { + static __from_binding(binding: JsDependencyMut | JsDependency): Dependency { return new Dependency(binding); } @@ -22,7 +20,7 @@ export class Dependency { } } - private constructor(binding: JsDependency | JsCompiledDependency) { + private constructor(binding: JsDependencyMut | JsDependency) { this.#binding = binding; } @@ -50,7 +48,7 @@ export class Dependency { this.ensureValidLifecycle(); if ( typeof critital === "boolean" && - this.#binding instanceof JsDependency + this.#binding instanceof JsDependencyMut ) { this.#binding.critical = critital; } From 183afd88e1fe26e99ae1c26f56f0c13bcea313b6 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Wed, 9 Oct 2024 17:44:40 +0800 Subject: [PATCH 7/9] fix: remove lifecycle --- .../rspack_binding_values/src/dependency.rs | 8 +- packages/rspack/src/Compiler.ts | 6 - packages/rspack/src/Module.ts | 281 ++++++++---------- 3 files changed, 133 insertions(+), 162 deletions(-) diff --git a/crates/rspack_binding_values/src/dependency.rs b/crates/rspack_binding_values/src/dependency.rs index 7e177a35b56..1d85b7e7218 100644 --- a/crates/rspack_binding_values/src/dependency.rs +++ b/crates/rspack_binding_values/src/dependency.rs @@ -10,8 +10,8 @@ impl JsDependency { pub(crate) fn new(dependency: &BoxDependency) -> Self { // SAFETY: // The lifetime of the &mut BoxDependency reference is extended to 'static. - // This is safe because the JS side will guarantee that the JsDependency instance's - // lifetime is properly managed and restricted. + // Accessing fields and methods on the Rust object from the JS side after the Rust object's + // lifetime has ended is undefined behavior, which we currently disregard. let dependency = unsafe { std::mem::transmute::<&BoxDependency, &'static BoxDependency>(dependency) }; Self(dependency) @@ -60,8 +60,8 @@ impl JsDependencyMut { pub(crate) fn new(dependency: &mut BoxDependency) -> Self { // SAFETY: // The lifetime of the &mut BoxDependency reference is extended to 'static. - // This is safe because the JS side will guarantee that the JsDependency instance's - // lifetime is properly managed and restricted. + // Accessing fields and methods on the Rust object from the JS side after the Rust object's + // lifetime has ended is undefined behavior, which we currently disregard. let dependency = unsafe { std::mem::transmute::<&mut BoxDependency, &'static mut BoxDependency>(dependency) }; Self(dependency) diff --git a/packages/rspack/src/Compiler.ts b/packages/rspack/src/Compiler.ts index 8e38aefb59d..25eb42ef6e8 100644 --- a/packages/rspack/src/Compiler.ts +++ b/packages/rspack/src/Compiler.ts @@ -1166,9 +1166,6 @@ class Compiler { ) : false; const result = await queried.promise(data); - if (data) { - ContextModuleFactoryBeforeResolveData.__drop(data); - } return result ? ContextModuleFactoryBeforeResolveData.__to_binding(result) : false; @@ -1191,9 +1188,6 @@ class Compiler { ) : false; const result = await queried.promise(data); - if (data) { - ContextModuleFactoryAfterResolveData.__drop(data); - } return result ? ContextModuleFactoryAfterResolveData.__to_binding(result) : false; diff --git a/packages/rspack/src/Module.ts b/packages/rspack/src/Module.ts index 20e782d366d..e978e1565e6 100644 --- a/packages/rspack/src/Module.ts +++ b/packages/rspack/src/Module.ts @@ -39,7 +39,11 @@ export type ResolveData = { export class ContextModuleFactoryBeforeResolveData { #inner: JsContextModuleFactoryBeforeResolveData; - #dropped = false; + + declare context: string; + declare request: string; + declare regExp: RegExp | undefined; + declare recursive: boolean; static __from_binding(binding: JsContextModuleFactoryBeforeResolveData) { return new ContextModuleFactoryBeforeResolveData(binding); @@ -51,70 +55,58 @@ export class ContextModuleFactoryBeforeResolveData { return data.#inner; } - static __drop(data: ContextModuleFactoryBeforeResolveData) { - data.#dropped = true; - } - private constructor(binding: JsContextModuleFactoryBeforeResolveData) { this.#inner = binding; - } - - private ensureValidLifecycle() { - if (this.#dropped) { - throw new Error( - "The ContextModuleFactoryBeforeResolveData has exceeded its lifecycle and has been dropped by Rust." - ); - } - } - - get context(): string { - this.ensureValidLifecycle(); - return this.#inner.context; - } - - set context(val: string) { - this.ensureValidLifecycle(); - this.#inner.context = val; - } - - get request(): string { - this.ensureValidLifecycle(); - return this.#inner.request; - } - - set request(val: string) { - this.#inner.request = val; - } - get regExp(): RegExp | undefined { - this.ensureValidLifecycle(); - if (!this.#inner.regExp) { - return undefined; - } - const { source, flags } = this.#inner.regExp; - return new RegExp(source, flags); - } - - set regExp(val: RegExp | undefined) { - this.ensureValidLifecycle(); - if (!val) { - this.#inner.regExp = undefined; - return; - } - this.#inner.regExp = { - source: val.source, - flags: val.flags - }; - } - - get recursive(): boolean { - this.ensureValidLifecycle(); - return this.#inner.recursive; - } - - set recursive(val: boolean) { - this.ensureValidLifecycle(); - this.#inner.recursive = val; + Object.defineProperties(this, { + context: { + enumerable: true, + get(): string { + return binding.context; + }, + set(val: string) { + binding.context = val; + } + }, + request: { + enumerable: true, + get(): string { + return binding.request; + }, + set(val: string) { + binding.request = val; + } + }, + regExp: { + enumerable: true, + get(): RegExp | undefined { + if (!binding.regExp) { + return undefined; + } + const { source, flags } = binding.regExp; + return new RegExp(source, flags); + }, + set(val: RegExp | undefined) { + if (!val) { + binding.regExp = undefined; + return; + } + binding.regExp = { + source: val.source, + flags: val.flags + }; + } + }, + recursive: { + enumerable: true, + get(this: ContextModuleFactoryAfterResolveData): boolean { + return binding.recursive; + }, + set(val: boolean) { + binding.recursive = val; + } + } + }); } } @@ -124,8 +116,13 @@ export type ContextModuleFactoryBeforeResolveResult = export class ContextModuleFactoryAfterResolveData { #inner: JsContextModuleFactoryAfterResolveData; - #resolvedDependencies?: Dependency[]; - #dropped = false; + + declare resource: number; + declare context: string; + declare request: string; + declare regExp: RegExp | undefined; + declare recursive: boolean; + declare readonly dependencies: Dependency[]; static __from_binding(binding: JsContextModuleFactoryAfterResolveData) { return new ContextModuleFactoryAfterResolveData(binding); @@ -137,95 +134,75 @@ export class ContextModuleFactoryAfterResolveData { return data.#inner; } - static __drop(data: ContextModuleFactoryAfterResolveData) { - data.#dropped = true; - if (data.#resolvedDependencies) { - for (const dependency of data.#resolvedDependencies) { - Dependency.__drop(dependency); - } - } - } - - private ensureValidLifecycle() { - if (this.#dropped) { - throw new Error( - "The ContextModuleFactoryAfterResolveData has exceeded its lifecycle and has been dropped by Rust." - ); - } - } - - private constructor(data: JsContextModuleFactoryAfterResolveData) { - this.#inner = data; - } - - get resource(): string { - this.ensureValidLifecycle(); - return this.#inner.resource; - } - - set resource(val: string) { - this.ensureValidLifecycle(); - this.#inner.resource = val; - } - - get context(): string { - this.ensureValidLifecycle(); - return this.#inner.context; - } - - set context(val: string) { - this.ensureValidLifecycle(); - this.#inner.context = val; - } - - get request(): string { - this.ensureValidLifecycle(); - return this.#inner.request; - } - - set request(val: string) { - this.#inner.request = val; - } - - get regExp(): RegExp | undefined { - this.ensureValidLifecycle(); - if (!this.#inner.regExp) { - return undefined; - } - const { source, flags } = this.#inner.regExp; - return new RegExp(source, flags); - } - - set regExp(val: RegExp | undefined) { - this.ensureValidLifecycle(); - if (!val) { - this.#inner.regExp = undefined; - return; - } - this.#inner.regExp = { - source: val.source, - flags: val.flags - }; - } - - get recursive(): boolean { - this.ensureValidLifecycle(); - return this.#inner.recursive; - } - - set recursive(val: boolean) { - this.ensureValidLifecycle(); - this.#inner.recursive = val; - } + private constructor(binding: JsContextModuleFactoryAfterResolveData) { + this.#inner = binding; - get dependencies(): Dependency[] { - this.ensureValidLifecycle(); - if (!this.#resolvedDependencies) { - this.#resolvedDependencies = this.#inner.dependencies.map(dep => - Dependency.__from_binding(dep) - ); - } - return this.#resolvedDependencies; + Object.defineProperties(this, { + resource: { + enumerable: true, + get(): string { + return binding.resource; + }, + set(val: string) { + binding.resource = val; + } + }, + context: { + enumerable: true, + get(): string { + return binding.context; + }, + set(val: string) { + binding.context = val; + } + }, + request: { + enumerable: true, + get(): string { + return binding.request; + }, + set(val: string) { + binding.request = val; + } + }, + regExp: { + enumerable: true, + get(): RegExp | undefined { + if (!binding.regExp) { + return undefined; + } + const { source, flags } = binding.regExp; + return new RegExp(source, flags); + }, + set(val: RegExp | undefined) { + if (!val) { + binding.regExp = undefined; + return; + } + binding.regExp = { + source: val.source, + flags: val.flags + }; + } + }, + recursive: { + enumerable: true, + get(): boolean { + return binding.recursive; + }, + set(val: boolean) { + binding.recursive = val; + } + }, + dependencies: { + enumerable: true, + get(): Dependency[] { + return binding.dependencies.map(dep => + Dependency.__from_binding(dep) + ); + } + } + }); } } From 483327d892c291d3f79473687a15e95fa7321dce Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Wed, 9 Oct 2024 18:23:48 +0800 Subject: [PATCH 8/9] chore: update api --- packages/rspack/etc/api.md | 123 +++++++++++++++---------------------- 1 file changed, 50 insertions(+), 73 deletions(-) diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index deab01cd8f8..f688133839a 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -698,6 +698,7 @@ const baseRuleSetRule: z.ZodObject<{ sideEffects: z.ZodOptional; enforce: z.ZodOptional, z.ZodLiteral<"post">]>>; }, "strict", z.ZodTypeAny, { + resource?: RuleSetCondition | undefined; options?: string | Record | undefined; type?: string | undefined; exclude?: RuleSetCondition | undefined; @@ -710,7 +711,6 @@ const baseRuleSetRule: z.ZodObject<{ issuer?: RuleSetCondition | undefined; issuerLayer?: RuleSetCondition | undefined; dependency?: RuleSetCondition | undefined; - resource?: RuleSetCondition | undefined; resourceFragment?: RuleSetCondition | undefined; resourceQuery?: RuleSetCondition | undefined; scheme?: RuleSetCondition | undefined; @@ -734,6 +734,7 @@ const baseRuleSetRule: z.ZodObject<{ generator?: Record | undefined; resolve?: ResolveOptions | undefined; }, { + resource?: RuleSetCondition | undefined; options?: string | Record | undefined; type?: string | undefined; exclude?: RuleSetCondition | undefined; @@ -746,7 +747,6 @@ const baseRuleSetRule: z.ZodObject<{ issuer?: RuleSetCondition | undefined; issuerLayer?: RuleSetCondition | undefined; dependency?: RuleSetCondition | undefined; - resource?: RuleSetCondition | undefined; resourceFragment?: RuleSetCondition | undefined; resourceQuery?: RuleSetCondition | undefined; scheme?: RuleSetCondition | undefined; @@ -1579,29 +1579,22 @@ class ContextModuleFactory { // @public (undocumented) class ContextModuleFactoryAfterResolveData { - // (undocumented) - static __drop(data: ContextModuleFactoryAfterResolveData): void; // (undocumented) static __from_binding(binding: JsContextModuleFactoryAfterResolveData): ContextModuleFactoryAfterResolveData; // (undocumented) static __to_binding(data: ContextModuleFactoryAfterResolveData): JsContextModuleFactoryAfterResolveData; // (undocumented) - get context(): string; - set context(val: string); + context: string; // (undocumented) - get dependencies(): Dependency[]; + readonly dependencies: Dependency[]; // (undocumented) - get recursive(): boolean; - set recursive(val: boolean); + recursive: boolean; // (undocumented) - get regExp(): RegExp | undefined; - set regExp(val: RegExp | undefined); + regExp: RegExp | undefined; // (undocumented) - get request(): string; - set request(val: string); + request: string; // (undocumented) - get resource(): string; - set resource(val: string); + resource: number; } // @public (undocumented) @@ -1609,24 +1602,10 @@ type ContextModuleFactoryAfterResolveResult = false | ContextModuleFactoryAfterR // @public (undocumented) class ContextModuleFactoryBeforeResolveData { - // (undocumented) - static __drop(data: ContextModuleFactoryBeforeResolveData): void; // (undocumented) static __from_binding(binding: JsContextModuleFactoryBeforeResolveData): ContextModuleFactoryBeforeResolveData; // (undocumented) static __to_binding(data: ContextModuleFactoryBeforeResolveData): JsContextModuleFactoryBeforeResolveData; - // (undocumented) - get context(): string; - set context(val: string); - // (undocumented) - get recursive(): boolean; - set recursive(val: boolean); - // (undocumented) - get regExp(): RegExp | undefined; - set regExp(val: RegExp | undefined); - // (undocumented) - get request(): string; - set request(val: string); } // @public (undocumented) @@ -1888,8 +1867,6 @@ class DependenciesBlock { // @public (undocumented) class Dependency { - // (undocumented) - static __drop(dependency: Dependency): void; // (undocumented) static __from_binding(binding: JsDependencyMut | JsDependency): Dependency; // (undocumented) @@ -3731,15 +3708,15 @@ const externalItem: z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, }>>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -3756,15 +3733,15 @@ const externalItem: z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, }>>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -3787,15 +3764,15 @@ const externalItemFunctionData: z.ZodObject<{ }>>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -3830,15 +3807,15 @@ const externals: z.ZodUnion<[z.ZodArray>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -3855,15 +3832,15 @@ const externals: z.ZodUnion<[z.ZodArray>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -3880,15 +3857,15 @@ const externals: z.ZodUnion<[z.ZodArray>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -3905,15 +3882,15 @@ const externals: z.ZodUnion<[z.ZodArray>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -3923,29 +3900,29 @@ const externals: z.ZodUnion<[z.ZodArray> | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "commonjs2" | "var" | "assign" | "this" | "window" | "self" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, ...args_1: unknown[]) => Promise>) | (string | RegExp | Record> | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "commonjs2" | "var" | "assign" | "this" | "window" | "self" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -3953,29 +3930,29 @@ export const ExternalsPlugin: { name: BuiltinPluginName; _args: [type: string, externals: string | RegExp | Record> | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "commonjs2" | "var" | "assign" | "this" | "window" | "self" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, ...args_1: unknown[]) => Promise>) | (string | RegExp | Record> | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "commonjs2" | "var" | "assign" | "this" | "window" | "self" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -11424,15 +11401,15 @@ export const rspackOptions: z.ZodObject<{ }>>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -11449,15 +11426,15 @@ export const rspackOptions: z.ZodObject<{ }>>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -11474,15 +11451,15 @@ export const rspackOptions: z.ZodObject<{ }>>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -11499,15 +11476,15 @@ export const rspackOptions: z.ZodObject<{ }>>; }, "strict", z.ZodTypeAny, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -13078,6 +13055,8 @@ export const rspackOptions: z.ZodObject<{ maxEntrypointSize?: number | undefined; }>, z.ZodLiteral]>>; }, "strict", z.ZodTypeAny, { + context?: string | undefined; + dependencies?: string[] | undefined; module?: { parser?: { javascript?: { @@ -13207,9 +13186,7 @@ export const rspackOptions: z.ZodObject<{ defaultRules?: (false | "" | 0 | "..." | RuleSetRule | null | undefined)[] | undefined; noParse?: string | RegExp | ((args_0: string, ...args_1: unknown[]) => boolean) | (string | RegExp | ((args_0: string, ...args_1: unknown[]) => boolean))[] | undefined; } | undefined; - dependencies?: string[] | undefined; name?: string | undefined; - context?: string | undefined; performance?: false | { assetFilter?: ((args_0: string, ...args_1: unknown[]) => boolean) | undefined; hints?: false | "error" | "warning" | undefined; @@ -13445,29 +13422,29 @@ export const rspackOptions: z.ZodObject<{ } | undefined; externals?: string | RegExp | Record> | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "commonjs2" | "var" | "assign" | "this" | "window" | "self" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, ...args_1: unknown[]) => Promise>) | (string | RegExp | Record> | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "commonjs2" | "var" | "assign" | "this" | "window" | "self" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; @@ -13652,6 +13629,8 @@ export const rspackOptions: z.ZodObject<{ devServer?: DevServer | undefined; bail?: boolean | undefined; }, { + context?: string | undefined; + dependencies?: string[] | undefined; module?: { parser?: { javascript?: { @@ -13781,9 +13760,7 @@ export const rspackOptions: z.ZodObject<{ defaultRules?: (false | "" | 0 | "..." | RuleSetRule | null | undefined)[] | undefined; noParse?: string | RegExp | ((args_0: string, ...args_1: unknown[]) => boolean) | (string | RegExp | ((args_0: string, ...args_1: unknown[]) => boolean))[] | undefined; } | undefined; - dependencies?: string[] | undefined; name?: string | undefined; - context?: string | undefined; performance?: false | { assetFilter?: ((args_0: string, ...args_1: unknown[]) => boolean) | undefined; hints?: false | "error" | "warning" | undefined; @@ -14019,29 +13996,29 @@ export const rspackOptions: z.ZodObject<{ } | undefined; externals?: string | RegExp | Record> | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "commonjs2" | "var" | "assign" | "this" | "window" | "self" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, ...args_1: unknown[]) => Promise>) | (string | RegExp | Record> | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "commonjs2" | "var" | "assign" | "this" | "window" | "self" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { context?: string | undefined; - dependencyType?: string | undefined; request?: string | undefined; + dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; From 15406711f45f20f8e966bdb2dc9ba978d7e0ac81 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Thu, 10 Oct 2024 13:21:17 +0800 Subject: [PATCH 9/9] fix: Dependency --- packages/rspack/etc/api.md | 87 +++++++++++++++---------------- packages/rspack/src/Dependency.ts | 80 +++++++++++++--------------- 2 files changed, 77 insertions(+), 90 deletions(-) diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index 1da04167482..c933341dde7 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -520,10 +520,10 @@ const baseRuleSetRule: z.ZodObject<{ sideEffects: z.ZodOptional; enforce: z.ZodOptional, z.ZodLiteral<"post">]>>; }, "strict", z.ZodTypeAny, { + type?: string | undefined; resource?: RuleSetCondition | undefined; layer?: string | undefined; options?: string | Record | undefined; - type?: string | undefined; test?: RuleSetCondition | undefined; enforce?: "pre" | "post" | undefined; sideEffects?: boolean | undefined; @@ -556,10 +556,10 @@ const baseRuleSetRule: z.ZodObject<{ generator?: Record | undefined; resolve?: t.ResolveOptions | undefined; }, { + type?: string | undefined; resource?: RuleSetCondition | undefined; layer?: string | undefined; options?: string | Record | undefined; - type?: string | undefined; test?: RuleSetCondition | undefined; enforce?: "pre" | "post" | undefined; sideEffects?: boolean | undefined; @@ -1667,19 +1667,16 @@ class DependenciesBlock { // @public (undocumented) class Dependency { - // (undocumented) - static __drop(dependency: Dependency): void; // (undocumented) static __from_binding(binding: JsDependencyMut | JsDependency): Dependency; // (undocumented) - get category(): string; + readonly category: string; // (undocumented) - get critital(): boolean; - set critital(critital: boolean); + critical: boolean; // (undocumented) - get request(): string | undefined; + readonly request: string | undefined; // (undocumented) - get type(): string; + readonly type: string; } // @public (undocumented) @@ -5839,10 +5836,10 @@ const optimization: z.ZodObject<{ type: z.ZodOptional]>>; idHint: z.ZodOptional; }, "strict", z.ZodTypeAny, { + type?: string | RegExp | undefined; filename?: string | undefined; name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -5859,10 +5856,10 @@ const optimization: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; }, { + type?: string | RegExp | undefined; filename?: string | undefined; name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -5916,10 +5913,10 @@ const optimization: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -5959,10 +5956,10 @@ const optimization: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -6038,10 +6035,10 @@ const optimization: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -6104,10 +6101,10 @@ const optimization: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -6205,10 +6202,10 @@ const optimizationSplitChunksCacheGroup: z.ZodObject<{ type: z.ZodOptional]>>; idHint: z.ZodOptional; }, "strict", z.ZodTypeAny, { + type?: string | RegExp | undefined; filename?: string | undefined; name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -6225,10 +6222,10 @@ const optimizationSplitChunksCacheGroup: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; }, { + type?: string | RegExp | undefined; filename?: string | undefined; name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -6290,10 +6287,10 @@ const optimizationSplitChunksOptions: z.ZodObject<{ type: z.ZodOptional]>>; idHint: z.ZodOptional; }, "strict", z.ZodTypeAny, { + type?: string | RegExp | undefined; filename?: string | undefined; name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -6310,10 +6307,10 @@ const optimizationSplitChunksOptions: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; }, { + type?: string | RegExp | undefined; filename?: string | undefined; name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -6367,10 +6364,10 @@ const optimizationSplitChunksOptions: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -6410,10 +6407,10 @@ const optimizationSplitChunksOptions: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -9692,15 +9689,15 @@ export const rspackOptions: z.ZodObject<{ issuer: string; }>>; }, "strict", z.ZodTypeAny, { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; @@ -9717,15 +9714,15 @@ export const rspackOptions: z.ZodObject<{ issuer: string; }>>; }, "strict", z.ZodTypeAny, { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; @@ -9742,15 +9739,15 @@ export const rspackOptions: z.ZodObject<{ issuer: string; }>>; }, "strict", z.ZodTypeAny, { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; @@ -9767,15 +9764,15 @@ export const rspackOptions: z.ZodObject<{ issuer: string; }>>; }, "strict", z.ZodTypeAny, { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; @@ -10143,10 +10140,10 @@ export const rspackOptions: z.ZodObject<{ type: z.ZodOptional]>>; idHint: z.ZodOptional; }, "strict", z.ZodTypeAny, { + type?: string | RegExp | undefined; filename?: string | undefined; name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -10163,10 +10160,10 @@ export const rspackOptions: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; }, { + type?: string | RegExp | undefined; filename?: string | undefined; name?: string | false | ((args_0: Module | undefined, ...args_1: unknown[]) => unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -10220,10 +10217,10 @@ export const rspackOptions: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -10263,10 +10260,10 @@ export const rspackOptions: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -10342,10 +10339,10 @@ export const rspackOptions: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -10408,10 +10405,10 @@ export const rspackOptions: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -11721,29 +11718,29 @@ export const rspackOptions: z.ZodObject<{ } | undefined; } | undefined; externals?: string | RegExp | Record> | ((args_0: { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "var" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, ...args_1: unknown[]) => Promise>) | (string | RegExp | Record> | ((args_0: { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "var" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; @@ -11878,10 +11875,10 @@ export const rspackOptions: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; @@ -12303,29 +12300,29 @@ export const rspackOptions: z.ZodObject<{ } | undefined; } | undefined; externals?: string | RegExp | Record> | ((args_0: { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "var" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, ...args_1: unknown[]) => Promise>) | (string | RegExp | Record> | ((args_0: { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; } | undefined; }, args_1: (args_0: Error | undefined, args_1: string | boolean | string[] | Record | undefined, args_2: "module" | "global" | "system" | "promise" | "commonjs" | "umd" | "amd" | "jsonp" | "import" | "var" | "assign" | "this" | "window" | "self" | "commonjs2" | "commonjs-module" | "commonjs-static" | "amd-require" | "umd2" | "module-import" | "script" | "node-commonjs" | undefined, ...args_3: unknown[]) => void, ...args_2: unknown[]) => unknown) | ((args_0: { - context?: string | undefined; request?: string | undefined; + context?: string | undefined; dependencyType?: string | undefined; contextInfo?: { issuer: string; @@ -12460,10 +12457,10 @@ export const rspackOptions: z.ZodObject<{ maxInitialRequests?: number | undefined; automaticNameDelimiter?: string | undefined; cacheGroups?: Record unknown) | undefined; priority?: number | undefined; - type?: string | RegExp | undefined; test?: string | RegExp | ((args_0: Module, ...args_1: unknown[]) => unknown) | undefined; enforce?: boolean | undefined; reuseExistingChunk?: boolean | undefined; diff --git a/packages/rspack/src/Dependency.ts b/packages/rspack/src/Dependency.ts index 8379aa30576..49e3eb5b3ef 100644 --- a/packages/rspack/src/Dependency.ts +++ b/packages/rspack/src/Dependency.ts @@ -1,56 +1,46 @@ import { type JsDependency, JsDependencyMut } from "@rspack/binding"; export class Dependency { - #binding: JsDependencyMut | JsDependency; - #dropped = false; + declare readonly type: string; + declare readonly category: string; + declare readonly request: string | undefined; + declare critical: boolean; static __from_binding(binding: JsDependencyMut | JsDependency): Dependency { return new Dependency(binding); } - static __drop(dependency: Dependency) { - dependency.#dropped = true; - } - - private ensureValidLifecycle() { - if (this.#dropped) { - throw new Error( - "The Dependency has exceeded its lifecycle and has been dropped by Rust." - ); - } - } - private constructor(binding: JsDependencyMut | JsDependency) { - this.#binding = binding; - } - - get type(): string { - this.ensureValidLifecycle(); - return this.#binding.type; - } - - get category(): string { - this.ensureValidLifecycle(); - return this.#binding.category; - } - - get request(): string | undefined { - this.ensureValidLifecycle(); - return this.#binding.request; - } - - get critital(): boolean { - this.ensureValidLifecycle(); - return this.#binding.critical; - } - - set critital(critital: boolean) { - this.ensureValidLifecycle(); - if ( - typeof critital === "boolean" && - this.#binding instanceof JsDependencyMut - ) { - this.#binding.critical = critital; - } + Object.defineProperties(this, { + type: { + enumerable: true, + get(): string { + return binding.type; + } + }, + category: { + enumerable: true, + get(): string { + return binding.category; + } + }, + request: { + enumerable: true, + get(): string | undefined { + return binding.request; + } + }, + critical: { + enumerable: true, + get(): boolean { + return binding.critical; + }, + set(val: boolean) { + if (binding instanceof JsDependencyMut) { + binding.critical = val; + } + } + } + }); } }