From ab3cdfb6b949058f9f43b602e7a211572e996668 Mon Sep 17 00:00:00 2001 From: Cong-Cong Pan Date: Wed, 6 Nov 2024 11:42:15 +0800 Subject: [PATCH] refactor: rm UNASSOCIATED_MODULE_INSTANCE_REFS (#8327) --- .../node_binding/src/plugins/interceptor.rs | 26 +++- .../raw_builtins/raw_lazy_compilation.rs | 6 +- .../raw_split_chunk_cache_group_test.rs | 6 +- .../raw_split_chunks/raw_split_chunk_name.rs | 6 +- .../src/plugins/js_loader/context.rs | 2 +- .../rspack_binding_values/src/chunk_graph.rs | 6 +- .../rspack_binding_values/src/chunk_group.rs | 2 +- .../src/compilation/mod.rs | 16 ++- crates/rspack_binding_values/src/module.rs | 124 +++++++----------- .../rspack_core/src/compiler/compilation.rs | 6 +- .../src/compiler/make/repair/add.rs | 2 + .../src/compiler/make/repair/build.rs | 9 +- .../src/compiler/make/repair/factorize.rs | 10 +- .../src/compiler/make/repair/mod.rs | 8 +- .../make/repair/process_dependencies.rs | 1 + .../src/compiler/module_executor/entry.rs | 1 + .../rspack_core/src/loader/loader_runner.rs | 3 +- crates/rspack_core/src/module.rs | 9 +- crates/rspack_core/src/module_factory.rs | 4 +- crates/rspack_core/src/normal_module.rs | 1 + .../src/js_values/one_shot_value_ref.rs | 32 ++++- ...urce_map_dev_tool_module_options_plugin.rs | 4 +- .../src/plugin/module_concatenation_plugin.rs | 1 + .../src/factory.rs | 1 + .../src/plugin.rs | 16 +-- crates/rspack_plugin_progress/src/lib.rs | 10 +- packages/rspack/src/Module.ts | 7 +- 27 files changed, 179 insertions(+), 140 deletions(-) diff --git a/crates/node_binding/src/plugins/interceptor.rs b/crates/node_binding/src/plugins/interceptor.rs index 433b9b197ca..655f5ca8aef 100644 --- a/crates/node_binding/src/plugins/interceptor.rs +++ b/crates/node_binding/src/plugins/interceptor.rs @@ -33,7 +33,7 @@ use rspack_core::{ CompilationBuildModule, CompilationBuildModuleHook, CompilationChunkAsset, CompilationChunkAssetHook, CompilationChunkHash, CompilationChunkHashHook, CompilationExecuteModule, CompilationExecuteModuleHook, CompilationFinishModules, - CompilationFinishModulesHook, CompilationOptimizeChunkModules, + CompilationFinishModulesHook, CompilationId, CompilationOptimizeChunkModules, CompilationOptimizeChunkModulesHook, CompilationOptimizeModules, CompilationOptimizeModulesHook, CompilationOptimizeTree, CompilationOptimizeTreeHook, CompilationParams, CompilationProcessAssets, CompilationProcessAssetsHook, CompilationRuntimeModule, @@ -1011,10 +1011,14 @@ impl CompilerAssetEmitted for CompilerAssetEmittedTap { #[async_trait] impl CompilationBuildModule for CompilationBuildModuleTap { - async fn run(&self, module: &mut BoxModule) -> rspack_error::Result<()> { + async fn run( + &self, + compilation_id: CompilationId, + module: &mut BoxModule, + ) -> rspack_error::Result<()> { self .function - .call_with_sync(JsModuleWrapper::new(module.as_ref(), None)) + .call_with_sync(JsModuleWrapper::new(module.as_ref(), compilation_id, None)) .await } @@ -1025,10 +1029,14 @@ impl CompilationBuildModule for CompilationBuildModuleTap { #[async_trait] impl CompilationStillValidModule for CompilationStillValidModuleTap { - async fn run(&self, module: &mut BoxModule) -> rspack_error::Result<()> { + async fn run( + &self, + compilation_id: CompilationId, + module: &mut BoxModule, + ) -> rspack_error::Result<()> { self .function - .call_with_sync(JsModuleWrapper::new(module.as_ref(), None)) + .call_with_sync(JsModuleWrapper::new(module.as_ref(), compilation_id, None)) .await } @@ -1039,10 +1047,14 @@ impl CompilationStillValidModule for CompilationStillValidModuleTap { #[async_trait] impl CompilationSucceedModule for CompilationSucceedModuleTap { - async fn run(&self, module: &mut BoxModule) -> rspack_error::Result<()> { + async fn run( + &self, + compilation_id: CompilationId, + module: &mut BoxModule, + ) -> rspack_error::Result<()> { self .function - .call_with_sync(JsModuleWrapper::new(module.as_ref(), None)) + .call_with_sync(JsModuleWrapper::new(module.as_ref(), compilation_id, None)) .await } diff --git a/crates/rspack_binding_options/src/options/raw_builtins/raw_lazy_compilation.rs b/crates/rspack_binding_options/src/options/raw_builtins/raw_lazy_compilation.rs index 67d01e17cb2..7b7dcf0d05a 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/raw_lazy_compilation.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/raw_lazy_compilation.rs @@ -4,7 +4,7 @@ use napi::{ }; use napi_derive::napi; use rspack_binding_values::JsModuleWrapper; -use rspack_core::ModuleIdentifier; +use rspack_core::{CompilationId, ModuleIdentifier}; use rspack_napi::threadsafe_function::ThreadsafeFunction; use rspack_plugin_lazy_compilation::{ backend::{Backend, ModuleInfo}, @@ -41,10 +41,10 @@ pub struct LazyCompilationTestFn { } impl LazyCompilationTestCheck for LazyCompilationTestFn { - fn test(&self, m: &dyn rspack_core::Module) -> bool { + fn test(&self, compilation_id: CompilationId, m: &dyn rspack_core::Module) -> bool { let res = self .tsfn - .blocking_call_with_sync(JsModuleWrapper::new(m, None)) + .blocking_call_with_sync(JsModuleWrapper::new(m, compilation_id, None)) .expect("failed to invoke lazyCompilation.test"); res.unwrap_or(false) diff --git a/crates/rspack_binding_options/src/options/raw_split_chunks/raw_split_chunk_cache_group_test.rs b/crates/rspack_binding_options/src/options/raw_split_chunks/raw_split_chunk_cache_group_test.rs index 36586ee3c94..c6a77e37bf8 100644 --- a/crates/rspack_binding_options/src/options/raw_split_chunks/raw_split_chunk_cache_group_test.rs +++ b/crates/rspack_binding_options/src/options/raw_split_chunks/raw_split_chunk_cache_group_test.rs @@ -28,7 +28,11 @@ impl FromNapiValue for JsCacheGroupTestCtx { impl<'a> From> for JsCacheGroupTestCtx { fn from(value: CacheGroupTestFnCtx<'a>) -> Self { JsCacheGroupTestCtx { - module: JsModuleWrapper::new(value.module, Some(value.compilation)), + module: JsModuleWrapper::new( + value.module, + value.compilation.id(), + Some(value.compilation), + ), } } } diff --git a/crates/rspack_binding_options/src/options/raw_split_chunks/raw_split_chunk_name.rs b/crates/rspack_binding_options/src/options/raw_split_chunks/raw_split_chunk_name.rs index 76ed8b101f8..5545489dbf9 100644 --- a/crates/rspack_binding_options/src/options/raw_split_chunks/raw_split_chunk_name.rs +++ b/crates/rspack_binding_options/src/options/raw_split_chunks/raw_split_chunk_name.rs @@ -35,7 +35,11 @@ impl FromNapiValue for RawChunkOptionNameCtx { impl<'a> From> for RawChunkOptionNameCtx { fn from(value: ChunkNameGetterFnCtx<'a>) -> Self { RawChunkOptionNameCtx { - module: JsModuleWrapper::new(value.module, Some(value.compilation)), + module: JsModuleWrapper::new( + value.module, + value.compilation.id(), + Some(value.compilation), + ), chunks: value .chunks .iter() diff --git a/crates/rspack_binding_options/src/plugins/js_loader/context.rs b/crates/rspack_binding_options/src/plugins/js_loader/context.rs index d5152432454..83da01720f6 100644 --- a/crates/rspack_binding_options/src/plugins/js_loader/context.rs +++ b/crates/rspack_binding_options/src/plugins/js_loader/context.rs @@ -96,7 +96,7 @@ impl TryFrom<&mut LoaderContext> for JsLoaderContext { Ok(JsLoaderContext { resource_data: cx.resource_data.as_ref().into(), module_identifier: module.identifier().to_string(), - module: JsModuleWrapper::new(module, None), + module: JsModuleWrapper::new(module, cx.context.compilation_id, None), hot: cx.hot, content: match cx.content() { Some(c) => Either::B(c.to_owned().into_bytes().into()), diff --git a/crates/rspack_binding_values/src/chunk_graph.rs b/crates/rspack_binding_values/src/chunk_graph.rs index 18833b19c64..83a0866cdb7 100644 --- a/crates/rspack_binding_values/src/chunk_graph.rs +++ b/crates/rspack_binding_values/src/chunk_graph.rs @@ -21,7 +21,7 @@ pub fn get_chunk_modules( return modules .iter() - .map(|module| JsModuleWrapper::new(module.as_ref(), Some(compilation))) + .map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))) .collect::>(); } @@ -42,7 +42,7 @@ pub fn get_chunk_entry_modules( return modules .iter() .filter_map(|module| module_graph.module_by_identifier(module)) - .map(|module| JsModuleWrapper::new(module.as_ref(), Some(compilation))) + .map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))) .collect::>(); } @@ -86,7 +86,7 @@ pub fn get_chunk_modules_iterable_by_source_type( SourceType::from(source_type.as_str()), &compilation.get_module_graph(), ) - .map(|module| JsModuleWrapper::new(module, Some(compilation))) + .map(|module| JsModuleWrapper::new(module, compilation.id(), Some(compilation))) .collect(), ) } diff --git a/crates/rspack_binding_values/src/chunk_group.rs b/crates/rspack_binding_values/src/chunk_group.rs index d4937a03b9e..d850405ba20 100644 --- a/crates/rspack_binding_values/src/chunk_group.rs +++ b/crates/rspack_binding_values/src/chunk_group.rs @@ -57,7 +57,7 @@ impl JsChunkGroup { let module = compilation .module_by_identifier(&module_id) .unwrap_or_else(|| panic!("failed to retrieve module by id: {}", module_id)); - JsModuleWrapper::new(module.as_ref(), Some(compilation)) + JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)) }), request: origin.request.clone(), }) diff --git a/crates/rspack_binding_values/src/compilation/mod.rs b/crates/rspack_binding_values/src/compilation/mod.rs index fffdce1312b..e5a45c53b43 100644 --- a/crates/rspack_binding_values/src/compilation/mod.rs +++ b/crates/rspack_binding_values/src/compilation/mod.rs @@ -168,9 +168,9 @@ impl JsCompilation { .modules() .keys() .filter_map(|module_id| { - compilation - .module_by_identifier(module_id) - .map(|module| JsModuleWrapper::new(module.as_ref(), Some(compilation))) + compilation.module_by_identifier(module_id).map(|module| { + JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)) + }) }) .collect::>(), ) @@ -185,9 +185,9 @@ impl JsCompilation { .built_modules .iter() .filter_map(|module_id| { - compilation - .module_by_identifier(module_id) - .map(|module| JsModuleWrapper::new(module.as_ref(), Some(compilation))) + compilation.module_by_identifier(module_id).map(|module| { + JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)) + }) }) .collect::>(), ) @@ -592,13 +592,15 @@ impl JsCompilation { let compilation = self.as_mut()?; callbackify(env, f, async { + let compilation_id = compilation.id(); + let mut modules = compilation .rebuild_module( IdentifierSet::from_iter(module_identifiers.into_iter().map(ModuleIdentifier::from)), |modules| { modules .into_iter() - .map(|module| JsModuleWrapper::new(module.as_ref(), None)) + .map(|module| JsModuleWrapper::new(module.as_ref(), compilation_id, None)) .collect::>() }, ) diff --git a/crates/rspack_binding_values/src/module.rs b/crates/rspack_binding_values/src/module.rs index ff027732f9f..6f5c090ded9 100644 --- a/crates/rspack_binding_values/src/module.rs +++ b/crates/rspack_binding_values/src/module.rs @@ -83,16 +83,14 @@ impl JsDependenciesBlock { pub struct JsModule { identifier: ModuleIdentifier, module: NonNull, + compilation_id: CompilationId, compilation: Option>, } impl JsModule { - fn attach(&mut self, compilation: *const Compilation) { + fn attach(&mut self, compilation: NonNull) { if self.compilation.is_none() { - self.compilation = Some( - #[allow(clippy::unwrap_used)] - NonNull::new(compilation as *mut Compilation).unwrap(), - ); + self.compilation = Some(compilation); } } @@ -323,7 +321,9 @@ impl JsModule { .filter_map(|inner_module_info| { compilation .module_by_identifier(&inner_module_info.id) - .map(|module| JsModuleWrapper::new(module.as_ref(), Some(compilation))) + .map(|module| { + JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)) + }) }) .collect::>(); Either::A(inner_modules) @@ -341,14 +341,12 @@ impl JsModule { } } -type ModuleInstanceRefs = IdentifierMap>>; +type ModuleInstanceRefs = IdentifierMap>; type ModuleInstanceRefsByCompilationId = RefCell>; thread_local! { static MODULE_INSTANCE_REFS: ModuleInstanceRefsByCompilationId = Default::default(); - - static UNASSOCIATED_MODULE_INSTANCE_REFS: RefCell = Default::default(); } // The difference between JsModuleWrapper and JsModule is: @@ -358,13 +356,18 @@ thread_local! { pub struct JsModuleWrapper { identifier: ModuleIdentifier, module: NonNull, + compilation_id: CompilationId, compilation: Option>, } unsafe impl Send for JsModuleWrapper {} impl JsModuleWrapper { - pub fn new(module: &dyn Module, compilation: Option<&Compilation>) -> Self { + pub fn new( + module: &dyn Module, + compilation_id: CompilationId, + compilation: Option<&Compilation>, + ) -> Self { #[allow(clippy::not_unsafe_ptr_arg_deref)] let identifier = module.identifier(); @@ -372,6 +375,7 @@ impl JsModuleWrapper { Self { identifier, module: NonNull::new(module as *const dyn Module as *mut dyn Module).unwrap(), + compilation_id, compilation: compilation .map(|c| NonNull::new(c as *const Compilation as *mut Compilation).unwrap()), } @@ -398,86 +402,52 @@ impl ToNapiValue for JsModuleWrapper { unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result { let module = unsafe { val.module.as_ref() }; - match val.compilation { - Some(compilation_ptr) => MODULE_INSTANCE_REFS.with(|refs| { - let compilation = unsafe { compilation_ptr.as_ref() }; - - let mut refs_by_compilation_id = refs.borrow_mut(); - let entry = refs_by_compilation_id.entry(compilation.id()); - let refs = match entry { - std::collections::hash_map::Entry::Occupied(entry) => entry.into_mut(), - std::collections::hash_map::Entry::Vacant(entry) => { - let refs = IdentifierMap::default(); - entry.insert(refs) - } - }; - - UNASSOCIATED_MODULE_INSTANCE_REFS.with(|ref_cell| { - let mut unassociated_refs = ref_cell.borrow_mut(); - if let Some(unassociated_ref) = unassociated_refs.remove(&module.identifier()) { - let mut instance = unassociated_ref.from_napi_value()?; - instance.as_mut().attach(compilation_ptr.as_ptr()); - - let napi_value = ToNapiValue::to_napi_value(env, &unassociated_ref); - refs.insert(module.identifier(), unassociated_ref); - napi_value + MODULE_INSTANCE_REFS.with(|refs| { + let mut refs_by_compilation_id = refs.borrow_mut(); + let entry = refs_by_compilation_id.entry(val.compilation_id); + let refs = match entry { + std::collections::hash_map::Entry::Occupied(entry) => entry.into_mut(), + std::collections::hash_map::Entry::Vacant(entry) => { + let refs = IdentifierMap::default(); + entry.insert(refs) + } + }; + + match refs.entry(module.identifier()) { + std::collections::hash_map::Entry::Occupied(entry) => { + let r = entry.get(); + let instance = r.from_napi_mut_ref()?; + if let Some(compilation) = val.compilation { + instance.attach(compilation); } else { - match refs.entry(module.identifier()) { - std::collections::hash_map::Entry::Occupied(entry) => { - let r = entry.get(); - ToNapiValue::to_napi_value(env, r) - } - std::collections::hash_map::Entry::Vacant(entry) => { - let instance: ClassInstance = JsModule { - identifier: val.identifier, - module: val.module, - compilation: Some(compilation_ptr), - } - .into_instance(Env::from_raw(env))?; - let r = entry.insert(OneShotRef::new(env, instance)?); - ToNapiValue::to_napi_value(env, r) - } - } - } - }) - }), - None => UNASSOCIATED_MODULE_INSTANCE_REFS.with(|ref_cell| { - let mut refs = ref_cell.borrow_mut(); - match refs.entry(module.identifier()) { - std::collections::hash_map::Entry::Occupied(entry) => { - let r = entry.get(); - - let mut instance: ClassInstance = r.from_napi_value()?; - if !std::ptr::addr_eq(instance.module.as_ptr(), val.module.as_ptr()) { - instance.module = val.module; - } - ToNapiValue::to_napi_value(env, r) - } - std::collections::hash_map::Entry::Vacant(entry) => { - let instance = JsModule { - identifier: val.identifier, - module: val.module, - compilation: None, - } - .into_instance(Env::from_raw(env))?; - let r = entry.insert(OneShotRef::new(env, instance)?); - ToNapiValue::to_napi_value(env, r) + instance.module = val.module; } + ToNapiValue::to_napi_value(env, r) } - }), - } + std::collections::hash_map::Entry::Vacant(entry) => { + let js_module = JsModule { + identifier: val.identifier, + module: val.module, + compilation_id: val.compilation_id, + compilation: val.compilation, + }; + let r = entry.insert(OneShotRef::new(env, js_module)?); + ToNapiValue::to_napi_value(env, r) + } + } + }) } } impl FromNapiValue for JsModuleWrapper { unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result { let instance: ClassInstance = FromNapiValue::from_napi_value(env, napi_val)?; - let module = instance.module; Ok(JsModuleWrapper { identifier: instance.identifier, #[allow(clippy::unwrap_used)] - module: NonNull::new(module.as_ptr()).unwrap(), + module: instance.module, + compilation_id: instance.compilation_id, compilation: instance.compilation, }) } diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 5bb6eec8083..99972dd0780 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -50,9 +50,9 @@ pub type BuildDependency = ( ); define_hook!(CompilationAddEntry: AsyncSeries(compilation: &mut Compilation, entry_name: Option<&str>)); -define_hook!(CompilationBuildModule: AsyncSeries(module: &mut BoxModule)); -define_hook!(CompilationStillValidModule: AsyncSeries(module: &mut BoxModule)); -define_hook!(CompilationSucceedModule: AsyncSeries(module: &mut BoxModule)); +define_hook!(CompilationBuildModule: AsyncSeries(compilation_id: CompilationId, module: &mut BoxModule)); +define_hook!(CompilationStillValidModule: AsyncSeries(compilation_id: CompilationId, module: &mut BoxModule)); +define_hook!(CompilationSucceedModule: AsyncSeries(compilation_id: CompilationId, module: &mut BoxModule)); define_hook!(CompilationExecuteModule: SyncSeries(module: &ModuleIdentifier, runtime_modules: &IdentifierSet, codegen_results: &CodeGenerationResults, execute_module_id: &ExecuteModuleId)); define_hook!(CompilationFinishModules: AsyncSeries(compilation: &mut Compilation)); diff --git a/crates/rspack_core/src/compiler/make/repair/add.rs b/crates/rspack_core/src/compiler/make/repair/add.rs index 1c3df6051bd..9294e31e090 100644 --- a/crates/rspack_core/src/compiler/make/repair/add.rs +++ b/crates/rspack_core/src/compiler/make/repair/add.rs @@ -15,6 +15,7 @@ pub struct AddTask { pub dependencies: Vec, pub current_profile: Option>, } + #[async_trait::async_trait] impl Task for AddTask { fn get_task_type(&self) -> TaskType { @@ -72,6 +73,7 @@ impl Task for AddTask { artifact.built_modules.insert(module_identifier); Ok(vec![Box::new(BuildTask { + compilation_id: context.compilation_id, module: self.module, current_profile: self.current_profile, resolver_factory: context.resolver_factory.clone(), diff --git a/crates/rspack_core/src/compiler/make/repair/build.rs b/crates/rspack_core/src/compiler/make/repair/build.rs index be4bfe52ee8..896a69b88b2 100644 --- a/crates/rspack_core/src/compiler/make/repair/build.rs +++ b/crates/rspack_core/src/compiler/make/repair/build.rs @@ -6,12 +6,13 @@ use rspack_fs::ReadableFileSystem; use super::{process_dependencies::ProcessDependenciesTask, MakeTaskContext}; use crate::{ utils::task_loop::{Task, TaskResult, TaskType}, - AsyncDependenciesBlock, BoxDependency, BuildContext, BuildResult, CompilerOptions, + AsyncDependenciesBlock, BoxDependency, BuildContext, BuildResult, CompilationId, CompilerOptions, DependencyParents, Module, ModuleProfile, ResolverFactory, SharedPluginDriver, }; #[derive(Debug)] pub struct BuildTask { + pub compilation_id: CompilationId, pub module: Box, pub current_profile: Option>, pub resolver_factory: Arc, @@ -27,6 +28,7 @@ impl Task for BuildTask { } async fn async_run(self: Box) -> TaskResult { let Self { + compilation_id, compiler_options, resolver_factory, plugin_driver, @@ -41,12 +43,13 @@ impl Task for BuildTask { plugin_driver .compilation_hooks .build_module - .call(&mut module) + .call(compilation_id, &mut module) .await?; let result = module .build( BuildContext { + compilation_id, compiler_options: compiler_options.clone(), resolver_factory: resolver_factory.clone(), plugin_driver: plugin_driver.clone(), @@ -59,7 +62,7 @@ impl Task for BuildTask { plugin_driver .compilation_hooks .succeed_module - .call(&mut module) + .call(compilation_id, &mut module) .await?; let build_result = result.map(|t| { diff --git a/crates/rspack_core/src/compiler/make/repair/factorize.rs b/crates/rspack_core/src/compiler/make/repair/factorize.rs index 47c3f27e1bd..efb215e2958 100644 --- a/crates/rspack_core/src/compiler/make/repair/factorize.rs +++ b/crates/rspack_core/src/compiler/make/repair/factorize.rs @@ -8,13 +8,14 @@ use super::{add::AddTask, MakeTaskContext}; use crate::{ module_graph::ModuleGraphModule, utils::task_loop::{Task, TaskResult, TaskType}, - BoxDependency, CompilerOptions, Context, ExportInfoData, ExportsInfoData, ModuleFactory, - ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer, ModuleProfile, - Resolve, + BoxDependency, CompilationId, CompilerOptions, Context, ExportInfoData, ExportsInfoData, + ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer, + ModuleProfile, Resolve, }; #[derive(Debug)] pub struct FactorizeTask { + pub compilation_id: CompilationId, pub module_factory: Arc, pub original_module_identifier: Option, pub original_module_source: Option, @@ -60,7 +61,7 @@ impl Task for FactorizeTask { let side_effects_only_info = ExportInfoData::new(Some("*side effects only*".into()), None); let exports_info = ExportsInfoData::new(other_exports_info.id(), side_effects_only_info.id()); let factorize_result_task = FactorizeResultTask { - // dependency: dep_id, + // dependency: dep_id, original_module_identifier: self.original_module_identifier, factory_result: None, dependencies: vec![], @@ -79,6 +80,7 @@ impl Task for FactorizeTask { // Error and result are not mutually exclusive in webpack module factorization. // Rspack puts results that need to be shared in both error and ok in [ModuleFactoryCreateData]. let mut create_data = ModuleFactoryCreateData { + compilation_id: self.compilation_id, resolve_options: self.resolve_options, options: self.options.clone(), context, diff --git a/crates/rspack_core/src/compiler/make/repair/mod.rs b/crates/rspack_core/src/compiler/make/repair/mod.rs index ee2d10411ad..2c4fc28c052 100644 --- a/crates/rspack_core/src/compiler/make/repair/mod.rs +++ b/crates/rspack_core/src/compiler/make/repair/mod.rs @@ -14,12 +14,12 @@ use crate::{ module_graph::{ModuleGraph, ModuleGraphPartial}, old_cache::Cache as OldCache, utils::task_loop::{run_task_loop, Task}, - BuildDependency, Compilation, CompilerOptions, DependencyType, Module, ModuleFactory, - ModuleProfile, NormalModuleSource, ResolverFactory, SharedPluginDriver, + BuildDependency, Compilation, CompilationId, CompilerOptions, DependencyType, Module, + ModuleFactory, ModuleProfile, NormalModuleSource, ResolverFactory, SharedPluginDriver, }; pub struct MakeTaskContext { - // compilation info + pub compilation_id: CompilationId, pub plugin_driver: SharedPluginDriver, pub buildtime_plugin_driver: SharedPluginDriver, pub fs: Arc, @@ -35,6 +35,7 @@ pub struct MakeTaskContext { impl MakeTaskContext { pub fn new(compilation: &Compilation, artifact: MakeArtifact) -> Self { Self { + compilation_id: compilation.id(), plugin_driver: compilation.plugin_driver.clone(), buildtime_plugin_driver: compilation.buildtime_plugin_driver.clone(), compiler_options: compilation.options.clone(), @@ -123,6 +124,7 @@ pub async fn repair( } }); Some(Box::new(factorize::FactorizeTask { + compilation_id: compilation.id(), module_factory: compilation.get_dependency_factory(dependency), original_module_identifier: parent_module_identifier, original_module_source, diff --git a/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs b/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs index def63b7a81d..285a6938b13 100644 --- a/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs +++ b/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs @@ -96,6 +96,7 @@ impl Task for ProcessDependenciesTask { }) .clone(); res.push(Box::new(FactorizeTask { + compilation_id: context.compilation_id, module_factory, original_module_identifier: Some(module.identifier()), original_module_context: module.get_context(), diff --git a/crates/rspack_core/src/compiler/module_executor/entry.rs b/crates/rspack_core/src/compiler/module_executor/entry.rs index 95d61b5ad9c..60eb6b09dbe 100644 --- a/crates/rspack_core/src/compiler/module_executor/entry.rs +++ b/crates/rspack_core/src/compiler/module_executor/entry.rs @@ -22,6 +22,7 @@ impl Task for EntryTask { module_graph.add_dependency(dep.clone()); Ok(vec![Box::new(FactorizeTask { + compilation_id: context.compilation_id, module_factory: context .dependency_factories .get(dep.dependency_type()) diff --git a/crates/rspack_core/src/loader/loader_runner.rs b/crates/rspack_core/src/loader/loader_runner.rs index 53a02b843d1..58300f3e857 100644 --- a/crates/rspack_core/src/loader/loader_runner.rs +++ b/crates/rspack_core/src/loader/loader_runner.rs @@ -3,10 +3,11 @@ use std::{ptr::NonNull, sync::Arc}; pub use rspack_loader_runner::{run_loaders, Content, Loader, LoaderContext}; use rspack_util::source_map::SourceMapKind; -use crate::{CompilerOptions, Module, ResolverFactory}; +use crate::{CompilationId, CompilerOptions, Module, ResolverFactory}; #[derive(Debug, Clone)] pub struct RunnerContext { + pub compilation_id: CompilationId, pub options: Arc, pub resolver_factory: Arc, pub module: NonNull, diff --git a/crates/rspack_core/src/module.rs b/crates/rspack_core/src/module.rs index b34701f8f61..15d9bd601ae 100644 --- a/crates/rspack_core/src/module.rs +++ b/crates/rspack_core/src/module.rs @@ -20,13 +20,14 @@ use crate::concatenated_module::ConcatenatedModule; use crate::dependencies_block::dependencies_block_update_hash; use crate::{ AsyncDependenciesBlock, BoxDependency, ChunkGraph, ChunkUkey, CodeGenerationResult, Compilation, - CompilerOptions, ConcatenationScope, ConnectionState, Context, ContextModule, DependenciesBlock, - DependencyId, DependencyTemplate, ExportInfoProvided, ExternalModule, ModuleDependency, - ModuleGraph, ModuleLayer, ModuleType, NormalModule, RawModule, Resolve, ResolverFactory, - RuntimeSpec, SelfModule, SharedPluginDriver, SourceType, + CompilationId, CompilerOptions, ConcatenationScope, ConnectionState, Context, ContextModule, + DependenciesBlock, DependencyId, DependencyTemplate, ExportInfoProvided, ExternalModule, + ModuleDependency, ModuleGraph, ModuleLayer, ModuleType, NormalModule, RawModule, Resolve, + ResolverFactory, RuntimeSpec, SelfModule, SharedPluginDriver, SourceType, }; pub struct BuildContext { + pub compilation_id: CompilationId, pub compiler_options: Arc, pub resolver_factory: Arc, pub plugin_driver: SharedPluginDriver, diff --git a/crates/rspack_core/src/module_factory.rs b/crates/rspack_core/src/module_factory.rs index c078390506e..7e59f0e78db 100644 --- a/crates/rspack_core/src/module_factory.rs +++ b/crates/rspack_core/src/module_factory.rs @@ -4,11 +4,13 @@ use rspack_error::{Diagnostic, Result}; use rustc_hash::FxHashSet as HashSet; use crate::{ - BoxDependency, BoxModule, CompilerOptions, Context, ModuleIdentifier, ModuleLayer, Resolve, + BoxDependency, BoxModule, CompilationId, CompilerOptions, Context, ModuleIdentifier, ModuleLayer, + Resolve, }; #[derive(Debug, Clone)] pub struct ModuleFactoryCreateData { + pub compilation_id: CompilationId, pub resolve_options: Option>, pub options: Arc, pub context: Context, diff --git a/crates/rspack_core/src/normal_module.rs b/crates/rspack_core/src/normal_module.rs index 462e3c0a87a..1801c4f211b 100644 --- a/crates/rspack_core/src/normal_module.rs +++ b/crates/rspack_core/src/normal_module.rs @@ -425,6 +425,7 @@ impl Module for NormalModule { self.resource_data.clone(), Some(plugin.clone()), RunnerContext { + compilation_id: build_context.compilation_id, options: build_context.compiler_options.clone(), resolver_factory: build_context.resolver_factory.clone(), #[allow(clippy::unwrap_used)] diff --git a/crates/rspack_napi/src/js_values/one_shot_value_ref.rs b/crates/rspack_napi/src/js_values/one_shot_value_ref.rs index 2b11898f8f7..b8b0f12a753 100644 --- a/crates/rspack_napi/src/js_values/one_shot_value_ref.rs +++ b/crates/rspack_napi/src/js_values/one_shot_value_ref.rs @@ -5,7 +5,9 @@ use std::marker::PhantomData; use std::ptr; use std::rc::Rc; -use napi::bindgen_prelude::{check_status, FromNapiValue, ToNapiValue}; +use napi::bindgen_prelude::{ + check_status, FromNapiMutRef, FromNapiRef, FromNapiValue, ToNapiValue, +}; use napi::sys::{self, napi_env}; use napi::{Env, Result}; @@ -59,6 +61,34 @@ impl OneShotRef { } } +impl OneShotRef { + pub fn from_napi_ref(&self) -> Result<&T> { + let r = unsafe { + let mut result = ptr::null_mut(); + check_status!( + sys::napi_get_reference_value(self.env, self.napi_ref, &mut result), + "Failed to get reference value" + )?; + T::from_napi_ref(self.env, result)? + }; + Ok(r) + } +} + +impl OneShotRef { + pub fn from_napi_mut_ref(&self) -> Result<&mut T> { + let r = unsafe { + let mut result = ptr::null_mut(); + check_status!( + sys::napi_get_reference_value(self.env, self.napi_ref, &mut result), + "Failed to get reference value" + )?; + T::from_napi_mut_ref(self.env, result)? + }; + Ok(r) + } +} + impl Drop for OneShotRef { fn drop(&mut self) { if !*self.cleanup_flag.borrow() { diff --git a/crates/rspack_plugin_devtool/src/source_map_dev_tool_module_options_plugin.rs b/crates/rspack_plugin_devtool/src/source_map_dev_tool_module_options_plugin.rs index 7576a9f0648..dca98bbdb8c 100644 --- a/crates/rspack_plugin_devtool/src/source_map_dev_tool_module_options_plugin.rs +++ b/crates/rspack_plugin_devtool/src/source_map_dev_tool_module_options_plugin.rs @@ -1,5 +1,5 @@ use rspack_core::{ - ApplyContext, BoxModule, ChunkUkey, Compilation, CompilationBuildModule, + ApplyContext, BoxModule, ChunkUkey, Compilation, CompilationBuildModule, CompilationId, CompilationRuntimeModule, CompilerOptions, ModuleIdentifier, Plugin, PluginContext, }; use rspack_error::Result; @@ -25,7 +25,7 @@ impl SourceMapDevToolModuleOptionsPlugin { } #[plugin_hook(CompilationBuildModule for SourceMapDevToolModuleOptionsPlugin)] -async fn build_module(&self, module: &mut BoxModule) -> Result<()> { +async fn build_module(&self, _compilation_id: CompilationId, module: &mut BoxModule) -> Result<()> { if self.module { module.set_source_map_kind(SourceMapKind::SourceMap); } else { diff --git a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs index feeb2be87b8..d4d3d558197 100644 --- a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs @@ -632,6 +632,7 @@ impl ModuleConcatenationPlugin { new_module .build( rspack_core::BuildContext { + compilation_id: compilation.id(), resolver_factory: compilation.resolver_factory.clone(), plugin_driver: compilation.plugin_driver.clone(), compiler_options: compilation.options.clone(), diff --git a/crates/rspack_plugin_lazy_compilation/src/factory.rs b/crates/rspack_plugin_lazy_compilation/src/factory.rs index 7b07381da70..ca6670badc3 100644 --- a/crates/rspack_plugin_lazy_compilation/src/factory.rs +++ b/crates/rspack_plugin_lazy_compilation/src/factory.rs @@ -33,6 +33,7 @@ impl ModuleFactory for LazyCompilationDependencyFactory { let dep = dep.clone(); let mut create_data = ModuleFactoryCreateData { + compilation_id: data.compilation_id, resolve_options: proxy_data.resolve_options.clone(), options: data.options.clone(), context: proxy_data.context.clone(), diff --git a/crates/rspack_plugin_lazy_compilation/src/plugin.rs b/crates/rspack_plugin_lazy_compilation/src/plugin.rs index 27d881fe5bc..aef2454cbf0 100644 --- a/crates/rspack_plugin_lazy_compilation/src/plugin.rs +++ b/crates/rspack_plugin_lazy_compilation/src/plugin.rs @@ -2,8 +2,8 @@ use std::sync::LazyLock; use std::{fmt::Debug, sync::Arc}; use rspack_core::{ - ApplyContext, BoxModule, Compilation, CompilationParams, CompilerCompilation, CompilerOptions, - DependencyType, EntryDependency, Module, ModuleFactory, ModuleFactoryCreateData, + ApplyContext, BoxModule, Compilation, CompilationId, CompilationParams, CompilerCompilation, + CompilerOptions, DependencyType, EntryDependency, Module, ModuleFactory, ModuleFactoryCreateData, NormalModuleCreateData, NormalModuleFactoryModule, Plugin, PluginContext, }; use rspack_error::Result; @@ -29,16 +29,16 @@ pub enum LazyCompilationTest { } pub trait LazyCompilationTestCheck: Send + Sync + Debug { - fn test(&self, module: &dyn Module) -> bool; + fn test(&self, compilation_id: CompilationId, module: &dyn Module) -> bool; } impl LazyCompilationTest { - fn test(&self, module: &dyn Module) -> bool { + fn test(&self, compilation_id: CompilationId, module: &dyn Module) -> bool { match self { LazyCompilationTest::Regex(regex) => { regex.test(&module.name_for_condition().unwrap_or("".into())) } - LazyCompilationTest::Fn(f) => f.test(module), + LazyCompilationTest::Fn(f) => f.test(compilation_id, module), } } } @@ -64,9 +64,9 @@ impl LazyCompilationPlugin { Self::new_inner(Mutex::new(backend), entries, imports, test, cacheable) } - fn check_test(&self, module: &BoxModule) -> bool { + fn check_test(&self, compilation_id: CompilationId, module: &BoxModule) -> bool { if let Some(test) = &self.inner.test { - test.test(module.as_ref()) + test.test(compilation_id, module.as_ref()) } else { true } @@ -149,7 +149,7 @@ async fn normal_module_factory_module( } if WEBPACK_DEV_SERVER_CLIENT_RE.test(&create_data.resource_resolve_data.resource) - || !self.check_test(module) + || !self.check_test(module_factory_create_data.compilation_id, module) { return Ok(()); } diff --git a/crates/rspack_plugin_progress/src/lib.rs b/crates/rspack_plugin_progress/src/lib.rs index 2ed58772ac7..dc6cceeb4d0 100644 --- a/crates/rspack_plugin_progress/src/lib.rs +++ b/crates/rspack_plugin_progress/src/lib.rs @@ -10,7 +10,7 @@ use rspack_collections::IdentifierMap; use rspack_core::{ ApplyContext, BoxModule, Compilation, CompilationAfterOptimizeModules, CompilationAfterProcessAssets, CompilationBuildModule, CompilationChunkIds, - CompilationFinishModules, CompilationModuleIds, CompilationOptimizeChunkModules, + CompilationFinishModules, CompilationId, CompilationModuleIds, CompilationOptimizeChunkModules, CompilationOptimizeChunks, CompilationOptimizeDependencies, CompilationOptimizeModules, CompilationOptimizeTree, CompilationParams, CompilationProcessAssets, CompilationSeal, CompilationSucceedModule, CompilerAfterEmit, CompilerCompilation, CompilerEmit, @@ -321,7 +321,7 @@ async fn make(&self, _compilation: &mut Compilation) -> Result<()> { } #[plugin_hook(CompilationBuildModule for ProgressPlugin)] -async fn build_module(&self, module: &mut BoxModule) -> Result<()> { +async fn build_module(&self, _compilation_id: CompilationId, module: &mut BoxModule) -> Result<()> { self .active_modules .write() @@ -343,7 +343,11 @@ async fn build_module(&self, module: &mut BoxModule) -> Result<()> { } #[plugin_hook(CompilationSucceedModule for ProgressPlugin)] -async fn succeed_module(&self, module: &mut BoxModule) -> Result<()> { +async fn succeed_module( + &self, + _compilation_id: CompilationId, + module: &mut BoxModule, +) -> Result<()> { self.modules_done.fetch_add(1, Relaxed); self .last_active_module diff --git a/packages/rspack/src/Module.ts b/packages/rspack/src/Module.ts index c67b9623e80..a4164237e3c 100644 --- a/packages/rspack/src/Module.ts +++ b/packages/rspack/src/Module.ts @@ -191,7 +191,6 @@ const MODULE_MAPPINGS = new WeakMap(); export class Module { #inner: JsModule; - #originalSource?: Source; declare readonly context?: string; declare readonly resource?: string; @@ -340,12 +339,8 @@ export class Module { } originalSource(): Source | null { - if (this.#originalSource) return this.#originalSource; if (this.#inner.originalSource) { - this.#originalSource = JsSource.__from_binding( - this.#inner.originalSource - ); - return this.#originalSource; + return JsSource.__from_binding(this.#inner.originalSource); } return null; }