From 8a2990b91fc20176e41dd4962647783be0104919 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Tue, 10 Oct 2023 19:28:59 +0800 Subject: [PATCH] feat: Adding runtime param when executing codegen (#4263) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: 🤖 init * chore: 🤖 add runtime * chore: 🤖 update runtime * chore: 🤖 refactor * chore: 🤖 pass test * chore: 🤖 add runtime param * chore: 🤖 fix compile issue * chore: 🤖 fix test case * chore: 🤖 recover binding.d.ts * chore: 🤖 remove unnecessary comment --- .../src/cache/occasion/code_generate.rs | 12 ++- .../src/code_generation_results.rs | 50 ++++++++-- .../rspack_core/src/compiler/compilation.rs | 98 ++++++++++++------- crates/rspack_core/src/compiler/hmr.rs | 4 +- crates/rspack_core/src/context_module.rs | 8 +- .../src/dependency/dependency_template.rs | 3 +- crates/rspack_core/src/external_module.rs | 8 +- crates/rspack_core/src/missing_module.rs | 10 +- crates/rspack_core/src/module.rs | 18 +++- crates/rspack_core/src/module_graph/mod.rs | 8 +- crates/rspack_core/src/normal_module.rs | 14 ++- .../rspack_core/src/parser_and_generator.rs | 2 + crates/rspack_core/src/raw_module.rs | 8 +- crates/rspack_core/src/runtime.rs | 15 ++- .../rspack_plugin_css/src/dependency/url.rs | 6 +- .../src/parser_and_generator/mod.rs | 1 + .../src/parser_and_generator/mod.rs | 1 + .../src/lazy_compilation.rs | 8 +- .../src/runtime_module/module_macro.rs | 1 + 19 files changed, 200 insertions(+), 75 deletions(-) diff --git a/crates/rspack_core/src/cache/occasion/code_generate.rs b/crates/rspack_core/src/cache/occasion/code_generate.rs index 6ba1d103cde..e42236803ac 100644 --- a/crates/rspack_core/src/cache/occasion/code_generate.rs +++ b/crates/rspack_core/src/cache/occasion/code_generate.rs @@ -2,8 +2,9 @@ use rspack_error::Result; use rspack_identifier::Identifier; use crate::{cache::storage, BoxModule, CodeGenerationResult, Compilation, NormalModuleSource}; +use crate::{RuntimeSpec, RuntimeSpecSet}; -type Storage = dyn storage::Storage; +type Storage = dyn storage::Storage>; #[derive(Debug)] pub struct CodeGenerateOccasion { @@ -19,16 +20,17 @@ impl CodeGenerateOccasion { pub fn use_cache<'a, G>( &self, module: &'a BoxModule, + runtimes: RuntimeSpecSet, compilation: &Compilation, generator: G, - ) -> Result<(CodeGenerationResult, bool)> + ) -> Result<(Vec<(CodeGenerationResult, RuntimeSpec)>, bool)> where - G: Fn(&'a BoxModule) -> Result, + G: Fn(&'a BoxModule, RuntimeSpecSet) -> Result>, { let storage = match &self.storage { Some(s) => s, // no cache return directly - None => return Ok((generator(module)?, false)), + None => return Ok((generator(module, runtimes)?, false)), }; let mut cache_id = None; @@ -55,7 +57,7 @@ impl CodeGenerateOccasion { } // run generator and save to cache - let data = generator(module)?; + let data = generator(module, runtimes)?; if let Some(id) = cache_id { storage.set(id, data.clone()); } diff --git a/crates/rspack_core/src/code_generation_results.rs b/crates/rspack_core/src/code_generation_results.rs index 2f10abde5c1..0a8a1e2fcee 100644 --- a/crates/rspack_core/src/code_generation_results.rs +++ b/crates/rspack_core/src/code_generation_results.rs @@ -1,17 +1,20 @@ use std::collections::hash_map::Entry; use std::hash::Hash; use std::ops::{Deref, DerefMut}; +use std::sync::atomic::AtomicU32; use anymap::CloneAny; +use once_cell::sync::Lazy; use rspack_error::{internal_error, Result}; use rspack_hash::{HashDigest, HashFunction, HashSalt, RspackHash, RspackHashDigest}; use rspack_identifier::IdentifierMap; use rspack_sources::BoxSource; use rustc_hash::FxHashMap as HashMap; +use serde::Serialize; use crate::{ - AssetInfo, ChunkInitFragments, ModuleIdentifier, RuntimeGlobals, RuntimeSpec, RuntimeSpecMap, - SourceType, + AssetInfo, ChunkInitFragments, ModuleIdentifier, RuntimeGlobals, RuntimeMode, RuntimeSpec, + RuntimeSpecMap, SourceType, }; #[derive(Clone, Debug)] @@ -86,6 +89,7 @@ pub struct CodeGenerationResult { pub chunk_init_fragments: ChunkInitFragments, pub runtime_requirements: RuntimeGlobals, pub hash: Option, + pub id: CodeGenResultId, } impl CodeGenerationResult { @@ -133,14 +137,48 @@ impl CodeGenerationResult { } } +#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)] +pub struct CodeGenResultId(u32); + +impl Default for CodeGenResultId { + fn default() -> Self { + Self(CODE_GEN_RESULT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed)) + } +} + +pub static CODE_GEN_RESULT_ID: Lazy = Lazy::new(|| AtomicU32::new(0)); + #[derive(Debug, Default)] pub struct CodeGenerationResults { - // TODO: This should be a map of ModuleIdentifier to CodeGenerationResult - pub module_generation_result_map: IdentifierMap, - map: IdentifierMap>, + pub module_generation_result_map: HashMap, + map: IdentifierMap>, } impl CodeGenerationResults { + pub fn get_one(&self, module_identifier: &ModuleIdentifier) -> Option<&CodeGenerationResult> { + self + .map + .get(module_identifier) + .and_then(|spec| match spec.mode { + RuntimeMode::Empty => None, + RuntimeMode::SingleEntry => spec + .single_value + .and_then(|result_id| self.module_generation_result_map.get(&result_id)), + RuntimeMode::Map => spec + .map + .values() + .next() + .and_then(|result_id| self.module_generation_result_map.get(result_id)), + }) + } + + pub fn clear_entry( + &mut self, + module_identifier: &ModuleIdentifier, + ) -> Option<(ModuleIdentifier, RuntimeSpecMap)> { + self.map.remove_entry(module_identifier) + } + pub fn get( &self, module_identifier: &ModuleIdentifier, @@ -195,7 +233,7 @@ impl CodeGenerationResults { &mut self, module_identifier: ModuleIdentifier, runtime: RuntimeSpec, - result: ModuleIdentifier, + result: CodeGenResultId, ) { match self.map.entry(module_identifier) { Entry::Occupied(mut record) => { diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 151b1f3f8d9..482fa0c7cc1 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -960,55 +960,85 @@ impl Compilation { codegen_cache_counter: &mut Option, filter_op: impl Fn(&(&ModuleIdentifier, &Box)) -> bool + Sync + Send, ) -> Result<()> { + // If the runtime optimization is not opt out, a module codegen should be executed for each runtime. + // Else, share same codegen result for all runtimes. + let used_exports_optimization = compilation.options.is_new_tree_shaking() + && compilation.options.optimization.used_exports.is_true(); let results = compilation .module_graph .modules() .par_iter() .filter(filter_op) - .filter(|(module_identifier, _)| { + .filter_map(|(module_identifier, module)| { let runtimes = compilation .chunk_graph - .get_module_runtimes(**module_identifier, &compilation.chunk_by_ukey); - !runtimes.is_empty() - }) - .map(|(module_identifier, module)| { - compilation + .get_module_runtimes(*module_identifier, &compilation.chunk_by_ukey); + if runtimes.is_empty() { + return None; + } + + let res = compilation .cache .code_generate_occasion - .use_cache(module, compilation, |module| { - module.code_generation(compilation) + .use_cache(module, runtimes, compilation, |module, runtimes| { + let take_length = if used_exports_optimization { + runtimes.len() + } else { + // Only codegen once + 1 + }; + let mut codegen_list = vec![]; + for runtime in runtimes.into_values().take(take_length) { + codegen_list.push(( + module.code_generation(compilation, Some(&runtime))?, + runtime, + )); + } + Ok(codegen_list) }) - .map(|(result, from_cache)| (*module_identifier, result, from_cache)) + .map(|(result, from_cache)| (*module_identifier, result, from_cache)); + Some(res) }) - .collect::>>()?; - + .collect::, + bool, + )>, + >>()?; results .into_iter() - .for_each(|(module_identifier, result, from_cache)| { - if let Some(counter) = codegen_cache_counter { - if from_cache { - counter.hit(); - } else { - counter.miss(); + .for_each(|(module_identifier, item, from_cache)| { + item.into_iter().for_each(|(result, runtime)| { + if let Some(counter) = codegen_cache_counter { + if from_cache { + counter.hit(); + } else { + counter.miss(); + } } - } - compilation.code_generated_modules.insert(module_identifier); + compilation.code_generated_modules.insert(module_identifier); - let runtimes = compilation - .chunk_graph - .get_module_runtimes(module_identifier, &compilation.chunk_by_ukey); - - compilation - .code_generation_results - .module_generation_result_map - .insert(module_identifier, result); - for runtime in runtimes.values() { - compilation.code_generation_results.add( - module_identifier, - runtime.clone(), - module_identifier, - ); - } + let runtimes = compilation + .chunk_graph + .get_module_runtimes(module_identifier, &compilation.chunk_by_ukey); + let result_id = result.id; + compilation + .code_generation_results + .module_generation_result_map + .insert(result.id, result); + if used_exports_optimization { + compilation + .code_generation_results + .add(module_identifier, runtime, result_id); + } else { + for runtime in runtimes.into_values() { + compilation + .code_generation_results + .add(module_identifier, runtime, result_id); + } + } + }) }); Ok(()) } diff --git a/crates/rspack_core/src/compiler/hmr.rs b/crates/rspack_core/src/compiler/hmr.rs index e1f6c502ef8..e1cda6ac707 100644 --- a/crates/rspack_core/src/compiler/hmr.rs +++ b/crates/rspack_core/src/compiler/hmr.rs @@ -169,7 +169,9 @@ pub fn collect_changed_modules( .par_iter() .filter_map(|(identifier, cgm)| { let cid = cgm.id.as_deref(); - if let Some(code_generation_result) = compilation.code_generation_results.module_generation_result_map.get(identifier) && let Some(module_hash) = &code_generation_result.hash && let Some(cid) = cid { + // TODO: Determine how to calc module hash if module related to multiple runtime code + // gen + if let Some(code_generation_result) = compilation.code_generation_results.get_one(identifier) && let Some(module_hash) = &code_generation_result.hash && let Some(cid) = cid { Some(( *identifier, ( diff --git a/crates/rspack_core/src/context_module.rs b/crates/rspack_core/src/context_module.rs index 4b2b1f16382..71aa50ca7db 100644 --- a/crates/rspack_core/src/context_module.rs +++ b/crates/rspack_core/src/context_module.rs @@ -20,7 +20,7 @@ use crate::{ BuildMeta, BuildResult, ChunkGraph, CodeGenerationResult, Compilation, ContextElementDependency, DependencyCategory, DependencyId, DependencyType, ExportsType, FakeNamespaceObjectMode, LibIdentOptions, Module, ModuleType, Resolve, ResolveInnerOptions, - ResolveOptionsWithDependencyType, ResolverFactory, RuntimeGlobals, SourceType, + ResolveOptionsWithDependencyType, ResolverFactory, RuntimeGlobals, RuntimeSpec, SourceType, }; #[derive(Debug, Clone)] @@ -482,7 +482,11 @@ impl Module for ContextModule { self.resolve_dependencies(build_context) } - fn code_generation(&self, compilation: &Compilation) -> Result { + fn code_generation( + &self, + compilation: &Compilation, + _runtime: Option<&RuntimeSpec>, + ) -> Result { let mut code_generation_result = CodeGenerationResult::default(); code_generation_result .runtime_requirements diff --git a/crates/rspack_core/src/dependency/dependency_template.rs b/crates/rspack_core/src/dependency/dependency_template.rs index 664b7b12ee4..f0dfe32743b 100644 --- a/crates/rspack_core/src/dependency/dependency_template.rs +++ b/crates/rspack_core/src/dependency/dependency_template.rs @@ -3,13 +3,14 @@ use std::fmt::Debug; use dyn_clone::{clone_trait_object, DynClone}; use rspack_sources::{BoxSource, ReplaceSource}; -use crate::{Compilation, Module, ModuleInitFragments, RuntimeGlobals}; +use crate::{Compilation, Module, ModuleInitFragments, RuntimeGlobals, RuntimeSpec}; pub struct TemplateContext<'a, 'b> { pub compilation: &'a Compilation, pub module: &'a dyn Module, pub runtime_requirements: &'a mut RuntimeGlobals, pub init_fragments: &'a mut ModuleInitFragments<'b>, + pub runtime: Option<&'a RuntimeSpec>, } pub type TemplateReplaceSource = ReplaceSource; diff --git a/crates/rspack_core/src/external_module.rs b/crates/rspack_core/src/external_module.rs index 09076d8dcf2..de35c1478a5 100644 --- a/crates/rspack_core/src/external_module.rs +++ b/crates/rspack_core/src/external_module.rs @@ -14,7 +14,7 @@ use crate::{ to_identifier, BuildContext, BuildInfo, BuildMetaExportsType, BuildResult, ChunkInitFragments, ChunkUkey, CodeGenerationDataUrl, CodeGenerationResult, Compilation, Context, ExternalType, InitFragmentExt, InitFragmentKey, InitFragmentStage, LibIdentOptions, Module, ModuleType, - NormalInitFragment, RuntimeGlobals, SourceType, + NormalInitFragment, RuntimeGlobals, RuntimeSpec, SourceType, }; static EXTERNAL_MODULE_JS_SOURCE_TYPES: &[SourceType] = &[SourceType::JavaScript]; @@ -316,7 +316,11 @@ impl Module for ExternalModule { Ok(build_result.with_empty_diagnostic()) } - fn code_generation(&self, compilation: &Compilation) -> Result { + fn code_generation( + &self, + compilation: &Compilation, + _runtime: Option<&RuntimeSpec>, + ) -> Result { let mut cgr = CodeGenerationResult::default(); let (request, external_type) = self.get_request_and_external_type(); match self.external_type.as_str() { diff --git a/crates/rspack_core/src/missing_module.rs b/crates/rspack_core/src/missing_module.rs index 72fe1d24267..031c8a91920 100644 --- a/crates/rspack_core/src/missing_module.rs +++ b/crates/rspack_core/src/missing_module.rs @@ -6,7 +6,9 @@ use rspack_identifier::{Identifiable, Identifier}; use rspack_sources::{RawSource, Source, SourceExt}; use serde_json::json; -use crate::{CodeGenerationResult, Compilation, Module, ModuleIdentifier, ModuleType, SourceType}; +use crate::{ + CodeGenerationResult, Compilation, Module, ModuleIdentifier, ModuleType, RuntimeSpec, SourceType, +}; #[derive(Debug, Eq)] pub struct MissingModule { @@ -52,7 +54,11 @@ impl Module for MissingModule { 160.0 } - fn code_generation(&self, compilation: &Compilation) -> Result { + fn code_generation( + &self, + compilation: &Compilation, + _runtime: Option<&RuntimeSpec>, + ) -> Result { let mut code_gen = CodeGenerationResult::default().with_javascript( RawSource::from(format!( "throw new Error({});\n", diff --git a/crates/rspack_core/src/module.rs b/crates/rspack_core/src/module.rs index 94e0ea436bd..e0c7f40695c 100644 --- a/crates/rspack_core/src/module.rs +++ b/crates/rspack_core/src/module.rs @@ -16,8 +16,8 @@ use crate::tree_shaking::visitor::OptimizeAnalyzeResult; use crate::{ BoxDependency, ChunkUkey, CodeGenerationResult, Compilation, CompilerContext, CompilerOptions, ConnectionState, Context, ContextModule, DependencyId, DependencyTemplate, ExternalModule, - ModuleDependency, ModuleGraph, ModuleType, NormalModule, RawModule, Resolve, SharedPluginDriver, - SourceType, + ModuleDependency, ModuleGraph, ModuleType, NormalModule, RawModule, Resolve, RuntimeSpec, + SharedPluginDriver, SourceType, }; pub struct BuildContext<'a> { @@ -186,7 +186,11 @@ pub trait Module: Debug + Send + Sync + AsAny + DynHash + DynEq + Identifiable { /// /// Code generation will often iterate through every `source_types` given by the module /// to provide multiple code generation results for different `source_type`s. - fn code_generation(&self, _compilation: &Compilation) -> Result; + fn code_generation( + &self, + _compilation: &Compilation, + _runtime: Option<&RuntimeSpec>, + ) -> Result; /// Name matched against bundle-splitting conditions. fn name_for_condition(&self) -> Option> { @@ -339,7 +343,7 @@ mod test { use super::Module; use crate::{ BuildContext, BuildResult, CodeGenerationResult, Compilation, Context, ModuleExt, ModuleType, - SourceType, + RuntimeSpec, SourceType, }; #[derive(Debug, Eq)] @@ -409,7 +413,11 @@ mod test { unreachable!() } - fn code_generation(&self, _compilation: &Compilation) -> Result { + fn code_generation( + &self, + _compilation: &Compilation, + _runtime: Option<&RuntimeSpec>, + ) -> Result { unreachable!() } } diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index 181089e26dc..eec01d18c41 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -599,7 +599,7 @@ mod test { use crate::{ BoxDependency, BuildContext, BuildResult, CodeGenerationResult, Compilation, Context, Dependency, DependencyId, ExportInfo, ExportsInfo, Module, ModuleDependency, ModuleGraph, - ModuleGraphModule, ModuleIdentifier, ModuleType, SourceType, UsageState, + ModuleGraphModule, ModuleIdentifier, ModuleType, RuntimeSpec, SourceType, UsageState, }; // Define a detailed node type for `ModuleGraphModule`s @@ -643,7 +643,11 @@ mod test { unreachable!() } - fn code_generation(&self, _compilation: &Compilation) -> Result { + fn code_generation( + &self, + _compilation: &Compilation, + _runtime: Option<&RuntimeSpec>, + ) -> Result { unreachable!() } } diff --git a/crates/rspack_core/src/normal_module.rs b/crates/rspack_core/src/normal_module.rs index 560cdabe92e..47735f304b6 100644 --- a/crates/rspack_core/src/normal_module.rs +++ b/crates/rspack_core/src/normal_module.rs @@ -30,7 +30,8 @@ use crate::{ BuildMeta, BuildResult, CodeGenerationResult, Compilation, CompilerOptions, ConnectionState, Context, DependencyTemplate, GenerateContext, GeneratorOptions, LibIdentOptions, LoaderRunnerPluginProcessResource, Module, ModuleDependency, ModuleGraph, ModuleIdentifier, - ModuleType, ParseContext, ParseResult, ParserAndGenerator, ParserOptions, Resolve, SourceType, + ModuleType, ParseContext, ParseResult, ParserAndGenerator, ParserOptions, Resolve, RuntimeSpec, + SourceType, }; bitflags! { @@ -194,6 +195,10 @@ impl NormalModule { } } + pub fn id(&self) -> ModuleIdentifier { + self.id + } + pub fn match_resource(&self) -> Option<&ResourceData> { self.match_resource.as_ref() } @@ -401,7 +406,11 @@ impl Module for NormalModule { ) } - fn code_generation(&self, compilation: &Compilation) -> Result { + fn code_generation( + &self, + compilation: &Compilation, + runtime: Option<&RuntimeSpec>, + ) -> Result { if let NormalModuleSource::BuiltSucceed(source) = &self.source { let mut code_generation_result = CodeGenerationResult::default(); for source_type in self.source_types() { @@ -414,6 +423,7 @@ impl Module for NormalModule { runtime_requirements: &mut code_generation_result.runtime_requirements, data: &mut code_generation_result.data, requested_source_type: *source_type, + runtime, }, )?; code_generation_result.add(*source_type, CachedSource::new(generation_result).boxed()); diff --git a/crates/rspack_core/src/parser_and_generator.rs b/crates/rspack_core/src/parser_and_generator.rs index e7d71882cad..07bb7329ee5 100644 --- a/crates/rspack_core/src/parser_and_generator.rs +++ b/crates/rspack_core/src/parser_and_generator.rs @@ -5,6 +5,7 @@ use rspack_error::{Result, TWithDiagnosticArray}; use rspack_loader_runner::ResourceData; use rspack_sources::BoxSource; +use crate::RuntimeSpec; use crate::{ tree_shaking::visitor::OptimizeAnalyzeResult, BoxDependency, BuildExtraDataType, BuildInfo, BuildMeta, CodeGenerationData, Compilation, CompilerOptions, DependencyTemplate, @@ -42,6 +43,7 @@ pub struct GenerateContext<'a> { pub runtime_requirements: &'a mut RuntimeGlobals, pub data: &'a mut CodeGenerationData, pub requested_source_type: SourceType, + pub runtime: Option<&'a RuntimeSpec>, } pub trait ParserAndGenerator: Send + Sync + Debug { diff --git a/crates/rspack_core/src/raw_module.rs b/crates/rspack_core/src/raw_module.rs index 12387645b34..bf539947232 100644 --- a/crates/rspack_core/src/raw_module.rs +++ b/crates/rspack_core/src/raw_module.rs @@ -8,7 +8,7 @@ use rspack_sources::{BoxSource, RawSource, Source, SourceExt}; use crate::{ BuildContext, BuildInfo, BuildResult, CodeGenerationResult, Context, Module, ModuleIdentifier, - ModuleType, RuntimeGlobals, SourceType, + ModuleType, RuntimeGlobals, RuntimeSpec, SourceType, }; #[derive(Debug)] @@ -86,7 +86,11 @@ impl Module for RawModule { ) } - fn code_generation(&self, compilation: &crate::Compilation) -> Result { + fn code_generation( + &self, + compilation: &crate::Compilation, + _runtime: Option<&RuntimeSpec>, + ) -> Result { let mut cgr = CodeGenerationResult::default(); cgr.runtime_requirements.insert(self.runtime_requirements); cgr.add(SourceType::JavaScript, self.source.clone()); diff --git a/crates/rspack_core/src/runtime.rs b/crates/rspack_core/src/runtime.rs index b201f2053c6..74db6fcd348 100644 --- a/crates/rspack_core/src/runtime.rs +++ b/crates/rspack_core/src/runtime.rs @@ -1,3 +1,4 @@ +use std::collections::hash_map::IntoValues; use std::{cmp::Ordering, fmt::Debug, sync::Arc}; use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; @@ -6,7 +7,7 @@ pub type RuntimeSpec = HashSet>; pub type RuntimeKey = String; #[derive(Default, Clone, Copy, Debug)] -enum RuntimeMode { +pub enum RuntimeMode { #[default] Empty = 0, SingleEntry = 1, @@ -50,11 +51,11 @@ pub fn compare_runtime(a: &RuntimeSpec, b: &RuntimeSpec) -> Ordering { #[derive(Default, Clone, Debug)] pub struct RuntimeSpecMap { - mode: RuntimeMode, - map: HashMap, + pub mode: RuntimeMode, + pub map: HashMap, - single_runtime: Option, - single_value: Option, + pub single_runtime: Option, + pub single_value: Option, } impl RuntimeSpecMap { @@ -152,6 +153,10 @@ impl RuntimeSpecSet { self.map.values().collect() } + pub fn into_values(self) -> IntoValues { + self.map.into_values() + } + pub fn len(&self) -> usize { self.map.len() } diff --git a/crates/rspack_plugin_css/src/dependency/url.rs b/crates/rspack_plugin_css/src/dependency/url.rs index 800f4c989af..b143ae26167 100644 --- a/crates/rspack_plugin_css/src/dependency/url.rs +++ b/crates/rspack_plugin_css/src/dependency/url.rs @@ -33,10 +33,8 @@ impl CssUrlDependency { identifier: &ModuleIdentifier, compilation: &Compilation, ) -> Option { - let code_gen_result = compilation - .code_generation_results - .module_generation_result_map - .get(identifier); + // TODO: how to handle if module related to multi runtime codegen + let code_gen_result = compilation.code_generation_results.get_one(identifier); if let Some(code_gen_result) = code_gen_result { if let Some(url) = code_gen_result.data.get::() { Some(url.inner().to_string()) diff --git a/crates/rspack_plugin_css/src/parser_and_generator/mod.rs b/crates/rspack_plugin_css/src/parser_and_generator/mod.rs index ec03c3e4351..c2f1fe1473f 100644 --- a/crates/rspack_plugin_css/src/parser_and_generator/mod.rs +++ b/crates/rspack_plugin_css/src/parser_and_generator/mod.rs @@ -236,6 +236,7 @@ impl ParserAndGenerator for CssParserAndGenerator { compilation, module, runtime_requirements: generate_context.runtime_requirements, + runtime: generate_context.runtime, init_fragments: &mut init_fragments, }; diff --git a/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs b/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs index 271d513498a..b57aa25e570 100644 --- a/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs +++ b/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs @@ -211,6 +211,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator { module, runtime_requirements: generate_context.runtime_requirements, init_fragments: &mut init_fragments, + runtime: generate_context.runtime, }; let mgm = compilation diff --git a/crates/rspack_plugin_runtime/src/lazy_compilation.rs b/crates/rspack_plugin_runtime/src/lazy_compilation.rs index c9a4e9862b5..0ee30a16909 100644 --- a/crates/rspack_plugin_runtime/src/lazy_compilation.rs +++ b/crates/rspack_plugin_runtime/src/lazy_compilation.rs @@ -5,7 +5,7 @@ use async_trait::async_trait; use rspack_core::{ rspack_sources::{RawSource, Source, SourceExt}, Compilation, DependencyType, Module, ModuleArgs, ModuleType, Plugin, PluginContext, - PluginModuleHookOutput, RuntimeGlobals, SourceType, + PluginModuleHookOutput, RuntimeGlobals, RuntimeSpec, SourceType, }; use rspack_core::{CodeGenerationResult, Context, ModuleIdentifier}; use rspack_error::Result; @@ -37,7 +37,11 @@ impl Module for LazyCompilationProxyModule { 200.0 } - fn code_generation(&self, compilation: &Compilation) -> Result { + fn code_generation( + &self, + compilation: &Compilation, + _runtime: Option<&RuntimeSpec>, + ) -> Result { let mut cgr = CodeGenerationResult::default(); cgr.runtime_requirements.insert(RuntimeGlobals::LOAD_SCRIPT); cgr.runtime_requirements.insert(RuntimeGlobals::MODULE); diff --git a/crates/rspack_plugin_runtime/src/runtime_module/module_macro.rs b/crates/rspack_plugin_runtime/src/runtime_module/module_macro.rs index e9ffe09ad7d..3f49a6e9e2b 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/module_macro.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/module_macro.rs @@ -44,6 +44,7 @@ macro_rules! impl_runtime_module { fn code_generation( &self, compilation: &rspack_core::Compilation, + _runtime: Option<&rspack_core::RuntimeSpec>, ) -> rspack_error::Result { let mut result = rspack_core::CodeGenerationResult::default(); result.add(