diff --git a/crates/rspack_core/src/build_chunk_graph/code_splitter.rs b/crates/rspack_core/src/build_chunk_graph/code_splitter.rs index 58f30320d72f..65ad727baa56 100644 --- a/crates/rspack_core/src/build_chunk_graph/code_splitter.rs +++ b/crates/rspack_core/src/build_chunk_graph/code_splitter.rs @@ -1,14 +1,13 @@ -use std::sync::Arc; - use anyhow::anyhow; use rspack_error::{internal_error, Result}; use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; use super::remove_parent_modules::RemoveParentModulesContext; use crate::{ - AsyncDependenciesBlockIdentifier, BoxDependency, ChunkGroup, ChunkGroupInfo, ChunkGroupKind, - ChunkGroupOptions, ChunkGroupUkey, ChunkLoading, ChunkUkey, Compilation, DependenciesBlock, - GroupOptions, Logger, ModuleGraphConnection, ModuleIdentifier, RuntimeSpec, IS_NEW_TREESHAKING, + get_entry_runtime, AsyncDependenciesBlockIdentifier, BoxDependency, ChunkGroup, ChunkGroupInfo, + ChunkGroupKind, ChunkGroupOptions, ChunkGroupUkey, ChunkLoading, ChunkUkey, Compilation, + DependenciesBlock, GroupOptions, Logger, ModuleGraphConnection, ModuleIdentifier, RuntimeSpec, + IS_NEW_TREESHAKING, }; pub(super) struct CodeSplitter<'me> { @@ -95,9 +94,7 @@ impl<'me> CodeSplitter<'me> { let mut entrypoint = ChunkGroup::new( ChunkGroupKind::new_entrypoint(true, Box::new(options.clone())), ChunkGroupInfo { - runtime: HashSet::from_iter([Arc::from( - options.runtime.clone().unwrap_or_else(|| name.to_string()), - )]), + runtime: get_entry_runtime(name, options), chunk_loading: !matches!( options .chunk_loading diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 1310c8b3af84..6a3793e4c8c6 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -41,11 +41,12 @@ use crate::{ ChunkGraph, ChunkGroupByUkey, ChunkGroupUkey, ChunkHashArgs, ChunkKind, ChunkUkey, CleanQueue, CleanTask, CleanTaskResult, CodeGenerationResult, CodeGenerationResults, CompilationLogger, CompilationLogging, CompilerOptions, ContentHashArgs, ContextDependency, DependencyId, - DependencyParents, Entry, EntryData, EntryOptions, Entrypoint, FactorizeQueue, FactorizeTask, - FactorizeTaskResult, Filename, Logger, Module, ModuleGraph, ModuleIdentifier, ModuleProfile, - ModuleType, PathData, ProcessAssetsArgs, ProcessDependenciesQueue, ProcessDependenciesResult, - ProcessDependenciesTask, RenderManifestArgs, Resolve, ResolverFactory, RuntimeGlobals, - RuntimeModule, RuntimeSpec, SharedPluginDriver, SourceType, Stats, TaskResult, WorkerTask, + DependencyParents, DependencyType, Entry, EntryData, EntryOptions, Entrypoint, FactorizeQueue, + FactorizeTask, FactorizeTaskResult, Filename, Logger, Module, ModuleGraph, ModuleIdentifier, + ModuleProfile, ModuleType, PathData, ProcessAssetsArgs, ProcessDependenciesQueue, + ProcessDependenciesResult, ProcessDependenciesTask, RenderManifestArgs, Resolve, ResolverFactory, + RuntimeGlobals, RuntimeModule, RuntimeSpec, SharedPluginDriver, SourceType, Stats, TaskResult, + WorkerTask, }; use crate::{tree_shaking::visitor::OptimizeAnalyzeResult, Context}; @@ -941,11 +942,13 @@ impl Compilation { if self.options.builtins.tree_shaking.enable() { self.bailout_module_identifiers = self .module_graph - .modules() + .dependencies() .values() .par_bridge() - .filter_map(|module| { - if module.as_context_module().is_some() { + .filter_map(|dep| { + if dep.as_context_dependency().is_some() + && let Some(module) = self.module_graph.get_module(dep.id()) + { let mut values = vec![(module.identifier(), BailoutFlag::CONTEXT_MODULE)]; if let Some(dependencies) = self .module_graph @@ -962,6 +965,10 @@ impl Compilation { } Some(values) + } else if matches!(dep.dependency_type(), DependencyType::ContainerExposed) + && let Some(module) = self.module_graph.get_module(dep.id()) + { + Some(vec![(module.identifier(), BailoutFlag::CONTAINER_EXPOSED)]) } else { None } diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index e07a99aa4e33..d6c4c251955d 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -125,6 +125,10 @@ impl ModuleGraph { self.blocks.get(block_id) } + pub fn dependencies(&self) -> &HashMap { + &self.dependencies + } + pub fn add_dependency(&mut self, dependency: BoxDependency) { self.dependencies.insert(*dependency.id(), dependency); } diff --git a/crates/rspack_core/src/runtime_globals.rs b/crates/rspack_core/src/runtime_globals.rs index e8802d4cd419..e285b9e7b25c 100644 --- a/crates/rspack_core/src/runtime_globals.rs +++ b/crates/rspack_core/src/runtime_globals.rs @@ -222,7 +222,7 @@ bitflags! { const SYSTEM_CONTEXT = 1 << 49; const THIS_AS_EXPORTS = 1 << 50; - + const CURRENT_REMOTE_GET_SCOPE = 1 << 51; const SHARE_SCOPE_MAP = 1 << 52; diff --git a/crates/rspack_core/src/tree_shaking/mod.rs b/crates/rspack_core/src/tree_shaking/mod.rs index e9e58a6202f9..8d8e2856d61b 100644 --- a/crates/rspack_core/src/tree_shaking/mod.rs +++ b/crates/rspack_core/src/tree_shaking/mod.rs @@ -67,6 +67,7 @@ bitflags::bitflags! { const COMMONJS_EXPORTS = 1 << 2; const DYNAMIC_IMPORT = 1 << 3; const CONTEXT_MODULE = 1 << 4; + const CONTAINER_EXPOSED = 1 << 5; } } diff --git a/crates/rspack_core/src/utils/mod.rs b/crates/rspack_core/src/utils/mod.rs index 7872cbe42228..660e6c6c8b78 100644 --- a/crates/rspack_core/src/utils/mod.rs +++ b/crates/rspack_core/src/utils/mod.rs @@ -6,6 +6,9 @@ use rustc_hash::FxHashMap as HashMap; mod identifier; pub use identifier::*; +mod runtime; +pub use runtime::*; + mod property_name; pub use property_name::*; diff --git a/crates/rspack_core/src/utils/runtime.rs b/crates/rspack_core/src/utils/runtime.rs new file mode 100644 index 000000000000..ba4dbfbbe4ca --- /dev/null +++ b/crates/rspack_core/src/utils/runtime.rs @@ -0,0 +1,9 @@ +use std::sync::Arc; + +use crate::{EntryOptions, RuntimeSpec}; + +pub fn get_entry_runtime(name: &str, options: &EntryOptions) -> RuntimeSpec { + RuntimeSpec::from_iter([Arc::from( + options.runtime.clone().unwrap_or_else(|| name.to_string()), + )]) +} diff --git a/crates/rspack_plugin_library/src/export_property_library_plugin.rs b/crates/rspack_plugin_library/src/export_property_library_plugin.rs index 40da566ed1b1..cf67afc529b6 100644 --- a/crates/rspack_plugin_library/src/export_property_library_plugin.rs +++ b/crates/rspack_plugin_library/src/export_property_library_plugin.rs @@ -17,11 +17,15 @@ struct ExportPropertyLibraryPluginParsed<'a> { #[derive(Debug, Default)] pub struct ExportPropertyLibraryPlugin { library_type: LibraryType, + _ns_object_used: bool, } impl ExportPropertyLibraryPlugin { - pub fn new(library_type: LibraryType) -> Self { - Self { library_type } + pub fn new(library_type: LibraryType, ns_object_used: bool) -> Self { + Self { + library_type, + _ns_object_used: ns_object_used, + } } fn parse_options<'a>( diff --git a/crates/rspack_plugin_library/src/lib.rs b/crates/rspack_plugin_library/src/lib.rs index 92b3b5dda4bd..e80ae3ebe19b 100644 --- a/crates/rspack_plugin_library/src/lib.rs +++ b/crates/rspack_plugin_library/src/lib.rs @@ -17,6 +17,7 @@ pub use system_library_plugin::SystemLibraryPlugin; pub use umd_library_plugin::UmdLibraryPlugin; pub fn enable_library_plugin(library_type: String, plugins: &mut Vec) { + let ns_object_used = library_type != "module"; match library_type.as_str() { "var" => plugins.push( AssignLibraryPlugin::new(AssignLibraryPluginOptions { @@ -99,19 +100,21 @@ pub fn enable_library_plugin(library_type: String, plugins: &mut Vec) .boxed(), ), "umd" | "umd2" => { - plugins.push(ExportPropertyLibraryPlugin::default().boxed()); + plugins.push(ExportPropertyLibraryPlugin::new(library_type.clone(), ns_object_used).boxed()); plugins.push(UmdLibraryPlugin::new("umd2" == library_type, library_type).boxed()); } "amd" | "amd-require" => { - plugins.push(ExportPropertyLibraryPlugin::default().boxed()); + plugins.push(ExportPropertyLibraryPlugin::new(library_type.clone(), ns_object_used).boxed()); plugins.push(AmdLibraryPlugin::new("amd-require" == library_type, library_type).boxed()); } "module" => { - plugins.push(ExportPropertyLibraryPlugin::default().boxed()); + plugins.push(ExportPropertyLibraryPlugin::new(library_type.clone(), ns_object_used).boxed()); plugins.push(ModuleLibraryPlugin::default().boxed()); } "system" => { - plugins.push(ExportPropertyLibraryPlugin::default().boxed()); + plugins.push( + ExportPropertyLibraryPlugin::new(library_type.clone(), library_type != "module").boxed(), + ); plugins.push(SystemLibraryPlugin::default().boxed()); } _ => {} diff --git a/packages/rspack-dev-server/tests/normalizeOptions.test.ts b/packages/rspack-dev-server/tests/normalizeOptions.test.ts index 5e1cbcbb3ad1..f7a3522a8118 100644 --- a/packages/rspack-dev-server/tests/normalizeOptions.test.ts +++ b/packages/rspack-dev-server/tests/normalizeOptions.test.ts @@ -175,7 +175,7 @@ async function getAdditionEntries( await server.start(); const entries = compiler.builtinPlugins .filter(p => p.name === "EntryPlugin") - .map(p => p.raw().options) + .map(p => p.options) .reduce((acc, cur: any) => { const name = cur.options.name; const request = cur.entry;