From ddc113c682849214b73173914c314dac12681547 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Mon, 28 Oct 2024 09:51:22 +0800 Subject: [PATCH 01/22] feat: rewrite dll-plugin by rust --- Cargo.lock | 17 ++ crates/node_binding/binding.d.ts | 18 ++ crates/rspack_binding_options/Cargo.toml | 1 + .../src/options/raw_builtins/mod.rs | 28 ++- .../src/options/raw_builtins/raw_dll.rs | 60 +++++ .../src/dependency/dependency_type.rs | 2 + crates/rspack_core/src/module.rs | 12 +- crates/rspack_plugin_dll/Cargo.toml | 24 ++ .../src/dll_entry/dll_entry_dependency.rs | 55 +++++ .../src/dll_entry/dll_entry_plugin.rs | 81 +++++++ .../src/dll_entry/dll_module.rs | 166 +++++++++++++ .../src/dll_entry/dll_module_factory.rs | 22 ++ crates/rspack_plugin_dll/src/dll_entry/mod.rs | 4 + .../src/flag_alll_modules_as_used_plugin.rs | 54 +++++ crates/rspack_plugin_dll/src/lib.rs | 7 + .../src/lib_manifest_plugin.rs | 218 ++++++++++++++++++ packages/rspack/etc/core.api.md | 18 ++ .../src/builtin-plugin/DllEntryPlugin.ts | 24 ++ .../FlagAllModulesAsUsedPlugin.ts | 7 + .../src/builtin-plugin/LibManifestPlugin.ts | 35 +++ packages/rspack/src/builtin-plugin/index.ts | 2 + packages/rspack/src/exports.ts | 1 + packages/rspack/src/lib/DllPlugin.ts | 94 ++++++++ 23 files changed, 942 insertions(+), 8 deletions(-) create mode 100644 crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs create mode 100644 crates/rspack_plugin_dll/Cargo.toml create mode 100644 crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs create mode 100644 crates/rspack_plugin_dll/src/dll_entry/dll_entry_plugin.rs create mode 100644 crates/rspack_plugin_dll/src/dll_entry/dll_module.rs create mode 100644 crates/rspack_plugin_dll/src/dll_entry/dll_module_factory.rs create mode 100644 crates/rspack_plugin_dll/src/dll_entry/mod.rs create mode 100644 crates/rspack_plugin_dll/src/flag_alll_modules_as_used_plugin.rs create mode 100644 crates/rspack_plugin_dll/src/lib.rs create mode 100644 crates/rspack_plugin_dll/src/lib_manifest_plugin.rs create mode 100644 packages/rspack/src/builtin-plugin/DllEntryPlugin.ts create mode 100644 packages/rspack/src/builtin-plugin/FlagAllModulesAsUsedPlugin.ts create mode 100644 packages/rspack/src/builtin-plugin/LibManifestPlugin.ts create mode 100644 packages/rspack/src/lib/DllPlugin.ts diff --git a/Cargo.lock b/Cargo.lock index fcdd9391198..9384e7cec03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3528,6 +3528,7 @@ dependencies = [ "rspack_plugin_copy", "rspack_plugin_css", "rspack_plugin_devtool", + "rspack_plugin_dll", "rspack_plugin_dynamic_entry", "rspack_plugin_ensure_chunk_conditions", "rspack_plugin_entry", @@ -4087,6 +4088,22 @@ dependencies = [ "tracing", ] +[[package]] +name = "rspack_plugin_dll" +version = "0.1.0" +dependencies = [ + "async-trait", + "rspack_collections", + "rspack_core", + "rspack_error", + "rspack_hook", + "rspack_macros", + "rspack_util", + "serde", + "serde_json", + "tracing", +] + [[package]] name = "rspack_plugin_dynamic_entry" version = "0.1.0" diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index c389affbe29..c469aeaf0af 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -301,6 +301,9 @@ export enum BuiltinPluginName { SizeLimitsPlugin = 'SizeLimitsPlugin', NoEmitOnErrorsPlugin = 'NoEmitOnErrorsPlugin', ContextReplacementPlugin = 'ContextReplacementPlugin', + DllEntryPlugin = 'DllEntryPlugin', + LibManifestPlugin = 'LibManifestPlugin', + FlagAllModulesAsUsedPlugin = 'FlagAllModulesAsUsedPlugin', HttpExternalsRspackPlugin = 'HttpExternalsRspackPlugin', CopyRspackPlugin = 'CopyRspackPlugin', HtmlRspackPlugin = 'HtmlRspackPlugin', @@ -1270,6 +1273,12 @@ export interface RawCssParserOptions { namedExports?: boolean } +export interface RawDllEntyPluginOptions { + context: string + entries: Array + name: string +} + export interface RawDraft { customMedia: boolean } @@ -1474,6 +1483,15 @@ export interface RawLazyCompilationOption { cacheable: boolean } +export interface RawLibManifestPluginOptions { + context?: string + entryOnly?: boolean + name?: JsFilename + path: JsFilename + format?: boolean + ty?: string +} + export interface RawLightningCssBrowsers { android?: number chrome?: number diff --git a/crates/rspack_binding_options/Cargo.toml b/crates/rspack_binding_options/Cargo.toml index 262d7f76624..23ee4681c37 100644 --- a/crates/rspack_binding_options/Cargo.toml +++ b/crates/rspack_binding_options/Cargo.toml @@ -40,6 +40,7 @@ rspack_plugin_context_replacement = { version = "0.1.0", path = "../rspack_p rspack_plugin_copy = { version = "0.1.0", path = "../rspack_plugin_copy" } rspack_plugin_css = { version = "0.1.0", path = "../rspack_plugin_css" } rspack_plugin_devtool = { version = "0.1.0", path = "../rspack_plugin_devtool" } +rspack_plugin_dll = { version = "0.1.0", path = "../rspack_plugin_dll" } rspack_plugin_dynamic_entry = { version = "0.1.0", path = "../rspack_plugin_dynamic_entry" } rspack_plugin_ensure_chunk_conditions = { version = "0.1.0", path = "../rspack_plugin_ensure_chunk_conditions" } rspack_plugin_entry = { version = "0.1.0", path = "../rspack_plugin_entry" } diff --git a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs index eff25843495..d033507d10f 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs @@ -2,6 +2,7 @@ mod raw_banner; mod raw_bundle_info; mod raw_copy; mod raw_css_extract; +mod raw_dll; mod raw_html; mod raw_ignore; mod raw_lazy_compilation; @@ -34,6 +35,7 @@ use rspack_plugin_devtool::{ SourceMapDevToolModuleOptionsPluginOptions, SourceMapDevToolPlugin, SourceMapDevToolPluginOptions, }; +use rspack_plugin_dll::{DllEntryPlugin, FlagAllModulesAsUsedPlugin, LibManifestPlugin}; use rspack_plugin_dynamic_entry::DynamicEntryPlugin; use rspack_plugin_ensure_chunk_conditions::EnsureChunkConditionsPlugin; use rspack_plugin_entry::EntryPlugin; @@ -77,9 +79,13 @@ use rspack_plugin_web_worker_template::web_worker_template_plugin; use rspack_plugin_worker::WorkerPlugin; pub use self::{ - raw_banner::RawBannerPluginOptions, raw_copy::RawCopyRspackPluginOptions, - raw_html::RawHtmlRspackPluginOptions, raw_ignore::RawIgnorePluginOptions, - raw_limit_chunk_count::RawLimitChunkCountPluginOptions, raw_mf::RawContainerPluginOptions, + raw_banner::RawBannerPluginOptions, + raw_copy::RawCopyRspackPluginOptions, + raw_dll::{RawDllEntyPluginOptions, RawLibManifestPluginOptions}, + raw_html::RawHtmlRspackPluginOptions, + raw_ignore::RawIgnorePluginOptions, + raw_limit_chunk_count::RawLimitChunkCountPluginOptions, + raw_mf::RawContainerPluginOptions, raw_progress::RawProgressPluginOptions, raw_swc_js_minimizer::RawSwcJsMinimizerRspackPluginOptions, }; @@ -164,6 +170,9 @@ pub enum BuiltinPluginName { SizeLimitsPlugin, NoEmitOnErrorsPlugin, ContextReplacementPlugin, + DllEntryPlugin, + LibManifestPlugin, + FlagAllModulesAsUsedPlugin, // rspack specific plugins // naming format follow XxxRspackPlugin @@ -512,6 +521,19 @@ impl BuiltinPlugin { let options = raw_options.try_into()?; plugins.push(ContextReplacementPlugin::new(options).boxed()); } + BuiltinPluginName::DllEntryPlugin => { + let raw_options = downcast_into::(self.options)?; + let options = raw_options.into(); + plugins.push(DllEntryPlugin::new(options).boxed()); + } + BuiltinPluginName::LibManifestPlugin => { + let raw_options = downcast_into::(self.options)?; + let options = raw_options.into(); + plugins.push(LibManifestPlugin::new(options).boxed()); + } + BuiltinPluginName::FlagAllModulesAsUsedPlugin => { + plugins.push(FlagAllModulesAsUsedPlugin::default().boxed()) + } } Ok(()) } diff --git a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs new file mode 100644 index 00000000000..b1a74ffb9d4 --- /dev/null +++ b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs @@ -0,0 +1,60 @@ +use napi_derive::napi; +use rspack_binding_values::JsFilename; +use rspack_plugin_dll::{DllEntryPluginOptions, LibManifestPluginOptions}; + +#[derive(Debug)] +#[napi(object)] +pub struct RawDllEntyPluginOptions { + pub context: String, + pub entries: Vec, + pub name: String, +} + +impl From for DllEntryPluginOptions { + fn from(value: RawDllEntyPluginOptions) -> Self { + let RawDllEntyPluginOptions { + name, + context, + entries, + } = value; + + Self { + name, + context: context.into(), + entries, + } + } +} + +#[derive(Debug)] +#[napi(object, object_to_js = false)] +pub struct RawLibManifestPluginOptions { + pub context: Option, + pub entry_only: Option, + pub name: Option, + pub path: JsFilename, + pub format: Option, + pub ty: Option, +} + +impl From for LibManifestPluginOptions { + fn from(value: RawLibManifestPluginOptions) -> Self { + let RawLibManifestPluginOptions { + context, + entry_only, + name, + path, + ty, + format, + } = value; + + Self { + context: context.map(|c| c.into()), + format, + entry_only, + name: name.map(|n| n.into()), + path: path.into(), + ty, + } + } +} diff --git a/crates/rspack_core/src/dependency/dependency_type.rs b/crates/rspack_core/src/dependency/dependency_type.rs index d0885d384a4..82e45030671 100644 --- a/crates/rspack_core/src/dependency/dependency_type.rs +++ b/crates/rspack_core/src/dependency/dependency_type.rs @@ -101,6 +101,7 @@ pub enum DependencyType { LoaderImport, LazyImport, ModuleDecorator, + DllEntry, Custom(&'static str), } @@ -156,6 +157,7 @@ impl DependencyType { DependencyType::ImportMetaContext => "import.meta context", DependencyType::ContainerExposed => "container exposed", DependencyType::ContainerEntry => "container entry", + DependencyType::DllEntry => "dll entry", DependencyType::RemoteToExternal => "remote to external", DependencyType::RemoteToFallback => "fallback", DependencyType::RemoteToFallbackItem => "fallback item", diff --git a/crates/rspack_core/src/module.rs b/crates/rspack_core/src/module.rs index 410defb58bd..eba5ddc8c89 100644 --- a/crates/rspack_core/src/module.rs +++ b/crates/rspack_core/src/module.rs @@ -15,6 +15,7 @@ use rspack_util::atom::Atom; use rspack_util::ext::{AsAny, DynHash}; use rspack_util::source_map::ModuleSourceMapConfig; use rustc_hash::FxHashSet as HashSet; +use serde::Serialize; use crate::concatenated_module::ConcatenatedModule; use crate::dependencies_block::dependencies_block_update_hash; @@ -78,7 +79,7 @@ impl Default for BuildInfo { } } -#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize)] pub enum BuildMetaExportsType { #[default] Unset, @@ -96,7 +97,7 @@ pub enum ExportsType { Dynamic, } -#[derive(Debug, Default, Clone, Copy, Hash)] +#[derive(Debug, Default, Clone, Copy, Hash, Serialize)] pub enum BuildMetaDefaultObject { #[default] False, @@ -110,7 +111,7 @@ pub enum BuildMetaDefaultObject { }, } -#[derive(Debug, Default, Clone, Copy, Hash)] +#[derive(Debug, Default, Clone, Copy, Hash, Serialize)] pub enum ModuleArgument { #[default] Module, @@ -126,7 +127,7 @@ impl Display for ModuleArgument { } } -#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize)] pub enum ExportsArgument { #[default] Exports, @@ -142,7 +143,8 @@ impl Display for ExportsArgument { } } -#[derive(Debug, Default, Clone, Hash)] +#[derive(Debug, Default, Clone, Hash, Serialize)] +#[serde(rename_all = "camelCase")] pub struct BuildMeta { pub strict_esm_module: bool, pub has_top_level_await: bool, diff --git a/crates/rspack_plugin_dll/Cargo.toml b/crates/rspack_plugin_dll/Cargo.toml new file mode 100644 index 00000000000..983bcb74ddd --- /dev/null +++ b/crates/rspack_plugin_dll/Cargo.toml @@ -0,0 +1,24 @@ +[package] +description = "rspack dynamic entry plugin" +edition = "2021" +homepage.workspace = true +license = "MIT" +name = "rspack_plugin_dll" +repository = "https://github.com/web-infra-dev/rspack" +version = "0.1.0" + +[dependencies] +rspack_collections = { version = "0.1.0", path = "../rspack_collections" } +rspack_core = { version = "0.1.0", path = "../rspack_core" } +rspack_error = { version = "0.1.0", path = "../rspack_error" } +rspack_hook = { version = "0.1.0", path = "../rspack_hook" } +rspack_macros = { version = "0.1.0", path = "../rspack_macros" } +rspack_util = { version = "0.1.0", path = "../rspack_util" } + +async-trait = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +tracing = { workspace = true } + +[lints] +workspace = true diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs new file mode 100644 index 00000000000..b28bf2c5eb5 --- /dev/null +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs @@ -0,0 +1,55 @@ +use rspack_core::{ + AffectType, AsContextDependency, AsDependencyTemplate, AsModuleDependency, Context, Dependency, + DependencyId, DependencyType, +}; + +use crate::DllEntryPluginOptions; + +#[derive(Debug, Clone)] +pub struct DllEntryDependency { + pub context: Context, + + pub entries: Vec, + + // TODO: pass by serialize & deserialize. + pub name: String, + + id: DependencyId, +} + +impl DllEntryDependency { + pub fn new(dll_entry_plugin_optinos: &DllEntryPluginOptions) -> Self { + let DllEntryPluginOptions { + context, + entries, + name, + } = dll_entry_plugin_optinos.clone(); + + Self { + context, + entries, + name, + id: DependencyId::new(), + } + } +} + +impl Dependency for DllEntryDependency { + fn id(&self) -> &DependencyId { + &self.id + } + + fn could_affect_referencing_module(&self) -> AffectType { + AffectType::Transitive + } + + fn dependency_type(&self) -> &DependencyType { + &DependencyType::DllEntry + } +} + +impl AsModuleDependency for DllEntryDependency {} + +impl AsContextDependency for DllEntryDependency {} + +impl AsDependencyTemplate for DllEntryDependency {} diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_plugin.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_plugin.rs new file mode 100644 index 00000000000..4fe2e7bd3ae --- /dev/null +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_plugin.rs @@ -0,0 +1,81 @@ +use std::sync::Arc; + +use rspack_core::{ + ApplyContext, Compilation, CompilationParams, CompilerCompilation, CompilerMake, CompilerOptions, + Context, DependencyType, EntryOptions, Plugin, PluginContext, +}; +use rspack_error::Result; +use rspack_hook::{plugin, plugin_hook}; + +use super::{dll_entry_dependency::DllEntryDependency, dll_module_factory}; + +#[derive(Debug, Clone, Default)] +pub struct DllEntryPluginOptions { + pub name: String, + + pub context: Context, + + pub entries: Vec, +} + +#[plugin] +#[derive(Debug)] +pub struct DllEntryPlugin { + options: DllEntryPluginOptions, +} + +impl DllEntryPlugin { + pub fn new(options: DllEntryPluginOptions) -> Self { + Self::new_inner(options) + } +} + +#[async_trait::async_trait] +impl Plugin for DllEntryPlugin { + fn name(&self) -> &'static str { + "rspack.DllEntryPlugin" + } + + fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> { + ctx + .context + .compiler_hooks + .compilation + .tap(compilation::new(self)); + + ctx.context.compiler_hooks.make.tap(make::new(self)); + + Ok(()) + } +} + +#[plugin_hook(CompilerCompilation for DllEntryPlugin)] +async fn compilation( + &self, + compilation: &mut Compilation, + params: &mut CompilationParams, +) -> Result<()> { + compilation.set_dependency_factory( + DependencyType::DllEntry, + Arc::new(dll_module_factory::DllModuleFactory), + ); + + compilation.set_dependency_factory(DependencyType::Entry, params.normal_module_factory.clone()); + + Ok(()) +} + +#[plugin_hook(CompilerMake for DllEntryPlugin)] +async fn make(&self, compilation: &mut Compilation) -> Result<()> { + compilation + .add_entry( + Box::new(DllEntryDependency::new(&self.options)), + EntryOptions { + name: Some(self.options.name.clone()), + ..Default::default() + }, + ) + .await?; + + Ok(()) +} diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs new file mode 100644 index 00000000000..90c995aa505 --- /dev/null +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs @@ -0,0 +1,166 @@ +use std::{borrow::Cow, hash::Hash, sync::Arc}; + +use async_trait::async_trait; +use rspack_collections::{Identifiable, Identifier}; +use rspack_core::{ + impl_module_meta_info, impl_source_map_config, module_update_hash, rspack_sources::RawSource, + rspack_sources::Source, AsyncDependenciesBlockIdentifier, BuildContext, BuildInfo, BuildMeta, + BuildResult, CodeGenerationResult, Compilation, ConcatenationScope, Context, DependenciesBlock, + Dependency, DependencyId, EntryDependency, FactoryMeta, Module, ModuleType, RuntimeGlobals, + RuntimeSpec, SourceType, +}; +use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result}; + +use super::dll_entry_dependency::DllEntryDependency; + +#[impl_source_map_config] +#[derive(Debug, Default)] +pub struct DllModule { + // TODO: it should be set to EntryDependency.loc + name: String, + + factory_meta: Option, + + build_info: Option, + + build_meta: Option, + + blocks: Vec, + + entries: Vec, + + context: Context, + + dependencies: Vec, +} + +impl DllModule { + pub fn new(dep: &DllEntryDependency) -> Self { + let DllEntryDependency { + entries, + context, + name, + .. + } = dep.clone(); + + Self { + name, + entries, + context, + ..Default::default() + } + } +} + +#[async_trait] +impl Module for DllModule { + impl_module_meta_info!(); + + fn module_type(&self) -> &ModuleType { + &ModuleType::JsDynamic + } + + fn source_types(&self) -> &[SourceType] { + &[SourceType::JavaScript] + } + + fn get_diagnostics(&self) -> Vec { + vec![] + } + + fn original_source(&self) -> Option<&dyn Source> { + None + } + + fn readable_identifier(&self, _context: &Context) -> Cow { + self.identifier().as_str().into() + } + + async fn build( + &mut self, + _build_context: BuildContext<'_>, + _compilation: Option<&Compilation>, + ) -> Result { + let dependencies = self + .entries + .clone() + .into_iter() + .map(|entry| EntryDependency::new(entry, self.context.clone(), None, false)) + .map(|dependency| Box::new(dependency) as Box) + .collect::>(); + + Ok(BuildResult { + dependencies, + ..Default::default() + }) + } + + fn code_generation( + &self, + _compilation: &Compilation, + _runtime: Option<&RuntimeSpec>, + _concatenation_scope: Option, + ) -> Result { + let mut runtime_requirements = RuntimeGlobals::default(); + runtime_requirements.insert(RuntimeGlobals::REQUIRE); + runtime_requirements.insert(RuntimeGlobals::MODULE); + + let mut code_generation_result = CodeGenerationResult { + runtime_requirements, + ..Default::default() + }; + + code_generation_result = code_generation_result.with_javascript(Arc::new(RawSource::from( + format!("module.exports = {}", RuntimeGlobals::REQUIRE.name()), + ))); + + Ok(code_generation_result) + } + + fn need_build(&self) -> bool { + self.build_meta.is_none() + } + + fn size(&self, _source_type: Option<&SourceType>, _compilation: &Compilation) -> f64 { + 12.0 + } + + fn update_hash( + &self, + mut hasher: &mut dyn std::hash::Hasher, + compilation: &Compilation, + runtime: Option<&RuntimeSpec>, + ) -> Result<()> { + format!("dll module {}", self.name).hash(&mut hasher); + + module_update_hash(self, hasher, compilation, runtime); + + Ok(()) + } +} + +impl Identifiable for DllModule { + fn identifier(&self) -> Identifier { + format!("dll {}", self.name).as_str().into() + } +} + +impl DependenciesBlock for DllModule { + fn add_block_id(&mut self, block: AsyncDependenciesBlockIdentifier) { + self.blocks.push(block); + } + + fn get_blocks(&self) -> &[AsyncDependenciesBlockIdentifier] { + &self.blocks + } + + fn add_dependency_id(&mut self, dependency: DependencyId) { + self.dependencies.push(dependency); + } + + fn get_dependencies(&self) -> &[DependencyId] { + &self.dependencies + } +} + +impl_empty_diagnosable_trait!(DllModule); diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_module_factory.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_module_factory.rs new file mode 100644 index 00000000000..57481504334 --- /dev/null +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_module_factory.rs @@ -0,0 +1,22 @@ +use async_trait::async_trait; +use rspack_core::{ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult}; +use rspack_error::Result; + +use super::{dll_entry_dependency::DllEntryDependency, dll_module::DllModule}; + +#[derive(Debug)] +pub(crate) struct DllModuleFactory; + +#[async_trait] +impl ModuleFactory for DllModuleFactory { + async fn create(&self, data: &mut ModuleFactoryCreateData) -> Result { + let dll_entry_dependency = data.dependencies[0] + .as_any() + .downcast_ref::() + .expect("unreachable"); + + Ok(ModuleFactoryResult { + module: Some(Box::new(DllModule::new(dll_entry_dependency))), + }) + } +} diff --git a/crates/rspack_plugin_dll/src/dll_entry/mod.rs b/crates/rspack_plugin_dll/src/dll_entry/mod.rs new file mode 100644 index 00000000000..2e84fff6ec9 --- /dev/null +++ b/crates/rspack_plugin_dll/src/dll_entry/mod.rs @@ -0,0 +1,4 @@ +pub mod dll_entry_dependency; +pub mod dll_entry_plugin; +pub mod dll_module; +pub mod dll_module_factory; diff --git a/crates/rspack_plugin_dll/src/flag_alll_modules_as_used_plugin.rs b/crates/rspack_plugin_dll/src/flag_alll_modules_as_used_plugin.rs new file mode 100644 index 00000000000..5e5c3d0c443 --- /dev/null +++ b/crates/rspack_plugin_dll/src/flag_alll_modules_as_used_plugin.rs @@ -0,0 +1,54 @@ +use rspack_collections::IdentifierSet; +use rspack_core::{ + get_entry_runtime, merge_runtime, ApplyContext, Compilation, CompilationOptimizeDependencies, + CompilerOptions, FactoryMeta, Plugin, PluginContext, RuntimeSpec, +}; +use rspack_error::Result; +use rspack_hook::{plugin, plugin_hook}; + +#[plugin] +#[derive(Debug, Default)] +pub struct FlagAllModulesAsUsedPlugin; + +impl Plugin for FlagAllModulesAsUsedPlugin { + fn name(&self) -> &'static str { + "rspack:FlagAllModulesAsUsedPlugin" + } + + fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> { + ctx + .context + .compilation_hooks + .optimize_dependencies + .tap(optimze_dependencies::new(self)); + + Ok(()) + } +} + +#[plugin_hook(CompilationOptimizeDependencies for FlagAllModulesAsUsedPlugin)] +fn optimze_dependencies(&self, compilation: &mut Compilation) -> Result> { + let entries = &compilation.entries; + + let runtime = compilation + .entries + .iter() + .map(|(name, entry_data)| get_entry_runtime(name, &entry_data.options, entries)) + .fold(RuntimeSpec::default(), |a, b| merge_runtime(&a, &b)); + + let mut mg = compilation.get_module_graph_mut(); + let modules_id: IdentifierSet = mg.modules().keys().cloned().collect(); + + for module_id in modules_id { + let exports_info = mg.get_exports_info(&module_id); + exports_info.set_used_in_unknown_way(&mut mg, Some(&runtime)); + // TODO: module_graph add extra reason + if let Some(module) = mg.module_by_identifier_mut(&module_id) { + module.set_factory_meta(FactoryMeta { + side_effect_free: Some(false), + }) + }; + } + + Ok(None) +} diff --git a/crates/rspack_plugin_dll/src/lib.rs b/crates/rspack_plugin_dll/src/lib.rs new file mode 100644 index 00000000000..83d70eb19fd --- /dev/null +++ b/crates/rspack_plugin_dll/src/lib.rs @@ -0,0 +1,7 @@ +mod dll_entry; +mod flag_alll_modules_as_used_plugin; +mod lib_manifest_plugin; + +pub use dll_entry::dll_entry_plugin::{DllEntryPlugin, DllEntryPluginOptions}; +pub use flag_alll_modules_as_used_plugin::FlagAllModulesAsUsedPlugin; +pub use lib_manifest_plugin::{LibManifestPlugin, LibManifestPluginOptions}; diff --git a/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs b/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs new file mode 100644 index 00000000000..73c2ae5815a --- /dev/null +++ b/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs @@ -0,0 +1,218 @@ +use std::borrow::Cow; +use std::collections::{HashMap, HashSet}; +use std::sync::Arc; + +use rspack_core::{ + rspack_sources::{BoxSource, RawSource}, + ApplyContext, BuildMeta, Compilation, CompilerEmit, CompilerOptions, Context, EntryDependency, + Filename, LibIdentOptions, PathData, Plugin, PluginContext, ProvidedExports, SourceType, +}; +use rspack_error::{Error, Result}; +use rspack_hook::{plugin, plugin_hook}; +use rspack_util::atom::Atom; +use serde::Serialize; + +#[derive(Debug, Clone)] +pub struct LibManifestPluginOptions { + pub context: Option, + + pub entry_only: Option, + + pub name: Option, + + pub format: Option, + + pub path: Filename, + + pub ty: Option, +} + +#[plugin] +#[derive(Debug)] +pub struct LibManifestPlugin { + options: LibManifestPluginOptions, +} + +impl LibManifestPlugin { + pub fn new(options: LibManifestPluginOptions) -> Self { + Self::new_inner(options) + } +} + +impl Plugin for LibManifestPlugin { + fn name(&self) -> &'static str { + "rspack.LibManifestPlugin" + } + + fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> { + ctx.context.compiler_hooks.emit.tap(emit::new(self)); + Ok(()) + } +} + +#[plugin_hook(CompilerEmit for LibManifestPlugin)] +async fn emit(&self, compilation: &mut Compilation) -> Result<()> { + let mut use_paths: HashSet = HashSet::new(); + + let chunk_graph = &compilation.chunk_graph; + + let mut manifest_files = vec![]; + + let module_graph = compilation.get_module_graph(); + + for (_, chunk) in compilation.chunk_by_ukey.iter() { + if !chunk.can_be_initial(&compilation.chunk_group_by_ukey) { + return Ok(()); + } + + let target_path = compilation.get_path( + &self.options.path, + PathData { + chunk: Some(chunk), + ..Default::default() + }, + )?; + + if use_paths.get(&target_path).is_some() { + return Err(Error::msg("each chunk must have a unique path")); + } + + use_paths.insert(target_path.clone()); + + let name = self.options.name.as_ref().and_then(|filename| { + compilation + .get_path( + filename, + PathData { + chunk: Some(chunk), + content_hash_type: Some(SourceType::JavaScript), + ..Default::default() + }, + ) + .ok() + }); + + let mut manifest_contents = HashMap::, ManifestContent>::new(); + + for module in chunk_graph.get_ordered_chunk_modules(&chunk.ukey, &module_graph) { + if self.options.entry_only.unwrap_or_default() + && !some_in_iterable( + module_graph + .get_incoming_connections(&module.identifier()) + .into_iter(), + |conn| { + let dep = module_graph.dependency_by_id(&conn.dependency_id); + + dep + .map(|dep| dep.is::()) + .unwrap_or_default() + }, + ) + { + continue; + } + + let context = match &self.options.context { + Some(ctx) => ctx, + None => &compilation.options.context, + }; + + let ident = module.lib_ident(LibIdentOptions { context }); + + if let Some(ident) = ident { + let exports_info = module_graph.get_exports_info(&module.identifier()); + + let provided_exports = match exports_info.get_provided_exports(&module_graph) { + ProvidedExports::Vec(vec) => Some(vec), + _ => None, + }; + + let id = chunk_graph.get_module_id(module.identifier()); + + let build_meta = module.build_meta(); + + manifest_contents.insert( + ident, + ManifestContent { + id, + build_meta, + provided_exports, + }, + ); + } + } + + let manifest = Manifest { + name: name.as_deref(), + content: manifest_contents, + ty: self.options.ty.clone(), + }; + + let format = self.options.format.unwrap_or_default(); + + let manifest_json = if format { + serde_json::to_string_pretty(&manifest).map_err(|e| Error::msg(format!("{}", e)))? + } else { + serde_json::to_string(&manifest).map_err(|e| Error::msg(format!("{}", e)))? + }; + + manifest_files.push(ManifestFile { + filename: target_path.clone(), + content: manifest_json.clone(), + }); + } + + for file in manifest_files { + let filename = file.filename; + let manifest_content = file.content; + let manifest_asset = Arc::new(RawSource::from(manifest_content)) as BoxSource; + + compilation.emit_asset(filename, manifest_asset.into()); + } + + Ok(()) +} + +#[derive(Serialize, Debug)] +#[serde(rename_all = "camelCase")] +struct ManifestContent<'i> { + #[serde(skip_serializing_if = "Option::is_none")] + id: Option<&'i str>, + + #[serde(skip_serializing_if = "Option::is_none")] + build_meta: Option<&'i BuildMeta>, + + #[serde(skip_serializing_if = "Option::is_none")] + provided_exports: Option>, +} + +#[derive(Serialize, Debug)] +struct Manifest<'i> { + #[serde(skip_serializing_if = "Option::is_none")] + name: Option<&'i str>, + + content: HashMap, ManifestContent<'i>>, + + #[serde(rename = "type")] + #[serde(skip_serializing_if = "Option::is_none")] + ty: Option, +} + +#[derive(Debug)] +struct ManifestFile { + filename: String, + + content: String, +} + +fn some_in_iterable(iterable: I, filter: F) -> bool +where + F: Fn(I::Item) -> bool, +{ + for item in iterable { + if filter(item) { + return true; + } + } + return false; +} diff --git a/packages/rspack/etc/core.api.md b/packages/rspack/etc/core.api.md index 5cb06ff5aad..931652de0d8 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -1377,6 +1377,23 @@ class DirectoryWatcher extends EventEmitter { }; } +// @public (undocumented) +export class DllPlugin { + constructor(options: DllPluginOptions); + // (undocumented) + apply(compiler: Compiler): void; +} + +// @public (undocumented) +type DllPluginOptions = { + context?: string; + entryOnly?: boolean; + format?: boolean; + name?: string; + path: string; + type?: string; +}; + // @public (undocumented) interface Drafts { customMedia?: boolean; @@ -4434,6 +4451,7 @@ declare namespace rspackExports { ExternalsPlugin, HotModuleReplacementPlugin, NoEmitOnErrorsPlugin, + DllPlugin, EnvironmentPlugin, LoaderOptionsPlugin, LoaderTargetPlugin, diff --git a/packages/rspack/src/builtin-plugin/DllEntryPlugin.ts b/packages/rspack/src/builtin-plugin/DllEntryPlugin.ts new file mode 100644 index 00000000000..c3f7532f039 --- /dev/null +++ b/packages/rspack/src/builtin-plugin/DllEntryPlugin.ts @@ -0,0 +1,24 @@ +import { + BuiltinPluginName, + type RawDllEntyPluginOptions +} from "@rspack/binding"; +import { create } from "./base"; + +export type DllEntryPluginOptions = { + name: string; +}; + +export const DllEntryPlugin = create( + BuiltinPluginName.DllEntryPlugin, + ( + context: string, + entries: string[], + options: DllEntryPluginOptions + ): RawDllEntyPluginOptions => { + return { + context, + entries, + name: options.name + }; + } +); diff --git a/packages/rspack/src/builtin-plugin/FlagAllModulesAsUsedPlugin.ts b/packages/rspack/src/builtin-plugin/FlagAllModulesAsUsedPlugin.ts new file mode 100644 index 00000000000..d4f6f2a0f8a --- /dev/null +++ b/packages/rspack/src/builtin-plugin/FlagAllModulesAsUsedPlugin.ts @@ -0,0 +1,7 @@ +import { BuiltinPluginName } from "@rspack/binding"; +import { create } from "./base"; + +export const FlagAllModulesAsUsedPlugin = create( + BuiltinPluginName.FlagAllModulesAsUsedPlugin, + (): undefined => {} +); diff --git a/packages/rspack/src/builtin-plugin/LibManifestPlugin.ts b/packages/rspack/src/builtin-plugin/LibManifestPlugin.ts new file mode 100644 index 00000000000..2453356e394 --- /dev/null +++ b/packages/rspack/src/builtin-plugin/LibManifestPlugin.ts @@ -0,0 +1,35 @@ +import { + BuiltinPluginName, + type RawLibManifestPluginOptions +} from "@rspack/binding"; +import { create } from "./base"; + +export type LibManifestPluginOptions = { + context?: string; + + entryOnly?: boolean; + + format?: boolean; + + name?: string; + + path: string; + + type?: string; +}; + +export const LibManifestPlugin = create( + BuiltinPluginName.LibManifestPlugin, + (options: LibManifestPluginOptions): RawLibManifestPluginOptions => { + const { context, entryOnly, format, name, path, type } = options; + + return { + context, + entryOnly, + format, + name, + path, + ty: type + }; + } +); diff --git a/packages/rspack/src/builtin-plugin/index.ts b/packages/rspack/src/builtin-plugin/index.ts index 25182ec9b0f..43a3cd7c38f 100644 --- a/packages/rspack/src/builtin-plugin/index.ts +++ b/packages/rspack/src/builtin-plugin/index.ts @@ -65,3 +65,5 @@ export * from "./WorkerPlugin"; export * from "./FetchCompileAsyncWasmPlugin"; export * from "./NoEmitOnErrorsPlugin"; export * from "./ContextReplacementPlugin"; +export * from "./LibManifestPlugin"; +export * from "./DllEntryPlugin"; diff --git a/packages/rspack/src/exports.ts b/packages/rspack/src/exports.ts index df45a1df66c..985aad0083a 100644 --- a/packages/rspack/src/exports.ts +++ b/packages/rspack/src/exports.ts @@ -103,6 +103,7 @@ export { DynamicEntryPlugin } from "./builtin-plugin"; export { ExternalsPlugin } from "./builtin-plugin"; export { HotModuleReplacementPlugin } from "./builtin-plugin"; export { NoEmitOnErrorsPlugin } from "./builtin-plugin"; +export { DllPlugin } from "./lib/DllPlugin"; export { EnvironmentPlugin } from "./lib/EnvironmentPlugin"; export { LoaderOptionsPlugin } from "./lib/LoaderOptionsPlugin"; export { LoaderTargetPlugin } from "./lib/LoaderTargetPlugin"; diff --git a/packages/rspack/src/lib/DllPlugin.ts b/packages/rspack/src/lib/DllPlugin.ts new file mode 100644 index 00000000000..6a158a3a991 --- /dev/null +++ b/packages/rspack/src/lib/DllPlugin.ts @@ -0,0 +1,94 @@ +/** + * The following code is modified based on + * https://github.com/webpack/webpack/blob/4b4ca3bb53f36a5b8fc6bc1bd976ed7af161bd80/lib/DllPlugin.js + * + * MIT Licensed + * Author Tobias Koppers @sokra + * Copyright (c) JS Foundation and other contributors + * https://github.com/webpack/webpack/blob/main/LICENSE + */ + +import z from "zod"; +import type { Compiler } from "../Compiler"; +import { LibManifestPlugin } from "../builtin-plugin"; +import { DllEntryPlugin } from "../builtin-plugin/DllEntryPlugin"; +import { FlagAllModulesAsUsedPlugin } from "../builtin-plugin/FlagAllModulesAsUsedPlugin"; +import { validate } from "../util/validate"; + +export type DllPluginOptions = { + /** + * Context of requests in the manifest file (defaults to the webpack context). + */ + context?: string; + + /** + * If true, only entry points will be exposed (default: true). + */ + entryOnly?: boolean; + + /** + * If true, manifest json file (output) will be formatted. + */ + format?: boolean; + + /** + * Name of the exposed dll function (external name, use value of 'output.library'). + */ + name?: string; + + /** + * Absolute path to the manifest json file (output). + */ + path: string; + + /** + * Type of the dll bundle (external type, use value of 'output.libraryTarget'). + */ + type?: string; +}; + +const dllPluginOptions = z.object({ + context: z.string().optional(), + entryOnly: z.boolean().optional(), + format: z.boolean().optional(), + name: z.string().optional(), + path: z.string(), + type: z.string().optional() +}) satisfies z.ZodType; + +export class DllPlugin { + private options: DllPluginOptions; + + constructor(options: DllPluginOptions) { + validate(options, dllPluginOptions); + this.options = { + ...options, + entryOnly: options.entryOnly !== false + }; + } + + apply(compiler: Compiler) { + compiler.hooks.entryOption.tap("DllPlugin", (context, entry) => { + if (typeof entry === "function") { + throw new Error( + "DllPlugin doesn't support dynamic entry (function) yet" + ); + } + + for (const name of Object.keys(entry)) { + const options = { + name + }; + const entries = entry[name].import || []; + + new DllEntryPlugin(context, entries, options).apply(compiler); + } + }); + + new LibManifestPlugin(this.options).apply(compiler); + + if (!this.options.entryOnly) { + new FlagAllModulesAsUsedPlugin().apply(compiler); + } + } +} From 61a83866c774dc6e8cdd36d495a81376f3a80265 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Thu, 31 Oct 2024 17:23:09 +0800 Subject: [PATCH 02/22] feat: support rspack.DllReferencePlugin --- Cargo.lock | 2 + crates/node_binding/binding.d.ts | 21 +- .../src/options/raw_builtins/mod.rs | 11 +- .../src/options/raw_builtins/raw_dll.rs | 49 +++- .../src/dependency/dependency_type.rs | 2 + crates/rspack_core/src/module.rs | 14 +- crates/rspack_plugin_dll/Cargo.toml | 14 +- crates/rspack_plugin_dll/LICENSE | 22 ++ .../src/dll_entry/dll_entry_dependency.rs | 16 +- .../src/dll_reference/delegated_module.rs | 235 ++++++++++++++++ .../src/dll_reference/delegated_plugin.rs | 156 +++++++++++ .../delegated_source_dependency.rs | 51 ++++ .../dll_reference_agency_plugin.rs | 99 +++++++ .../src/dll_reference/mod.rs | 4 + .../src/flag_alll_modules_as_used_plugin.rs | 23 +- crates/rspack_plugin_dll/src/lib.rs | 35 +++ .../src/lib_manifest_plugin.rs | 100 +++---- packages/rspack/etc/core.api.md | 55 +++- .../DllReferenceAgencyPlugin.ts | 15 ++ .../src/builtin-plugin/LibManifestPlugin.ts | 2 +- packages/rspack/src/builtin-plugin/index.ts | 1 + packages/rspack/src/exports.ts | 9 +- packages/rspack/src/lib/DllPlugin.ts | 2 + packages/rspack/src/lib/DllReferencePlugin.ts | 255 ++++++++++++++++++ 24 files changed, 1085 insertions(+), 108 deletions(-) create mode 100644 crates/rspack_plugin_dll/LICENSE create mode 100644 crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs create mode 100644 crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs create mode 100644 crates/rspack_plugin_dll/src/dll_reference/delegated_source_dependency.rs create mode 100644 crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs create mode 100644 crates/rspack_plugin_dll/src/dll_reference/mod.rs create mode 100644 packages/rspack/src/builtin-plugin/DllReferenceAgencyPlugin.ts create mode 100644 packages/rspack/src/lib/DllReferencePlugin.ts diff --git a/Cargo.lock b/Cargo.lock index 9384e7cec03..291d976312e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4098,7 +4098,9 @@ dependencies = [ "rspack_error", "rspack_hook", "rspack_macros", + "rspack_plugin_externals", "rspack_util", + "rustc-hash 1.1.0", "serde", "serde_json", "tracing", diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index c469aeaf0af..6ebed12fa39 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -302,6 +302,7 @@ export enum BuiltinPluginName { NoEmitOnErrorsPlugin = 'NoEmitOnErrorsPlugin', ContextReplacementPlugin = 'ContextReplacementPlugin', DllEntryPlugin = 'DllEntryPlugin', + DllReferenceAgencyPlugin = 'DllReferenceAgencyPlugin', LibManifestPlugin = 'LibManifestPlugin', FlagAllModulesAsUsedPlugin = 'FlagAllModulesAsUsedPlugin', HttpExternalsRspackPlugin = 'HttpExternalsRspackPlugin', @@ -1037,13 +1038,6 @@ export interface JsTap { stage: number } -export interface NodeFS { - writeFile: (...args: any[]) => any - removeFile: (...args: any[]) => any - mkdir: (...args: any[]) => any - mkdirp: (...args: any[]) => any -} - export interface PathWithInfo { path: string info: JsAssetInfo @@ -1279,6 +1273,17 @@ export interface RawDllEntyPluginOptions { name: string } +export interface RawDllReferencePluginAgencyOptions { + context?: string + name?: string + extensions: Array + scope?: string + sourceType?: string + type: string + content?: string + manifest?: string +} + export interface RawDraft { customMedia: boolean } @@ -1489,7 +1494,7 @@ export interface RawLibManifestPluginOptions { name?: JsFilename path: JsFilename format?: boolean - ty?: string + type?: string } export interface RawLightningCssBrowsers { diff --git a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs index d033507d10f..b5444085133 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs @@ -16,6 +16,7 @@ mod raw_swc_js_minimizer; use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown}; use napi_derive::napi; +use raw_dll::RawDllReferencePluginAgencyOptions; use raw_lightning_css_minimizer::RawLightningCssMinimizerRspackPluginOptions; use rspack_binding_values::entry::JsEntryPluginOptions; use rspack_core::{BoxPlugin, Plugin, PluginExt}; @@ -35,7 +36,9 @@ use rspack_plugin_devtool::{ SourceMapDevToolModuleOptionsPluginOptions, SourceMapDevToolPlugin, SourceMapDevToolPluginOptions, }; -use rspack_plugin_dll::{DllEntryPlugin, FlagAllModulesAsUsedPlugin, LibManifestPlugin}; +use rspack_plugin_dll::{ + DllEntryPlugin, DllReferenceAgencyPlugin, FlagAllModulesAsUsedPlugin, LibManifestPlugin, +}; use rspack_plugin_dynamic_entry::DynamicEntryPlugin; use rspack_plugin_ensure_chunk_conditions::EnsureChunkConditionsPlugin; use rspack_plugin_entry::EntryPlugin; @@ -171,6 +174,7 @@ pub enum BuiltinPluginName { NoEmitOnErrorsPlugin, ContextReplacementPlugin, DllEntryPlugin, + DllReferenceAgencyPlugin, LibManifestPlugin, FlagAllModulesAsUsedPlugin, @@ -534,6 +538,11 @@ impl BuiltinPlugin { BuiltinPluginName::FlagAllModulesAsUsedPlugin => { plugins.push(FlagAllModulesAsUsedPlugin::default().boxed()) } + BuiltinPluginName::DllReferenceAgencyPlugin => { + let raw_options = downcast_into::(self.options)?; + let options = raw_options.into(); + plugins.push(DllReferenceAgencyPlugin::new(options).boxed()); + } } Ok(()) } diff --git a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs index b1a74ffb9d4..a159b15ed76 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs @@ -1,6 +1,8 @@ use napi_derive::napi; use rspack_binding_values::JsFilename; -use rspack_plugin_dll::{DllEntryPluginOptions, LibManifestPluginOptions}; +use rspack_plugin_dll::{ + DllEntryPluginOptions, DllReferencePluginAgencyOptions, LibManifestPluginOptions, +}; #[derive(Debug)] #[napi(object)] @@ -34,7 +36,7 @@ pub struct RawLibManifestPluginOptions { pub name: Option, pub path: JsFilename, pub format: Option, - pub ty: Option, + pub r#type: Option, } impl From for LibManifestPluginOptions { @@ -44,7 +46,7 @@ impl From for LibManifestPluginOptions { entry_only, name, path, - ty, + r#type, format, } = value; @@ -54,7 +56,46 @@ impl From for LibManifestPluginOptions { entry_only, name: name.map(|n| n.into()), path: path.into(), - ty, + r#type, + } + } +} + +#[derive(Debug)] +#[napi(object)] +pub struct RawDllReferencePluginAgencyOptions { + pub context: Option, + pub name: Option, + pub extensions: Vec, + pub scope: Option, + pub source_type: Option, + pub r#type: String, + pub content: Option, + pub manifest: Option, +} + +impl From for DllReferencePluginAgencyOptions { + fn from(value: RawDllReferencePluginAgencyOptions) -> Self { + let RawDllReferencePluginAgencyOptions { + context, + name, + extensions, + scope, + source_type, + r#type, + content, + manifest, + } = value; + + Self { + context: context.map(|ctx| ctx.into()), + name, + extensions, + scope, + source_type, + r#type, + content, + manifest, } } } diff --git a/crates/rspack_core/src/dependency/dependency_type.rs b/crates/rspack_core/src/dependency/dependency_type.rs index 82e45030671..667ec12f71a 100644 --- a/crates/rspack_core/src/dependency/dependency_type.rs +++ b/crates/rspack_core/src/dependency/dependency_type.rs @@ -102,6 +102,7 @@ pub enum DependencyType { LazyImport, ModuleDecorator, DllEntry, + DelegatedSource, Custom(&'static str), } @@ -168,6 +169,7 @@ impl DependencyType { DependencyType::WebpackIsIncluded => "__webpack_is_included__", DependencyType::LazyImport => "lazy import()", DependencyType::ModuleDecorator => "module decorator", + DependencyType::DelegatedSource => "delegated source", } } } diff --git a/crates/rspack_core/src/module.rs b/crates/rspack_core/src/module.rs index eba5ddc8c89..d4868a9800b 100644 --- a/crates/rspack_core/src/module.rs +++ b/crates/rspack_core/src/module.rs @@ -15,7 +15,7 @@ use rspack_util::atom::Atom; use rspack_util::ext::{AsAny, DynHash}; use rspack_util::source_map::ModuleSourceMapConfig; use rustc_hash::FxHashSet as HashSet; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use crate::concatenated_module::ConcatenatedModule; use crate::dependencies_block::dependencies_block_update_hash; @@ -79,7 +79,7 @@ impl Default for BuildInfo { } } -#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize)] +#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)] pub enum BuildMetaExportsType { #[default] Unset, @@ -89,7 +89,7 @@ pub enum BuildMetaExportsType { Dynamic, } -#[derive(Debug, Clone, Copy, Hash)] +#[derive(Debug, Clone, Copy, Hash, Deserialize)] pub enum ExportsType { DefaultOnly, Namespace, @@ -97,7 +97,7 @@ pub enum ExportsType { Dynamic, } -#[derive(Debug, Default, Clone, Copy, Hash, Serialize)] +#[derive(Debug, Default, Clone, Copy, Hash, Serialize, Deserialize)] pub enum BuildMetaDefaultObject { #[default] False, @@ -111,7 +111,7 @@ pub enum BuildMetaDefaultObject { }, } -#[derive(Debug, Default, Clone, Copy, Hash, Serialize)] +#[derive(Debug, Default, Clone, Copy, Hash, Serialize, Deserialize)] pub enum ModuleArgument { #[default] Module, @@ -127,7 +127,7 @@ impl Display for ModuleArgument { } } -#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize)] +#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)] pub enum ExportsArgument { #[default] Exports, @@ -143,7 +143,7 @@ impl Display for ExportsArgument { } } -#[derive(Debug, Default, Clone, Hash, Serialize)] +#[derive(Debug, Default, Clone, Hash, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct BuildMeta { pub strict_esm_module: bool, diff --git a/crates/rspack_plugin_dll/Cargo.toml b/crates/rspack_plugin_dll/Cargo.toml index 983bcb74ddd..5b7cd82466b 100644 --- a/crates/rspack_plugin_dll/Cargo.toml +++ b/crates/rspack_plugin_dll/Cargo.toml @@ -8,14 +8,16 @@ repository = "https://github.com/web-infra-dev/rspack" version = "0.1.0" [dependencies] -rspack_collections = { version = "0.1.0", path = "../rspack_collections" } -rspack_core = { version = "0.1.0", path = "../rspack_core" } -rspack_error = { version = "0.1.0", path = "../rspack_error" } -rspack_hook = { version = "0.1.0", path = "../rspack_hook" } -rspack_macros = { version = "0.1.0", path = "../rspack_macros" } -rspack_util = { version = "0.1.0", path = "../rspack_util" } +rspack_collections = { version = "0.1.0", path = "../rspack_collections" } +rspack_core = { version = "0.1.0", path = "../rspack_core" } +rspack_error = { version = "0.1.0", path = "../rspack_error" } +rspack_hook = { version = "0.1.0", path = "../rspack_hook" } +rspack_macros = { version = "0.1.0", path = "../rspack_macros" } +rspack_plugin_externals = { version = "0.1.0", path = "../rspack_plugin_externals" } +rspack_util = { version = "0.1.0", path = "../rspack_util" } async-trait = { workspace = true } +rustc-hash = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } tracing = { workspace = true } diff --git a/crates/rspack_plugin_dll/LICENSE b/crates/rspack_plugin_dll/LICENSE new file mode 100644 index 00000000000..46310101ad8 --- /dev/null +++ b/crates/rspack_plugin_dll/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2022-present Bytedance, Inc. and its affiliates. + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs index b28bf2c5eb5..3d8dbf94a6e 100644 --- a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs @@ -1,6 +1,6 @@ use rspack_core::{ - AffectType, AsContextDependency, AsDependencyTemplate, AsModuleDependency, Context, Dependency, - DependencyId, DependencyType, + AffectType, AsContextDependency, AsDependencyTemplate, Context, Dependency, DependencyId, + DependencyType, ModuleDependency, }; use crate::DllEntryPluginOptions; @@ -11,7 +11,7 @@ pub struct DllEntryDependency { pub entries: Vec, - // TODO: pass by serialize & deserialize. + // TODO: The fiedls `name` for serialize & deserialize. pub name: String, id: DependencyId, @@ -34,6 +34,14 @@ impl DllEntryDependency { } } +// It would not create module by rspack,if depdency is not ModuleDependency. +// So we impl ModuleDepedency for [DllEntryDependency] +impl ModuleDependency for DllEntryDependency { + fn request(&self) -> &str { + "dll main" + } +} + impl Dependency for DllEntryDependency { fn id(&self) -> &DependencyId { &self.id @@ -48,7 +56,7 @@ impl Dependency for DllEntryDependency { } } -impl AsModuleDependency for DllEntryDependency {} +// impl AsModuleDependency for DllEntryDependency {} impl AsContextDependency for DllEntryDependency {} diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs new file mode 100644 index 00000000000..6fe963fe5d5 --- /dev/null +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs @@ -0,0 +1,235 @@ +use std::{borrow::Cow, hash::Hash, sync::Arc}; + +use async_trait::async_trait; +use rspack_collections::{Identifiable, Identifier}; +use rspack_core::{ + impl_module_meta_info, impl_source_map_config, module_raw, module_update_hash, + rspack_sources::{BoxSource, OriginalSource, RawSource, Source}, + throw_missing_module_error_block, AsyncDependenciesBlockIdentifier, BuildInfo, BuildMeta, + BuildResult, CodeGenerationResult, Compilation, ConcatenationScope, Context, DependenciesBlock, + DependencyId, FactoryMeta, LibIdentOptions, Module, ModuleDependency, ModuleType, RuntimeGlobals, + RuntimeSpec, SourceType, StaticExportsDependency, StaticExportsSpec, +}; +use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result}; +use rspack_util::source_map::ModuleSourceMapConfig; + +use super::delegated_source_dependency::DelegatedSourceDependency; +use crate::DllManifestContentItem; + +pub type SourceRequest = String; + +#[impl_source_map_config] +#[derive(Debug, Default)] +pub struct DelegatedModule { + source_request: SourceRequest, + request: Option, + delegation_type: String, + user_request: String, + original_request: Option, + delegate_data: DllManifestContentItem, + dependencies: Vec, + blocks: Vec, + factory_meta: Option, + build_info: Option, + build_meta: Option, +} + +impl DelegatedModule { + pub fn new( + source_request: SourceRequest, + data: DllManifestContentItem, + delegation_type: String, + user_request: String, + original_request: Option, + ) -> Self { + Self { + source_request, + // TODO: remove the clone + request: data.id.clone(), + delegation_type, + user_request, + original_request, + delegate_data: data, + ..Default::default() + } + } +} + +#[async_trait] +impl Module for DelegatedModule { + impl_module_meta_info!(); + + fn module_type(&self) -> &ModuleType { + &ModuleType::JsDynamic + } + + fn source_types(&self) -> &[SourceType] { + &[SourceType::JavaScript] + } + + fn lib_ident(&self, _options: LibIdentOptions) -> Option> { + self.original_request.as_ref().map(|request| request.into()) + } + + fn original_source(&self) -> Option<&dyn Source> { + None + } + + fn readable_identifier(&self, _context: &Context) -> Cow { + format!( + "delegated {} from {}", + self.user_request, self.source_request + ) + .into() + } + + fn size(&self, _source_type: Option<&SourceType>, _compilation: &Compilation) -> f64 { + 42.0 + } + + fn get_diagnostics(&self) -> Vec { + vec![] + } + + async fn build( + &mut self, + _build_context: rspack_core::BuildContext<'_>, + _compilation: Option<&Compilation>, + ) -> Result { + Ok(BuildResult { + dependencies: vec![ + Box::new(DelegatedSourceDependency::new(self.source_request.clone())), + Box::new(StaticExportsDependency::new( + match self.delegate_data.exports.clone() { + Some(exports) => StaticExportsSpec::Array(exports), + None => StaticExportsSpec::True, + }, + false, + )), + ], + build_meta: self.delegate_data.build_meta.clone().unwrap_or_default(), + ..Default::default() + }) + } + + fn code_generation( + &self, + compilation: &Compilation, + _runtime: Option<&RuntimeSpec>, + _concatenation_scope: Option, + ) -> Result { + let mut runtime_requirements = RuntimeGlobals::default(); + runtime_requirements.insert(RuntimeGlobals::REQUIRE); + runtime_requirements.insert(RuntimeGlobals::MODULE); + let mut code_generation_reuslt = CodeGenerationResult { + runtime_requirements, + ..Default::default() + }; + + let dep = self.dependencies[0]; + let mg = compilation.get_module_graph(); + let source_module = mg.get_module_by_dependency_id(&dep); + let dependency = mg + .dependency_by_id(&dep) + .and_then(|dep| dep.downcast_ref::()) + .expect("Should be module depdency"); + + let str = match source_module { + Some(_) => { + let mut s = format!( + "module.exports = {}", + module_raw( + compilation, + &mut code_generation_reuslt.runtime_requirements, + &dep, + dependency.request(), + false, + ) + ); + + // TODO: get request + let request = self.request.as_ref().expect("TODO: it should have request"); + + match self.delegation_type.as_ref() { + "require" => { + s += &format!("({})", request); + } + "object" => { + s += &format!("[{}]", request); + } + _ => panic!("delegation_type should be 'require' or 'object'"), + } + + s += ";"; + + s + } + None => throw_missing_module_error_block(&self.source_request), + }; + + let source_map = self.get_source_map_kind(); + + let source: BoxSource = if source_map.source_map() || source_map.simple_source_map() { + Arc::new(OriginalSource::new(str, self.identifier().to_string())) + } else { + let raw_source: RawSource = str.into(); + Arc::new(raw_source) + }; + + code_generation_reuslt = code_generation_reuslt.with_javascript(source); + + Ok(code_generation_reuslt) + } + + fn need_build(&self) -> bool { + self.build_meta.is_none() + } + + fn update_hash( + &self, + mut hasher: &mut dyn std::hash::Hasher, + compilation: &Compilation, + runtime: Option<&RuntimeSpec>, + ) -> Result<()> { + self.delegation_type.hash(&mut hasher); + + if let Some(request) = &self.request { + request.hash(&mut hasher); + } + + module_update_hash(self, hasher, compilation, runtime); + + Ok(()) + } +} + +impl Identifiable for DelegatedModule { + fn identifier(&self) -> Identifier { + format!( + "delegated {} from {}", + self.request.as_deref().unwrap_or_default(), + self.source_request + ) + .into() + } +} + +impl DependenciesBlock for DelegatedModule { + fn add_block_id(&mut self, block: AsyncDependenciesBlockIdentifier) { + self.blocks.push(block); + } + + fn get_blocks(&self) -> &[AsyncDependenciesBlockIdentifier] { + &self.blocks + } + + fn add_dependency_id(&mut self, dependency: DependencyId) { + self.dependencies.push(dependency); + } + + fn get_dependencies(&self) -> &[DependencyId] { + &self.dependencies + } +} + +impl_empty_diagnosable_trait!(DelegatedModule); diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs new file mode 100644 index 00000000000..83d1fa4df16 --- /dev/null +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs @@ -0,0 +1,156 @@ +use rspack_core::{ + ApplyContext, BoxModule, Compilation, CompilationParams, CompilerCompilation, CompilerOptions, + Context, DependencyType, LibIdentOptions, ModuleFactoryCreateData, NormalModuleCreateData, + NormalModuleFactoryFactorize, NormalModuleFactoryModule, Plugin, PluginContext, +}; +use rspack_error::Result; +use rspack_hook::{plugin, plugin_hook}; + +use super::delegated_module::DelegatedModule; +use crate::DllManifestContent; + +#[derive(Debug)] +pub struct DelegatedPluginOptions { + pub source: String, + + pub context: Option, + + pub content: DllManifestContent, + + pub r#type: String, + + pub extensions: Vec, + + pub scope: Option, + + pub compilation_context: Context, +} + +#[plugin] +#[derive(Debug)] +pub struct DelegatedPlugin { + options: DelegatedPluginOptions, +} + +impl DelegatedPlugin { + pub fn new(options: DelegatedPluginOptions) -> Self { + Self::new_inner(options) + } +} + +impl Plugin for DelegatedPlugin { + fn name(&self) -> &'static str { + "rspack.DelegatedPlugin" + } + + fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> { + ctx + .context + .compiler_hooks + .compilation + .tap(compilation::new(self)); + + ctx + .context + .normal_module_factory_hooks + .factorize + .tap(factorize::new(self)); + + ctx + .context + .normal_module_factory_hooks + .module + .tap(module::new(self)); + + Ok(()) + } +} + +#[plugin_hook(CompilerCompilation for DelegatedPlugin)] +async fn compilation( + &self, + compilation: &mut Compilation, + params: &mut CompilationParams, +) -> Result<()> { + compilation.set_dependency_factory( + DependencyType::DelegatedSource, + params.normal_module_factory.clone(), + ); + Ok(()) +} + +#[plugin_hook(NormalModuleFactoryFactorize for DelegatedPlugin)] +async fn factorize(&self, data: &mut ModuleFactoryCreateData) -> Result> { + if let Some(scope) = &self.options.scope { + if let Some(dependency) = data.dependencies[0].as_module_dependency() { + let scope_prefix = format!("{}/", scope); + let request = dependency.request(); + if request.starts_with(&scope_prefix) { + let inner_request = format!( + ".{}", + &request + .chars() + .skip(scope.len()) + .into_iter() + .collect::() + ); + + if let Some(resolved) = self.options.content.get(&inner_request) { + return Ok(Some(Box::new(DelegatedModule::new( + self.options.source.clone(), + resolved.clone(), + self.options.r#type.clone(), + inner_request, + Some(request.to_owned()), + )))); + } + + for extension in self.options.extensions.iter() { + let request_plus_ext = format!("{}{}", inner_request, extension); + + if let Some(resolved) = self.options.content.get(&request_plus_ext) { + return Ok(Some(Box::new(DelegatedModule::new( + self.options.source.clone(), + resolved.clone(), + self.options.r#type.clone(), + request_plus_ext, + format!("{}{}", request, extension).into(), + )))); + } + } + } + } + } + + Ok(None) +} + +#[plugin_hook(NormalModuleFactoryModule for DelegatedPlugin)] +async fn module( + &self, + _data: &mut ModuleFactoryCreateData, + _create_data: &mut NormalModuleCreateData, + module: &mut BoxModule, +) -> Result<()> { + if self.options.scope.is_none() { + if let Some(request) = module.lib_ident(LibIdentOptions { + context: self.options.context.as_ref().unwrap_or(&Context::from("")), + }) { + if let Some(resolved) = self.options.content.get(request.as_ref()) { + let original_request = module.lib_ident(LibIdentOptions { + context: &self.options.compilation_context, + }); + + *module = Box::new(DelegatedModule::new( + self.options.source.clone(), + resolved.clone(), + self.options.r#type.clone(), + request.to_string(), + original_request.map(|request| request.to_string()), + )); + } + }; + } + + Ok(()) +} diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_source_dependency.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_source_dependency.rs new file mode 100644 index 00000000000..3954dcf64d4 --- /dev/null +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_source_dependency.rs @@ -0,0 +1,51 @@ +use rspack_core::{ + AffectType, AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, + DependencyId, DependencyType, ModuleDependency, +}; + +#[derive(Debug, Clone)] +pub struct DelegatedSourceDependency { + id: DependencyId, + request: String, +} + +impl DelegatedSourceDependency { + pub fn new(request: String) -> Self { + Self { + id: DependencyId::new(), + request, + } + } +} + +impl Dependency for DelegatedSourceDependency { + fn id(&self) -> &DependencyId { + &self.id + } + + fn could_affect_referencing_module(&self) -> AffectType { + AffectType::True + } + + fn category(&self) -> &DependencyCategory { + &DependencyCategory::Esm + } + + fn dependency_type(&self) -> &DependencyType { + &DependencyType::DelegatedSource + } +} + +impl ModuleDependency for DelegatedSourceDependency { + fn request(&self) -> &str { + &self.request + } + + fn set_request(&mut self, request: String) { + self.request = request; + } +} + +impl AsContextDependency for DelegatedSourceDependency {} + +impl AsDependencyTemplate for DelegatedSourceDependency {} diff --git a/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs b/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs new file mode 100644 index 00000000000..5cde49c1199 --- /dev/null +++ b/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs @@ -0,0 +1,99 @@ +use rspack_core::{ + ApplyContext, CompilerOptions, Context, ExternalItem, ExternalItemValue, LibraryType, Plugin, + PluginContext, +}; +use rspack_error::Result; +use rspack_hook::plugin; +use rspack_plugin_externals::ExternalsPlugin; +use rustc_hash::FxHashMap as HashMap; + +use super::delegated_plugin::{DelegatedPlugin, DelegatedPluginOptions}; +use crate::{DllManifest, DllManifestContent}; + +#[derive(Debug, Clone)] +pub struct DllReferencePluginAgencyOptions { + pub context: Option, + pub name: Option, + pub content: Option, + pub manifest: Option, + pub extensions: Vec, + pub scope: Option, + pub source_type: Option, + pub r#type: String, +} + +#[plugin] +#[derive(Debug)] +pub struct DllReferenceAgencyPlugin { + options: DllReferencePluginAgencyOptions, +} + +impl DllReferenceAgencyPlugin { + pub fn new(options: DllReferencePluginAgencyOptions) -> Self { + Self::new_inner(options) + } +} + +impl Plugin for DllReferenceAgencyPlugin { + fn name(&self) -> &'static str { + "rspack.DllReferenceAgencyPlugin" + } + + fn apply(&self, ctx: PluginContext<&mut ApplyContext>, options: &CompilerOptions) -> Result<()> { + let mut name = self.options.name.clone(); + let mut source_type = self.options.source_type.clone(); + let mut resolved_content = self + .options + .content + .as_ref() + .and_then(|content| serde_json::from_str::(content).ok()); + + let manifest = self + .options + .manifest + .as_ref() + .and_then(|manifest| serde_json::from_str::(manifest).ok()); + + if let Some(manifest) = manifest { + if name.is_none() { + name = manifest.name; + } + + if source_type.is_none() { + source_type = manifest.r#type; + } + + if resolved_content.is_none() { + resolved_content = Some(manifest.content); + } + } + + let resolved_content = resolved_content.expect("Manifest should have content."); + + let name = name.expect("Should pass name or manifest should have the name attribute"); + + let source = format!("dll-reference {}", name); + + let mut external_item_object = HashMap::default(); + + external_item_object.insert(source.clone(), ExternalItemValue::String(name)); + + let external = ExternalItem::Object(external_item_object); + + ExternalsPlugin::new(source_type.unwrap_or("var".into()), vec![external]) + .apply(PluginContext::with_context(ctx.context), options)?; + + DelegatedPlugin::new(DelegatedPluginOptions { + source: source.clone(), + r#type: self.options.r#type.clone(), + scope: self.options.scope.clone(), + content: resolved_content, + extensions: self.options.extensions.clone(), + context: self.options.context.clone(), + compilation_context: options.context.clone(), + }) + .apply(PluginContext::with_context(ctx.context), options)?; + + Ok(()) + } +} diff --git a/crates/rspack_plugin_dll/src/dll_reference/mod.rs b/crates/rspack_plugin_dll/src/dll_reference/mod.rs new file mode 100644 index 00000000000..fa469e7959e --- /dev/null +++ b/crates/rspack_plugin_dll/src/dll_reference/mod.rs @@ -0,0 +1,4 @@ +pub mod delegated_module; +pub mod delegated_plugin; +pub mod delegated_source_dependency; +pub mod dll_reference_agency_plugin; diff --git a/crates/rspack_plugin_dll/src/flag_alll_modules_as_used_plugin.rs b/crates/rspack_plugin_dll/src/flag_alll_modules_as_used_plugin.rs index 5e5c3d0c443..6a9f02f832a 100644 --- a/crates/rspack_plugin_dll/src/flag_alll_modules_as_used_plugin.rs +++ b/crates/rspack_plugin_dll/src/flag_alll_modules_as_used_plugin.rs @@ -1,7 +1,7 @@ use rspack_collections::IdentifierSet; use rspack_core::{ get_entry_runtime, merge_runtime, ApplyContext, Compilation, CompilationOptimizeDependencies, - CompilerOptions, FactoryMeta, Plugin, PluginContext, RuntimeSpec, + CompilerOptions, Plugin, PluginContext, RuntimeSpec, }; use rspack_error::Result; use rspack_hook::{plugin, plugin_hook}; @@ -20,14 +20,14 @@ impl Plugin for FlagAllModulesAsUsedPlugin { .context .compilation_hooks .optimize_dependencies - .tap(optimze_dependencies::new(self)); + .tap(optimize_dependencies::new(self)); Ok(()) } } #[plugin_hook(CompilationOptimizeDependencies for FlagAllModulesAsUsedPlugin)] -fn optimze_dependencies(&self, compilation: &mut Compilation) -> Result> { +fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result> { let entries = &compilation.entries; let runtime = compilation @@ -37,17 +37,20 @@ fn optimze_dependencies(&self, compilation: &mut Compilation) -> Result; + +#[derive(Debug, Default, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct DllManifestContentItem { + #[serde(skip_serializing_if = "Option::is_none")] + pub build_meta: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub exports: Option>, + + #[serde(skip_serializing_if = "Option::is_none")] + pub id: Option, +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub(crate) struct DllManifest { + pub content: DllManifestContent, + + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub r#type: Option, +} + pub use dll_entry::dll_entry_plugin::{DllEntryPlugin, DllEntryPluginOptions}; +pub use dll_reference::dll_reference_agency_plugin::{ + DllReferenceAgencyPlugin, DllReferencePluginAgencyOptions, +}; pub use flag_alll_modules_as_used_plugin::FlagAllModulesAsUsedPlugin; pub use lib_manifest_plugin::{LibManifestPlugin, LibManifestPluginOptions}; diff --git a/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs b/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs index 73c2ae5815a..99d0b9ce0ee 100644 --- a/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs +++ b/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs @@ -1,16 +1,17 @@ -use std::borrow::Cow; -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; use std::sync::Arc; use rspack_core::{ rspack_sources::{BoxSource, RawSource}, - ApplyContext, BuildMeta, Compilation, CompilerEmit, CompilerOptions, Context, EntryDependency, - Filename, LibIdentOptions, PathData, Plugin, PluginContext, ProvidedExports, SourceType, + ApplyContext, Compilation, CompilationAssets, CompilerEmit, CompilerOptions, Context, + EntryDependency, Filename, LibIdentOptions, PathData, Plugin, PluginContext, ProvidedExports, + SourceType, }; use rspack_error::{Error, Result}; use rspack_hook::{plugin, plugin_hook}; -use rspack_util::atom::Atom; -use serde::Serialize; +use rustc_hash::FxHashMap as HashMap; + +use crate::{DllManifest, DllManifestContent, DllManifestContentItem}; #[derive(Debug, Clone)] pub struct LibManifestPluginOptions { @@ -24,7 +25,7 @@ pub struct LibManifestPluginOptions { pub path: Filename, - pub ty: Option, + pub r#type: Option, } #[plugin] @@ -56,13 +57,13 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { let chunk_graph = &compilation.chunk_graph; - let mut manifest_files = vec![]; + let mut manifests: CompilationAssets = HashMap::default(); let module_graph = compilation.get_module_graph(); for (_, chunk) in compilation.chunk_by_ukey.iter() { if !chunk.can_be_initial(&compilation.chunk_group_by_ukey) { - return Ok(()); + continue; } let target_path = compilation.get_path( @@ -79,10 +80,10 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { use_paths.insert(target_path.clone()); - let name = self.options.name.as_ref().and_then(|filename| { + let name = self.options.name.as_ref().and_then(|name| { compilation .get_path( - filename, + name, PathData { chunk: Some(chunk), content_hash_type: Some(SourceType::JavaScript), @@ -92,7 +93,7 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { .ok() }); - let mut manifest_contents = HashMap::, ManifestContent>::new(); + let mut manifest_content: DllManifestContent = HashMap::default(); for module in chunk_graph.get_ordered_chunk_modules(&chunk.ukey, &module_graph) { if self.options.entry_only.unwrap_or_default() @@ -123,7 +124,13 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { let exports_info = module_graph.get_exports_info(&module.identifier()); let provided_exports = match exports_info.get_provided_exports(&module_graph) { - ProvidedExports::Vec(vec) => Some(vec), + ProvidedExports::Vec(vec) => { + if vec.is_empty() { + None + } else { + Some(vec) + } + } _ => None, }; @@ -131,21 +138,21 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { let build_meta = module.build_meta(); - manifest_contents.insert( - ident, - ManifestContent { - id, - build_meta, - provided_exports, + manifest_content.insert( + ident.into_owned(), + DllManifestContentItem { + id: id.map(|id| id.to_string()), + build_meta: build_meta.map(|meta| meta.clone()), + exports: provided_exports, }, ); } } - let manifest = Manifest { - name: name.as_deref(), - content: manifest_contents, - ty: self.options.ty.clone(), + let manifest = DllManifest { + name, + content: manifest_content, + r#type: self.options.r#type.clone(), }; let format = self.options.format.unwrap_or_default(); @@ -156,55 +163,18 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { serde_json::to_string(&manifest).map_err(|e| Error::msg(format!("{}", e)))? }; - manifest_files.push(ManifestFile { - filename: target_path.clone(), - content: manifest_json.clone(), - }); - } + let asset = Arc::new(RawSource::from(manifest_json)) as BoxSource; - for file in manifest_files { - let filename = file.filename; - let manifest_content = file.content; - let manifest_asset = Arc::new(RawSource::from(manifest_content)) as BoxSource; + manifests.insert(target_path, asset.into()); + } - compilation.emit_asset(filename, manifest_asset.into()); + for (filename, asset) in manifests { + compilation.emit_asset(filename, asset); } Ok(()) } -#[derive(Serialize, Debug)] -#[serde(rename_all = "camelCase")] -struct ManifestContent<'i> { - #[serde(skip_serializing_if = "Option::is_none")] - id: Option<&'i str>, - - #[serde(skip_serializing_if = "Option::is_none")] - build_meta: Option<&'i BuildMeta>, - - #[serde(skip_serializing_if = "Option::is_none")] - provided_exports: Option>, -} - -#[derive(Serialize, Debug)] -struct Manifest<'i> { - #[serde(skip_serializing_if = "Option::is_none")] - name: Option<&'i str>, - - content: HashMap, ManifestContent<'i>>, - - #[serde(rename = "type")] - #[serde(skip_serializing_if = "Option::is_none")] - ty: Option, -} - -#[derive(Debug)] -struct ManifestFile { - filename: String, - - content: String, -} - fn some_in_iterable(iterable: I, filter: F) -> bool where F: Fn(I::Item) -> bool, diff --git a/packages/rspack/etc/core.api.md b/packages/rspack/etc/core.api.md index 931652de0d8..3afe79978c7 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -1385,7 +1385,7 @@ export class DllPlugin { } // @public (undocumented) -type DllPluginOptions = { +export type DllPluginOptions = { context?: string; entryOnly?: boolean; format?: boolean; @@ -1394,6 +1394,53 @@ type DllPluginOptions = { type?: string; }; +// @public (undocumented) +export class DllReferencePlugin { + constructor(options: DllReferencePluginOptions); + // (undocumented) + apply(compiler: Compiler): void; +} + +// @public (undocumented) +export type DllReferencePluginOptions = { + context?: string; + extensions?: string[]; + manifest: string | DllReferencePluginOptionsManifest; + name?: string; + scope?: string; + sourceType?: DllReferencePluginOptionsSourceType; + type?: "require" | "object"; +} | { + content: DllReferencePluginOptionsContent; + context?: string; + extensions?: string[]; + name: string; + scope?: string; + sourceType?: DllReferencePluginOptionsSourceType; + type?: "require" | "object"; +}; + +// @public +export interface DllReferencePluginOptionsContent { + [k: string]: { + buildMeta?: { + [k: string]: any; + }; + exports?: string[] | true; + id: number | string; + }; +} + +// @public +export interface DllReferencePluginOptionsManifest { + content: DllReferencePluginOptionsContent; + name?: string; + type?: DllReferencePluginOptionsSourceType; +} + +// @public +export type DllReferencePluginOptionsSourceType = "var" | "assign" | "this" | "window" | "global" | "commonjs" | "commonjs2" | "commonjs-module" | "amd" | "amd-require" | "umd" | "umd2" | "jsonp" | "system"; + // @public (undocumented) interface Drafts { customMedia?: boolean; @@ -4452,6 +4499,12 @@ declare namespace rspackExports { HotModuleReplacementPlugin, NoEmitOnErrorsPlugin, DllPlugin, + DllPluginOptions, + DllReferencePlugin, + DllReferencePluginOptions, + DllReferencePluginOptionsSourceType, + DllReferencePluginOptionsContent, + DllReferencePluginOptionsManifest, EnvironmentPlugin, LoaderOptionsPlugin, LoaderTargetPlugin, diff --git a/packages/rspack/src/builtin-plugin/DllReferenceAgencyPlugin.ts b/packages/rspack/src/builtin-plugin/DllReferenceAgencyPlugin.ts new file mode 100644 index 00000000000..80440ab9b0a --- /dev/null +++ b/packages/rspack/src/builtin-plugin/DllReferenceAgencyPlugin.ts @@ -0,0 +1,15 @@ +import { + BuiltinPluginName, + type RawDllReferencePluginAgencyOptions +} from "@rspack/binding"; +import { create } from "./base"; + +export type DllReferencePluginAgencyOptions = + RawDllReferencePluginAgencyOptions; + +export const DllReferenceAgencyPlugin = create( + BuiltinPluginName.DllReferenceAgencyPlugin, + ( + options: DllReferencePluginAgencyOptions + ): RawDllReferencePluginAgencyOptions => options +); diff --git a/packages/rspack/src/builtin-plugin/LibManifestPlugin.ts b/packages/rspack/src/builtin-plugin/LibManifestPlugin.ts index 2453356e394..c2f647d1edc 100644 --- a/packages/rspack/src/builtin-plugin/LibManifestPlugin.ts +++ b/packages/rspack/src/builtin-plugin/LibManifestPlugin.ts @@ -29,7 +29,7 @@ export const LibManifestPlugin = create( format, name, path, - ty: type + type }; } ); diff --git a/packages/rspack/src/builtin-plugin/index.ts b/packages/rspack/src/builtin-plugin/index.ts index 43a3cd7c38f..1e682bf7a08 100644 --- a/packages/rspack/src/builtin-plugin/index.ts +++ b/packages/rspack/src/builtin-plugin/index.ts @@ -67,3 +67,4 @@ export * from "./NoEmitOnErrorsPlugin"; export * from "./ContextReplacementPlugin"; export * from "./LibManifestPlugin"; export * from "./DllEntryPlugin"; +export * from "./DllReferenceAgencyPlugin"; diff --git a/packages/rspack/src/exports.ts b/packages/rspack/src/exports.ts index 985aad0083a..47c1e4a08bf 100644 --- a/packages/rspack/src/exports.ts +++ b/packages/rspack/src/exports.ts @@ -103,7 +103,14 @@ export { DynamicEntryPlugin } from "./builtin-plugin"; export { ExternalsPlugin } from "./builtin-plugin"; export { HotModuleReplacementPlugin } from "./builtin-plugin"; export { NoEmitOnErrorsPlugin } from "./builtin-plugin"; -export { DllPlugin } from "./lib/DllPlugin"; +export { DllPlugin, type DllPluginOptions } from "./lib/DllPlugin"; +export { + DllReferencePlugin, + type DllReferencePluginOptions, + type DllReferencePluginOptionsSourceType, + type DllReferencePluginOptionsContent, + type DllReferencePluginOptionsManifest +} from "./lib/DllReferencePlugin"; export { EnvironmentPlugin } from "./lib/EnvironmentPlugin"; export { LoaderOptionsPlugin } from "./lib/LoaderOptionsPlugin"; export { LoaderTargetPlugin } from "./lib/LoaderTargetPlugin"; diff --git a/packages/rspack/src/lib/DllPlugin.ts b/packages/rspack/src/lib/DllPlugin.ts index 6a158a3a991..d91744ca2ef 100644 --- a/packages/rspack/src/lib/DllPlugin.ts +++ b/packages/rspack/src/lib/DllPlugin.ts @@ -83,6 +83,8 @@ export class DllPlugin { new DllEntryPlugin(context, entries, options).apply(compiler); } + + return true; }); new LibManifestPlugin(this.options).apply(compiler); diff --git a/packages/rspack/src/lib/DllReferencePlugin.ts b/packages/rspack/src/lib/DllReferencePlugin.ts new file mode 100644 index 00000000000..42d78a0d4c3 --- /dev/null +++ b/packages/rspack/src/lib/DllReferencePlugin.ts @@ -0,0 +1,255 @@ +/** + * The following code is modified based on + * https://github.com/webpack/webpack/blob/4b4ca3bb53f36a5b8fc6bc1bd976ed7af161bd80/lib/DllReferencePlugin.js + * + * MIT Licensed + * Author Tobias Koppers @sokra + * Copyright (c) JS Foundation and other contributors + * https://github.com/webpack/webpack/blob/main/LICENSE + */ + +import z from "zod"; +import type { Compiler } from "../Compiler"; +import { DllReferenceAgencyPlugin } from "../builtin-plugin"; +import { validate } from "../util/validate"; + +export type DllReferencePluginOptions = + | { + /** + * Context of requests in the manifest (or content property) as absolute path. + */ + context?: string; + /** + * Extensions used to resolve modules in the dll bundle (only used when using 'scope'). + */ + extensions?: string[]; + /** + * An object containing content and name or a string to the absolute path of the JSON manifest to be loaded upon compilation. + */ + manifest: string | DllReferencePluginOptionsManifest; + /** + * The name where the dll is exposed (external name, defaults to manifest.name). + */ + name?: string; + /** + * Prefix which is used for accessing the content of the dll. + */ + scope?: string; + /** + * How the dll is exposed (libraryTarget, defaults to manifest.type). + */ + sourceType?: DllReferencePluginOptionsSourceType; + /** + * The way how the export of the dll bundle is used. + */ + type?: "require" | "object"; + } + | { + /** + * The mappings from request to module info. + */ + content: DllReferencePluginOptionsContent; + /** + * Context of requests in the manifest (or content property) as absolute path. + */ + context?: string; + /** + * Extensions used to resolve modules in the dll bundle (only used when using 'scope'). + */ + extensions?: string[]; + /** + * The name where the dll is exposed (external name). + */ + name: string; + /** + * Prefix which is used for accessing the content of the dll. + */ + scope?: string; + /** + * How the dll is exposed (libraryTarget). + */ + sourceType?: DllReferencePluginOptionsSourceType; + /** + * The way how the export of the dll bundle is used. + */ + type?: "require" | "object"; + }; +/** + * The type how the dll is exposed (external type). + */ +export type DllReferencePluginOptionsSourceType = + | "var" + | "assign" + | "this" + | "window" + | "global" + | "commonjs" + | "commonjs2" + | "commonjs-module" + | "amd" + | "amd-require" + | "umd" + | "umd2" + | "jsonp" + | "system"; + +/** + * An object containing content, name and type. + */ +export interface DllReferencePluginOptionsManifest { + /** + * The mappings from request to module info. + */ + content: DllReferencePluginOptionsContent; + /** + * The name where the dll is exposed (external name). + */ + name?: string; + /** + * The type how the dll is exposed (external type). + */ + type?: DllReferencePluginOptionsSourceType; +} +/** + * The mappings from request to module info. + */ +export interface DllReferencePluginOptionsContent { + /** + * Module info. + */ + [k: string]: { + /** + * Meta information about the module. + */ + buildMeta?: { + [k: string]: any; + }; + /** + * Information about the provided exports of the module. + */ + exports?: string[] | true; + /** + * Module ID. + */ + id: number | string; + }; +} + +const dllReferencePluginOptionsContentItem = z.object({ + buildMeta: z.record(z.any()).optional(), + exports: z.array(z.string()).or(z.literal(true)).optional(), + id: z.union([z.number(), z.string()]) +}); + +const dllReferencePluginOptionsContent = z.record( + dllReferencePluginOptionsContentItem +) satisfies z.ZodType; + +const dllReferencePluginOptionsSourceType = z.enum([ + "var", + "assign", + "this", + "window", + "global", + "commonjs", + "commonjs2", + "commonjs-module", + "amd", + "amd-require", + "umd", + "umd2", + "jsonp", + "system" +]) satisfies z.ZodType; + +const dllReferencePluginOptionsManifest = z.object({ + content: dllReferencePluginOptionsContent, + name: z.string().optional(), + type: dllReferencePluginOptionsSourceType.optional() +}) satisfies z.ZodType; + +const dllReferencePluginOptions = z.union([ + z.object({ + context: z.string().optional(), + extensions: z.array(z.string()).optional(), + manifest: z.string().or(dllReferencePluginOptionsManifest), + name: z.string().optional(), + scope: z.string().optional(), + sourceType: dllReferencePluginOptionsSourceType.optional(), + type: z.enum(["require", "object"]).optional() + }), + z.object({ + content: dllReferencePluginOptionsContent, + context: z.string().optional(), + extensions: z.array(z.string()).optional(), + name: z.string(), + scope: z.string().optional(), + sourceType: dllReferencePluginOptionsSourceType.optional(), + type: z.enum(["require", "object"]).optional() + }) +]) satisfies z.ZodType; + +const DLL_REFERENCE_PLUGIN_NAME = "DllReferencePlugin"; + +export class DllReferencePlugin { + private options: DllReferencePluginOptions; + + constructor(options: DllReferencePluginOptions) { + validate(options, dllReferencePluginOptions); + + this.options = options; + } + + apply(compiler: Compiler) { + compiler.hooks.beforeCompile.tapAsync( + DLL_REFERENCE_PLUGIN_NAME, + (_, callback) => { + if ("manifest" in this.options) { + const manifest = this.options.manifest; + + let manifest_content: string | undefined = undefined; + + if (typeof manifest === "string") { + compiler.inputFileSystem?.readFile( + manifest, + "utf8", + (err, result) => { + if (err) return callback(err); + + manifest_content = result; + } + ); + } else { + manifest_content = JSON.stringify(manifest); + } + + new DllReferenceAgencyPlugin({ + ...this.options, + type: this.options.type || "require", + extensions: this.options.extensions || [ + "", + ".js", + ".json", + ".wasm" + ], + manifest: manifest_content + }).apply(compiler); + + callback(); + } + } + ); + + compiler.hooks.compilation.tap( + DLL_REFERENCE_PLUGIN_NAME, + (compilation, _) => { + if ( + "manifest" in this.options && + typeof this.options.manifest === "string" + ) { + compilation.fileDependencies.add(this.options.manifest); + } + } + ); + } +} From 7cd40f2dea7d337cffb05261fccf1802566b2130 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Thu, 31 Oct 2024 17:23:39 +0800 Subject: [PATCH 03/22] test: add dll plugin tests --- .../chunk-loading/chunk-name/rspack.config.js | 1 + .../dll-plugin-entry/0-create-dll/dep.js | 5 ++ .../dll-plugin-entry/0-create-dll/dep2.js | 1 + .../dll-plugin-entry/0-create-dll/index.js | 4 ++ .../0-create-dll/rspack.config.js | 20 ++++++ .../0-create-dll/test.config.js | 1 + .../0-create-dll/test.filter.js | 1 + .../dll-plugin-entry/1-use-dll/index.js | 25 +++++++ .../1-use-dll/rspack.config.js | 17 +++++ .../dll-plugin-entry/1-use-dll/test.filter.js | 1 + .../2-error-non-entry/errors.js | 1 + .../2-error-non-entry/index.js | 1 + .../2-error-non-entry/rspack.config.js | 16 +++++ .../2-error-non-entry/test.filter.js | 1 + .../dll-plugin-format/0-create-dll/dep.js | 1 + .../dll-plugin-format/0-create-dll/index.js | 4 ++ .../0-create-dll/rspack.config.js | 24 +++++++ .../0-create-dll/test.config.js | 1 + .../0-create-dll/test.filter.js | 1 + .../dll-plugin-format/0-create-dll/utility.js | 7 ++ .../0-create-dll/dependency.js | 3 + .../0-create-dll/dependency2.js | 3 + .../0-create-dll/index.js | 3 + .../0-create-dll/module.js | 12 ++++ .../0-create-dll/rspack.config.js | 34 +++++++++ .../0-create-dll/test.config.js | 1 + .../0-create-dll/test.filter.js | 1 + .../1-use-dll/index.js | 9 +++ .../1-use-dll/rspack.config.js | 13 ++++ .../1-use-dll/test.filter.js | 1 + .../configCases/dll-plugin/0-create-dll/_d.js | 1 + .../configCases/dll-plugin/0-create-dll/_e.js | 3 + .../configCases/dll-plugin/0-create-dll/a.js | 1 + .../configCases/dll-plugin/0-create-dll/b.js | 3 + .../configCases/dll-plugin/0-create-dll/c.js | 1 + .../configCases/dll-plugin/0-create-dll/d.js | 1 + .../configCases/dll-plugin/0-create-dll/e.js | 4 ++ .../configCases/dll-plugin/0-create-dll/e1.js | 3 + .../configCases/dll-plugin/0-create-dll/e2.js | 3 + .../dll-plugin/0-create-dll/ee1.js | 2 + .../dll-plugin/0-create-dll/ee2.js | 2 + .../configCases/dll-plugin/0-create-dll/f.jsx | 1 + .../dll-plugin/0-create-dll/g-loader.js | 4 ++ .../dll-plugin/0-create-dll/g.abc.js | 1 + .../configCases/dll-plugin/0-create-dll/h.js | 1 + .../configCases/dll-plugin/0-create-dll/h1.js | 2 + .../configCases/dll-plugin/0-create-dll/ha.js | 1 + .../configCases/dll-plugin/0-create-dll/hb.js | 1 + .../dll-plugin/0-create-dll/rspack.config.js | 43 ++++++++++++ .../dll-plugin/0-create-dll/test.config.js | 1 + .../dll-plugin/0-create-dll/test.filter.js | 1 + .../dll-plugin/0-issue-10475/index.js | 5 ++ .../node_modules/test-package/constants.js | 2 + .../node_modules/test-package/index.js | 5 ++ .../node_modules/test-package/package.json | 4 ++ .../node_modules/test-package/someFunction.js | 3 + .../test-package/working-constants.js | 7 ++ .../dll-plugin/0-issue-10475/rspack.config.js | 23 +++++++ .../dll-plugin/0-issue-10475/test.config.js | 1 + .../dll-plugin/0-issue-10475/test.filter.js | 1 + .../dll-plugin/1-issue-10475/index.js | 3 + .../dll-plugin/1-issue-10475/rspack.config.js | 13 ++++ .../dll-plugin/1-issue-10475/test.filter.js | 1 + .../configCases/dll-plugin/1-use-dll/e.js | 2 + .../configCases/dll-plugin/1-use-dll/index.js | 60 ++++++++++++++++ .../dll-plugin/1-use-dll/rspack.config.js | 17 +++++ .../dll-plugin/1-use-dll/test.filter.js | 1 + .../dll-plugin/2-use-dll-without-scope/e.js | 2 + .../2-use-dll-without-scope/index.js | 69 +++++++++++++++++++ .../2-use-dll-without-scope/rspack.config.js | 37 ++++++++++ .../2-use-dll-without-scope/test.filter.js | 1 + .../dll-plugin/3-use-dll-with-hashid/e.js | 2 + .../dll-plugin/3-use-dll-with-hashid/e1.js | 2 + .../dll-plugin/3-use-dll-with-hashid/index.js | 29 ++++++++ .../3-use-dll-with-hashid/rspack.config.js | 32 +++++++++ .../3-use-dll-with-hashid/test.filter.js | 1 + .../3-use-dll-with-hashid/warnings.js | 3 + 77 files changed, 619 insertions(+) create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep2.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/errors.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/dep.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/utility.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency2.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/module.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_d.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_e.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/a.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/b.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/c.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/d.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e1.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e2.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee1.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee2.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/f.jsx create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g-loader.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g.abc.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h1.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ha.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/hb.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/constants.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/package.json create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/someFunction.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/working-constants.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/e.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/e.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e1.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js create mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/warnings.js diff --git a/packages/rspack-test-tools/tests/configCases/chunk-loading/chunk-name/rspack.config.js b/packages/rspack-test-tools/tests/configCases/chunk-loading/chunk-name/rspack.config.js index 2360cda109e..1ec9330dd53 100644 --- a/packages/rspack-test-tools/tests/configCases/chunk-loading/chunk-name/rspack.config.js +++ b/packages/rspack-test-tools/tests/configCases/chunk-loading/chunk-name/rspack.config.js @@ -2,6 +2,7 @@ const { EntryPlugin } = require("@rspack/core"); const path = require("path"); /** @type {import("@rspack/core").Configuration} */ module.exports = { + entry: './index.js', plugins: [ new EntryPlugin(__dirname, path.resolve(__dirname, "./index.js"), { name: "HtmlWebpackPlugin_0-C:\\userCode\\x-project\\node_modules\\html-webpack-plugin\\lib\\loader.js!C:\\userCode\\x-project\\index.html", diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep.js new file mode 100644 index 00000000000..bca818d4d63 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep.js @@ -0,0 +1,5 @@ +export function foo() { + console.log("foo"); +} + +export const bar = "bar"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep2.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep2.js new file mode 100644 index 00000000000..e7134e7006d --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep2.js @@ -0,0 +1 @@ +module.exports = "foo"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/index.js new file mode 100644 index 00000000000..80cdc870f74 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/index.js @@ -0,0 +1,4 @@ +export { bar } from "./dep"; +export default 42; + +require("./dep2"); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/rspack.config.js new file mode 100644 index 00000000000..22151bc3c02 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/rspack.config.js @@ -0,0 +1,20 @@ +var path = require("path"); +var rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: ["."], + output: { + filename: "dll.js", + chunkFilename: "[id].dll.js", + libraryTarget: "commonjs2" + }, + plugins: [ + new rspack.DllPlugin({ + path: path.resolve( + __dirname, + "../../../js/config/dll-plugin-entry/manifest0.json" + ) + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.config.js new file mode 100644 index 00000000000..08ea6c319c8 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.config.js @@ -0,0 +1 @@ +exports.noTests = true; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.filter.js new file mode 100644 index 00000000000..8e5c51af343 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.filter.js @@ -0,0 +1 @@ +module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/index.js new file mode 100644 index 00000000000..c9818abdef7 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/index.js @@ -0,0 +1,25 @@ +import Answer, { bar } from "dll/index"; + +it("should load a module from dll", function() { + expect(require("dll/index")).toEqual(nsObj({ bar: "bar", default: 42 })); +}); + +it("should load an ES module from dll (default export)", function() { + expect(Answer).toBe(42); +}); + +it("should load an ES module from dll (star export)", function() { + expect(bar).toBe("bar"); +}); + +it("should give modules the correct ids", function() { + expect( + Object.keys(__webpack_modules__) + .filter(m => !m.startsWith("../..")) + .sort() + ).toEqual([ + "./index.js", + "dll-reference ../0-create-dll/dll.js", + "dll/index.js" + ]); +}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/rspack.config.js new file mode 100644 index 00000000000..53f0ebf8f04 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/rspack.config.js @@ -0,0 +1,17 @@ +var rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: './index.js', + optimization: { + moduleIds: "named" + }, + plugins: [ + new rspack.DllReferencePlugin({ + manifest: require("../../../js/config/dll-plugin-entry/manifest0.json"), // eslint-disable-line node/no-missing-require + name: "../0-create-dll/dll.js", + scope: "dll", + sourceType: "commonjs2" + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/test.filter.js new file mode 100644 index 00000000000..8e5c51af343 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/test.filter.js @@ -0,0 +1 @@ +module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/errors.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/errors.js new file mode 100644 index 00000000000..857282ec937 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/errors.js @@ -0,0 +1 @@ +module.exports = [[/Can't resolve 'dll\/dep2'/]]; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/index.js new file mode 100644 index 00000000000..901f17038f8 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/index.js @@ -0,0 +1 @@ +require("dll/dep2"); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/rspack.config.js new file mode 100644 index 00000000000..c9ae2bf5352 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/rspack.config.js @@ -0,0 +1,16 @@ +var webpack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + optimization: { + moduleIds: "named" + }, + plugins: [ + new webpack.DllReferencePlugin({ + manifest: require("../../../js/config/dll-plugin-entry/manifest0.json"), // eslint-disable-line node/no-missing-require + name: "../0-create-dll/dll.js", + scope: "dll", + sourceType: "commonjs2" + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js new file mode 100644 index 00000000000..8e5c51af343 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js @@ -0,0 +1 @@ +module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/dep.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/dep.js new file mode 100644 index 00000000000..e7134e7006d --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/dep.js @@ -0,0 +1 @@ +module.exports = "foo"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/index.js new file mode 100644 index 00000000000..59ef4a4cd38 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/index.js @@ -0,0 +1,4 @@ +export { add } from "./utility"; +export default "Format"; + +require("./dep"); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/rspack.config.js new file mode 100644 index 00000000000..1ff1f56b10e --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/rspack.config.js @@ -0,0 +1,24 @@ +var path = require("path"); +var rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: ["."], + resolve: { + extensions: [".js"] + }, + output: { + filename: "dll.js", + chunkFilename: "[id].dll.js", + libraryTarget: "commonjs2" + }, + plugins: [ + new rspack.DllPlugin({ + path: path.resolve( + __dirname, + "../../../js/config/dll-plugin-format/manifest0.json" + ), + format: true + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.config.js new file mode 100644 index 00000000000..08ea6c319c8 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.config.js @@ -0,0 +1 @@ +exports.noTests = true; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.filter.js new file mode 100644 index 00000000000..8e5c51af343 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.filter.js @@ -0,0 +1 @@ +module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/utility.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/utility.js new file mode 100644 index 00000000000..cbed57e2c2d --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/utility.js @@ -0,0 +1,7 @@ +export function add(a, b) { + return a + b; +} + +export function diff(a, b) { + return a - b; +} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency.js new file mode 100644 index 00000000000..1946cd07cb8 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency.js @@ -0,0 +1,3 @@ +export default function createB() { + return "b"; +} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency2.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency2.js new file mode 100644 index 00000000000..0809fdd0180 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency2.js @@ -0,0 +1,3 @@ +export default function createC() { + return "c"; +} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/index.js new file mode 100644 index 00000000000..3a02fe46bff --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/index.js @@ -0,0 +1,3 @@ +import { a } from "./module"; + +export default a(); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/module.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/module.js new file mode 100644 index 00000000000..257c5b97d93 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/module.js @@ -0,0 +1,12 @@ +import createB from "./dependency"; +import createC from "./dependency2"; + +export function a() { + return "a"; +} + +export { createB as b }; + +export function c() { + return createC(); +} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/rspack.config.js new file mode 100644 index 00000000000..ff80a8e1a6f --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/rspack.config.js @@ -0,0 +1,34 @@ +var path = require("path"); +var rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: ["./index"], + output: { + filename: "dll.js", + chunkFilename: "[id].dll.js", + libraryTarget: "commonjs2" + }, + module: { + rules: [ + { + test: /0-create-dll.(module|dependency)/, + sideEffects: false + } + ] + }, + optimization: { + usedExports: true, + sideEffects: true, + concatenateModules: false + }, + plugins: [ + new rspack.DllPlugin({ + path: path.resolve( + __dirname, + "../../../js/config/dll-plugin-side-effects/manifest0.json", + ), + entryOnly: false + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.config.js new file mode 100644 index 00000000000..08ea6c319c8 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.config.js @@ -0,0 +1 @@ +exports.noTests = true; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js new file mode 100644 index 00000000000..8e5c51af343 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js @@ -0,0 +1 @@ +module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/index.js new file mode 100644 index 00000000000..db354f9f5d6 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/index.js @@ -0,0 +1,9 @@ +it("should include all exports and modules in the dll", function() { + const { a, b, c } = require("dll/module"); + expect(typeof a).toBe("function"); + expect(a()).toBe("a"); + expect(typeof b).toBe("function"); + expect(b()).toBe("b"); + expect(typeof c).toBe("function"); + expect(c()).toBe("c"); +}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/rspack.config.js new file mode 100644 index 00000000000..b8b98d05050 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/rspack.config.js @@ -0,0 +1,13 @@ +var rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + plugins: [ + new rspack.DllReferencePlugin({ + manifest: require("../../../js/config/dll-plugin-side-effects/manifest0.json"), // eslint-disable-line node/no-missing-require + name: "../0-create-dll/dll.js", + scope: "dll", + sourceType: "commonjs2" + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js new file mode 100644 index 00000000000..8e5c51af343 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js @@ -0,0 +1 @@ +module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_d.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_d.js new file mode 100644 index 00000000000..d108c9a3722 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_d.js @@ -0,0 +1 @@ +import "./d"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_e.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_e.js new file mode 100644 index 00000000000..586eb3aa06b --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_e.js @@ -0,0 +1,3 @@ +import "./e1"; +import "./e2"; +import "./e"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/a.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/a.js new file mode 100644 index 00000000000..6cd1d0075d4 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/a.js @@ -0,0 +1 @@ +module.exports = "a"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/b.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/b.js new file mode 100644 index 00000000000..58a90d8f841 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/b.js @@ -0,0 +1,3 @@ +module.exports = function() { + return import("./c"); +} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/c.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/c.js new file mode 100644 index 00000000000..b2091de76d6 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/c.js @@ -0,0 +1 @@ +export default "c"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/d.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/d.js new file mode 100644 index 00000000000..987d6d7e401 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/d.js @@ -0,0 +1 @@ +export default "d"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e.js new file mode 100644 index 00000000000..9fbe80f85cf --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e.js @@ -0,0 +1,4 @@ +export * from "./e1"; +export * from "./ee2"; + +console.log.bind(console); // side effect to avoid removing module diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e1.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e1.js new file mode 100644 index 00000000000..23709cd95ff --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e1.js @@ -0,0 +1,3 @@ +export * from "./ee1"; + +console.log.bind(console); // side effect to avoid removing module diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e2.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e2.js new file mode 100644 index 00000000000..25612746b57 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e2.js @@ -0,0 +1,3 @@ +export * from "./ee2"; + +console.log.bind(console); // side effect to avoid removing module diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee1.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee1.js new file mode 100644 index 00000000000..359c69fe3e7 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee1.js @@ -0,0 +1,2 @@ +export var x1 = 123; +export var y1 = 456; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee2.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee2.js new file mode 100644 index 00000000000..634e1a91947 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee2.js @@ -0,0 +1,2 @@ +export var x2 = 123; +export var y2 = 456; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/f.jsx b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/f.jsx new file mode 100644 index 00000000000..61445975b07 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/f.jsx @@ -0,0 +1 @@ +module.exports = 'f'; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g-loader.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g-loader.js new file mode 100644 index 00000000000..3ae53b4dca3 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g-loader.js @@ -0,0 +1,4 @@ +/** @type {import("@rspack/core").LoaderDefinition} */ +module.exports = function (source) { + return source; +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g.abc.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g.abc.js new file mode 100644 index 00000000000..483352ffbff --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g.abc.js @@ -0,0 +1 @@ +module.exports = typeof module.id; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h.js new file mode 100644 index 00000000000..1fa89a4fb1c --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h.js @@ -0,0 +1 @@ +export { B } from "./h1.js"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h1.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h1.js new file mode 100644 index 00000000000..a392743d956 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h1.js @@ -0,0 +1,2 @@ +export { A } from "./ha.js"; +export { B } from "./hb.js"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ha.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ha.js new file mode 100644 index 00000000000..6506d8d86b2 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ha.js @@ -0,0 +1 @@ +export const A = "A"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/hb.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/hb.js new file mode 100644 index 00000000000..f3c1f2c5d79 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/hb.js @@ -0,0 +1 @@ +export const B = "B"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/rspack.config.js new file mode 100644 index 00000000000..ac8d019e125 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/rspack.config.js @@ -0,0 +1,43 @@ +var path = require("path"); +var webpack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: ["./a", "./b", "./_d", "./_e", "./f", "./g.abc", "./h"], + resolve: { + extensions: [".js", ".jsx"] + }, + output: { + filename: "dll.js", + chunkFilename: "[id].dll.js", + libraryTarget: "commonjs2" + }, + module: { + rules: [ + { + test: /\.abc\.js$/, + loader: "./g-loader.js", + options: { + test: 1 + } + }, + { + test: /0-create-dll.h/, + sideEffects: false + } + ] + }, + optimization: { + usedExports: true, + sideEffects: true + }, + plugins: [ + new webpack.DllPlugin({ + path: path.resolve( + __dirname, + "../../../js/config/dll-plugin/manifest0.json" + ), + entryOnly: false + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.config.js new file mode 100644 index 00000000000..08ea6c319c8 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.config.js @@ -0,0 +1 @@ +exports.noTests = true; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.filter.js new file mode 100644 index 00000000000..13ddc93dc9b --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.filter.js @@ -0,0 +1 @@ +module.exports = () => { return true } diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/index.js new file mode 100644 index 00000000000..a88d1863e69 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/index.js @@ -0,0 +1,5 @@ +import { constants } from "test-package"; + +var x = constants; + +export {x} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/constants.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/constants.js new file mode 100644 index 00000000000..84fc2484ce4 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/constants.js @@ -0,0 +1,2 @@ +export const constant1 = 'constant1'; +export const constant2 = 'constant2'; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/index.js new file mode 100644 index 00000000000..89b290e87fa --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/index.js @@ -0,0 +1,5 @@ +import * as _constants from './constants'; +export var constants = _constants; +export { default as someFunction } from './someFunction'; + +if(Math.random() < 0) console.log(constants); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/package.json b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/package.json new file mode 100644 index 00000000000..ce5fa639dd0 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/package.json @@ -0,0 +1,4 @@ +{ + "main": "index.js", + "sideEffects": false +} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/someFunction.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/someFunction.js new file mode 100644 index 00000000000..757d25c6ae7 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/someFunction.js @@ -0,0 +1,3 @@ +export default function someFunction() { + console.log('This is some function'); +} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/working-constants.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/working-constants.js new file mode 100644 index 00000000000..cd433005d3a --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/working-constants.js @@ -0,0 +1,7 @@ +export const constant1 = 'constant1'; +export const constant2 = 'constant2'; + +export default { + constant1, + constant2, +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/rspack.config.js new file mode 100644 index 00000000000..f79631d2360 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/rspack.config.js @@ -0,0 +1,23 @@ +var path = require("path"); +var rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: ["./index.js"], + output: { + filename: "dll.js", + chunkFilename: "[id].dll.js", + library: { + type: 'commonjs2', + } + }, + + plugins: [ + new rspack.DllPlugin({ + path: path.resolve( + __dirname, + "../../../js/config/dll-plugin/issue-10475.json" + ) + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.config.js new file mode 100644 index 00000000000..08ea6c319c8 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.config.js @@ -0,0 +1 @@ +exports.noTests = true; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.filter.js new file mode 100644 index 00000000000..13ddc93dc9b --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.filter.js @@ -0,0 +1 @@ +module.exports = () => { return true } diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/index.js new file mode 100644 index 00000000000..ae5367529b1 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/index.js @@ -0,0 +1,3 @@ +it("should have all modules", () => { + require("dll/index.js"); +}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/rspack.config.js new file mode 100644 index 00000000000..ec154d0c098 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/rspack.config.js @@ -0,0 +1,13 @@ +var rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + plugins: [ + new rspack.DllReferencePlugin({ + manifest: require("../../../js/config/dll-plugin/issue-10475.json"), // eslint-disable-line node/no-missing-require + name: "../0-issue-10475/dll.js", + scope: "dll", + sourceType: "commonjs2", + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/test.filter.js new file mode 100644 index 00000000000..13ddc93dc9b --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/test.filter.js @@ -0,0 +1 @@ +module.exports = () => { return true } diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/e.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/e.js new file mode 100644 index 00000000000..f490fc4645b --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/e.js @@ -0,0 +1,2 @@ +export * from "dll/e1"; +export * from "dll/e2"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/index.js new file mode 100644 index 00000000000..18431f91483 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/index.js @@ -0,0 +1,60 @@ +import d from "dll/d"; +import { x1, y2 } from "./e"; +import { x2, y1 } from "dll/e"; +import { B } from "dll/h"; + +it("should load a module from dll", function() { + expect(require("dll/a")).toBe("a"); +}); + +it("should load a module of non-default type without extension from dll", function() { + expect(require("dll/f")).toBe("f"); +}); + +it("should load an async module from dll", function(done) { + require("dll/b")() + .then(function(c) { + expect(c).toEqual(nsObj({ default: "c" })); + done(); + }) + .catch(done); +}); + +it("should load an ES module from dll (default export)", function() { + expect(d).toBe("d"); +}); + +it("should load an ES module from dll (star export)", function() { + expect(x1).toBe(123); + expect(x2).toBe(123); + expect(y1).toBe(456); + expect(y2).toBe(456); +}); + +it("should load a module with loader applied", function() { + expect(require("dll/g.abc.js")).toBe("number"); +}); + +it("should give modules the correct ids", function() { + expect( + Object.keys(__webpack_modules__) + .filter(m => !m.startsWith("../..")) + .sort() + ).toEqual([ + "./index.js", + "dll-reference ../0-create-dll/dll.js", + "dll/a.js", + "dll/b.js", + "dll/d.js", + "dll/e.js", + "dll/e1.js", + "dll/e2.js", + "dll/f.jsx", + "dll/g.abc.js", + "dll/h.js" + ]); +}); + +it("should not crash on side-effect-free modules", function() { + expect(B).toBe("B"); +}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/rspack.config.js new file mode 100644 index 00000000000..e3b0daffa9c --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/rspack.config.js @@ -0,0 +1,17 @@ +var rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + optimization: { + moduleIds: "named" + }, + plugins: [ + new rspack.DllReferencePlugin({ + manifest: require("../../../js/config/dll-plugin/manifest0.json"), // eslint-disable-line node/no-missing-require + name: "../0-create-dll/dll.js", + scope: "dll", + sourceType: "commonjs2", + extensions: [".js", ".jsx"] + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/test.filter.js new file mode 100644 index 00000000000..687e4d88602 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/test.filter.js @@ -0,0 +1 @@ +module.exports = () => { return false } diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/e.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/e.js new file mode 100644 index 00000000000..106e272113d --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/e.js @@ -0,0 +1,2 @@ +export * from "../0-create-dll/e1"; +export * from "../0-create-dll/e2"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/index.js new file mode 100644 index 00000000000..977c65d2aa5 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/index.js @@ -0,0 +1,69 @@ +import d from "../0-create-dll/d"; +import { x1, y2 } from "./e"; +import { x2, y1 } from "../0-create-dll/e"; +import { B } from "../0-create-dll/h"; +import { A } from "../0-create-dll/h1"; + +it("should load a module from dll", function () { + expect(require("../0-create-dll/a")).toBe("a"); +}); + +it("should load a module of non-default type without extension from dll", function () { + expect(require("../0-create-dll/f")).toBe("f"); +}); + +it("should load an async module from dll", function (done) { + require("../0-create-dll/b")() + .then(function (c) { + expect(c).toEqual(nsObj({ default: "c" })); + done(); + }) + .catch(done); +}); + +it("should load an ES module from dll (default export)", function () { + expect(d).toBe("d"); +}); + +it("should load an ES module from dll (star export)", function () { + expect(x1).toBe(123); + expect(x2).toBe(123); + expect(y1).toBe(456); + expect(y2).toBe(456); +}); + +it("should load a module with loader applied", function () { + expect(require("../0-create-dll/g.abc.js")).toBe("number"); +}); + +it("should give modules the correct ids", function () { + expect( + Object.keys(__webpack_modules__) + .filter(m => !m.startsWith("../..")) + .sort() + ).toEqual([ + "../0-create-dll/a.js", + "../0-create-dll/b.js", + "../0-create-dll/d.js", + "../0-create-dll/e.js", + "../0-create-dll/e1.js", + "../0-create-dll/e2.js", + "../0-create-dll/ee1.js", + "../0-create-dll/ee2.js", + "../0-create-dll/f.jsx", + "../0-create-dll/g.abc.js", + "../0-create-dll/h.js", + "../0-create-dll/ha.js", + "./e.js", + "./index.js", + "dll-reference ../0-create-dll/dll.js" + ]); +}); + +it("should not crash on side-effect-free modules", function () { + expect(B).toBe("B"); +}); + +it("should be able to reference side-effect-free reexport-only module", function () { + expect(A).toBe("A"); +}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/rspack.config.js new file mode 100644 index 00000000000..461b7443b26 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/rspack.config.js @@ -0,0 +1,37 @@ +var path = require("path"); +var rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + entry: './index.js', + module: { + rules: [ + { + oneOf: [ + { + test: /\.abc\.js$/, + loader: "../0-create-dll/g-loader.js", + options: { + test: 1 + } + } + ] + } + ] + }, + optimization: { + moduleIds: "named", + concatenateModules: false, + }, + resolve: { + extensions: [".js", ".jsx"] + }, + plugins: [ + new rspack.DllReferencePlugin({ + manifest: require("../../../js/config/dll-plugin/manifest0.json"), // eslint-disable-line node/no-missing-require + name: "../0-create-dll/dll.js", + context: path.resolve(__dirname, "../0-create-dll"), + sourceType: "commonjs2", + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js new file mode 100644 index 00000000000..13ddc93dc9b --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js @@ -0,0 +1 @@ +module.exports = () => { return true } diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e.js new file mode 100644 index 00000000000..106e272113d --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e.js @@ -0,0 +1,2 @@ +export * from "../0-create-dll/e1"; +export * from "../0-create-dll/e2"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e1.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e1.js new file mode 100644 index 00000000000..f490fc4645b --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e1.js @@ -0,0 +1,2 @@ +export * from "dll/e1"; +export * from "dll/e2"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/index.js new file mode 100644 index 00000000000..d4fe59e07e7 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/index.js @@ -0,0 +1,29 @@ +import d from "../0-create-dll/d"; +import { x1, y2 } from "./e"; +import { x2, y1 } from "../0-create-dll/e"; + +it("should load a module from dll", function() { + expect(require("../0-create-dll/a")).toBe("a"); +}); + +it("should load an async module from dll", function(done) { + require("../0-create-dll/b")().then(function(c) { + expect(c).toEqual(nsObj({ default: "c" })); + done(); + }).catch(done); +}); + +it("should load an ES module from dll (default export)", function() { + expect(d).toBe("d"); +}); + +it("should load an ES module from dll (star export)", function() { + expect(x1).toBe(123); + expect(x2).toBe(123); + expect(y1).toBe(456); + expect(y2).toBe(456); +}); + +it("should load a module with loader applied", function() { + expect(require("../0-create-dll/g.abc.js")).toBe("number"); +}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/rspack.config.js new file mode 100644 index 00000000000..73538730cf4 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/rspack.config.js @@ -0,0 +1,32 @@ +var path = require("path"); +var webpack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + module: { + rules: [ + { + oneOf: [ + { + test: /\.abc\.js$/, + loader: "../0-create-dll/g-loader.js", + options: { + test: 1 + } + } + ] + } + ] + }, + optimization: { + moduleIds: "hashed" + }, + plugins: [ + new webpack.DllReferencePlugin({ + manifest: require("../../../js/config/dll-plugin/manifest0.json"), // eslint-disable-line node/no-missing-require + name: "../0-create-dll/dll.js", + context: path.resolve(__dirname, "../0-create-dll"), + sourceType: "commonjs2" + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js new file mode 100644 index 00000000000..3be456dcd23 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js @@ -0,0 +1 @@ +module.exports = () => {return false} \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/warnings.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/warnings.js new file mode 100644 index 00000000000..5d0640d1c37 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/warnings.js @@ -0,0 +1,3 @@ +module.exports = [ + [/hashed/, /deprecated/] +]; From 1ac86e96b305f9b6a5d7ca6d818a10303cf6e593 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Thu, 31 Oct 2024 17:44:46 +0800 Subject: [PATCH 04/22] refactor: improve delegated_module code --- .../src/dll_reference/delegated_module.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs index e3210f467a4..9c25ee2dcd0 100644 --- a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs @@ -44,7 +44,6 @@ impl DelegatedModule { ) -> Self { Self { source_request, - // TODO: remove the clone request: data.id.clone(), delegation_type, user_request, @@ -147,8 +146,10 @@ impl Module for DelegatedModule { ) ); - // TODO: get request - let request = self.request.as_ref().expect("TODO: it should have request"); + let request = self + .request + .as_ref() + .expect("manifest content should have `id`."); match self.delegation_type.as_ref() { "require" => { From e35cac8adf6d1f7006caab90e522c3d5f01cb576 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Fri, 1 Nov 2024 15:22:21 +0800 Subject: [PATCH 05/22] docs: add dll-plugin & dll-refernce-plugin docs --- .../chunk-loading/chunk-name/rspack.config.js | 1 - packages/rspack/src/lib/DllPlugin.ts | 7 +- website/docs/en/plugins/webpack/_meta.json | 4 +- .../docs/en/plugins/webpack/dll-plugin.mdx | 98 +++++++++++ .../plugins/webpack/dll-reference-plugin.mdx | 166 ++++++++++++++++++ 5 files changed, 272 insertions(+), 4 deletions(-) create mode 100644 website/docs/en/plugins/webpack/dll-plugin.mdx create mode 100644 website/docs/en/plugins/webpack/dll-reference-plugin.mdx diff --git a/packages/rspack-test-tools/tests/configCases/chunk-loading/chunk-name/rspack.config.js b/packages/rspack-test-tools/tests/configCases/chunk-loading/chunk-name/rspack.config.js index 1ec9330dd53..2360cda109e 100644 --- a/packages/rspack-test-tools/tests/configCases/chunk-loading/chunk-name/rspack.config.js +++ b/packages/rspack-test-tools/tests/configCases/chunk-loading/chunk-name/rspack.config.js @@ -2,7 +2,6 @@ const { EntryPlugin } = require("@rspack/core"); const path = require("path"); /** @type {import("@rspack/core").Configuration} */ module.exports = { - entry: './index.js', plugins: [ new EntryPlugin(__dirname, path.resolve(__dirname, "./index.js"), { name: "HtmlWebpackPlugin_0-C:\\userCode\\x-project\\node_modules\\html-webpack-plugin\\lib\\loader.js!C:\\userCode\\x-project\\index.html", diff --git a/packages/rspack/src/lib/DllPlugin.ts b/packages/rspack/src/lib/DllPlugin.ts index d91744ca2ef..e0e710b20f5 100644 --- a/packages/rspack/src/lib/DllPlugin.ts +++ b/packages/rspack/src/lib/DllPlugin.ts @@ -22,7 +22,8 @@ export type DllPluginOptions = { context?: string; /** - * If true, only entry points will be exposed (default: true). + * If true, only entry points will be exposed. + * @default true */ entryOnly?: boolean; @@ -56,6 +57,8 @@ const dllPluginOptions = z.object({ type: z.string().optional() }) satisfies z.ZodType; +const DLL_PLUGIN_NAME = "DllPlugin"; + export class DllPlugin { private options: DllPluginOptions; @@ -68,7 +71,7 @@ export class DllPlugin { } apply(compiler: Compiler) { - compiler.hooks.entryOption.tap("DllPlugin", (context, entry) => { + compiler.hooks.entryOption.tap(DLL_PLUGIN_NAME, (context, entry) => { if (typeof entry === "function") { throw new Error( "DllPlugin doesn't support dynamic entry (function) yet" diff --git a/website/docs/en/plugins/webpack/_meta.json b/website/docs/en/plugins/webpack/_meta.json index bcdf3eca101..9fd64bf85e6 100644 --- a/website/docs/en/plugins/webpack/_meta.json +++ b/website/docs/en/plugins/webpack/_meta.json @@ -23,5 +23,7 @@ "javascript-modules-plugin", "internal-plugins", "no-emit-on-errors-plugin", - "context-replacement-plugin" + "context-replacement-plugin", + "dll-plugin", + "dll-reference-plugin" ] diff --git a/website/docs/en/plugins/webpack/dll-plugin.mdx b/website/docs/en/plugins/webpack/dll-plugin.mdx new file mode 100644 index 00000000000..94ec665b97c --- /dev/null +++ b/website/docs/en/plugins/webpack/dll-plugin.mdx @@ -0,0 +1,98 @@ +import { Table } from '@builtIns'; +import WebpackLicense from '@components/WebpackLicense'; + + + +# DllPlugin + +The `DllPlugin` is used in a separate rspack configuration exclusively to create a dll-only-bundle. + +## Options + +- **Type:** + +```ts +type DllPluginOptions = { + context?: string; + entryOnly?: boolean; + format?: boolean; + name?: string; + path: string; + type?: string; +}; +``` + + + +## Examples + +```js +new rspack.DllPlugin({ + path: path.resolve(__dirname, 'manifest.json'), + name: '[name]_dll_lib', +}); +``` + +The Plugin will create a `manifest.json` which is written to the given path. +It contains mappings from require and import requests to module ids. + +The `manifest.json` is used by the [DllReferencePlugin](/plugins/webpack/dll-reference-plugin) + +Combine this plugin with `output.library` options to expose the dll function. diff --git a/website/docs/en/plugins/webpack/dll-reference-plugin.mdx b/website/docs/en/plugins/webpack/dll-reference-plugin.mdx new file mode 100644 index 00000000000..81c4f0c28d7 --- /dev/null +++ b/website/docs/en/plugins/webpack/dll-reference-plugin.mdx @@ -0,0 +1,166 @@ +import { Table } from '@builtIns'; +import WebpackLicense from '@components/WebpackLicense'; + + + +# DllReferencePlugin + +The `DllReferencePlugin` is used be reference the dll-only-bundle to require pre-built dependencies. + +## Options + +- **Types:** + +```ts +type DllReferencePluginOptionsContent = { + /** + * Module info. + */ + [k: string]: { + /** + * Meta information about the module. + */ + buildMeta?: { + [k: string]: any; + }; + /** + * Information about the provided exports of the module. + */ + exports?: string[] | true; + /** + * Module ID. + */ + id: number | string; + }; +}; + +type DllReferencePluginOptionsManifest = { + /** + * The mappings from request to module info. + */ + content: DllReferencePluginOptionsContent; + /** + * The name where the dll is exposed (external name). + */ + name?: string; + /** + * The type how the dll is exposed (external type). + */ + type?: DllReferencePluginOptionsSourceType; +}; + +/** + * The type how the dll is exposed (external type). + */ +type DllReferencePluginOptionsSourceType = + | 'var' + | 'assign' + | 'this' + | 'window' + | 'global' + | 'commonjs' + | 'commonjs2' + | 'commonjs-module' + | 'amd' + | 'amd-require' + | 'umd' + | 'umd2' + | 'jsonp' + | 'system'; + +type DllReferencePluginOptions = + | { + /** + * Context of requests in the manifest (or content property) as absolute path. + */ + context?: string; + /** + * Extensions used to resolve modules in the dll bundle (only used when using 'scope'). + */ + extensions?: string[]; + /** + * An object containing content and name or a string to the absolute path of the JSON manifest to be loaded upon compilation. + */ + manifest: string | DllReferencePluginOptionsManifest; + /** + * The name where the dll is exposed (external name, defaults to manifest.name). + */ + name?: string; + /** + * Prefix which is used for accessing the content of the dll. + */ + scope?: string; + /** + * How the dll is exposed (libraryTarget, defaults to manifest.type). + */ + sourceType?: DllReferencePluginOptionsSourceType; + /** + * The way how the export of the dll bundle is used. + */ + type?: 'require' | 'object'; + } + | { + /** + * The mappings from request to module info. + */ + content: DllReferencePluginOptionsContent; + /** + * Context of requests in the manifest (or content property) as absolute path. + */ + context?: string; + /** + * Extensions used to resolve modules in the dll bundle (only used when using 'scope'). + */ + extensions?: string[]; + /** + * The name where the dll is exposed (external name). + */ + name: string; + /** + * Prefix which is used for accessing the content of the dll. + */ + scope?: string; + /** + * How the dll is exposed (libraryTarget). + */ + sourceType?: DllReferencePluginOptionsSourceType; + /** + * The way how the export of the dll bundle is used. + */ + type?: 'require' | 'object'; + }; +``` + +This plugin references a dll manifest file to map dependency names to module ids, then require them as needed. + +## Examples + +### Basics Example + +```js +new rspack.DllReferencePlugin({ + // Manifest should be generated by DllPlugin + manifest: require('../lib/manifest.json'), + + name: '[name]_dll_lib', +}); +``` + +Application require dependencies will reference to pre-built using `DllPlugin`. + +### With Scope + +The content of the dll is accessible under a module prefix when setted scope. + +```js +new rspack.DllReferencePlugin({ + // Manifest should be generated by DllPlugin + manifest: require('../lib/manifest.json'), + + name: '[name]_dll_lib', + + scope: 'xyz', +}); +``` + +Access via `require('xzy/abc')`, you can require `abc` from another pre-built lib. From 68beb1a4131c1d4da877bec8f50382db173316c2 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Tue, 5 Nov 2024 17:06:06 +0800 Subject: [PATCH 06/22] fix: flagAllModuleAsUsedPlugin add_extra_reason --- crates/node_binding/binding.d.ts | 4 ++ .../src/options/raw_builtins/mod.rs | 5 +- .../src/options/raw_builtins/raw_dll.rs | 6 ++ .../src/module_graph/connection.rs | 19 ++++- crates/rspack_core/src/module_graph/mod.rs | 17 +++++ .../src/dll_reference/delegated_plugin.rs | 4 +- ....rs => flag_all_modules_as_used_plugin.rs} | 46 ++++++++++--- crates/rspack_plugin_dll/src/lib.rs | 4 +- .../src/plugin/module_concatenation_plugin.rs | 26 ++++--- .../dll-plugin-entry/0-create-dll/dep.js | 5 -- .../dll-plugin-entry/0-create-dll/dep2.js | 1 - .../dll-plugin-entry/0-create-dll/index.js | 4 -- .../0-create-dll/rspack.config.js | 20 ------ .../0-create-dll/test.config.js | 1 - .../0-create-dll/test.filter.js | 1 - .../dll-plugin-entry/1-use-dll/index.js | 25 ------- .../1-use-dll/rspack.config.js | 17 ----- .../dll-plugin-entry/1-use-dll/test.filter.js | 1 - .../2-error-non-entry/errors.js | 1 - .../2-error-non-entry/index.js | 1 - .../2-error-non-entry/rspack.config.js | 16 ----- .../2-error-non-entry/test.filter.js | 1 - .../dll-plugin-format/0-create-dll/dep.js | 1 - .../dll-plugin-format/0-create-dll/index.js | 4 -- .../0-create-dll/rspack.config.js | 24 ------- .../0-create-dll/test.config.js | 1 - .../0-create-dll/test.filter.js | 1 - .../dll-plugin-format/0-create-dll/utility.js | 7 -- .../0-create-dll/dependency.js | 3 - .../0-create-dll/dependency2.js | 3 - .../0-create-dll/index.js | 3 - .../0-create-dll/module.js | 12 ---- .../0-create-dll/rspack.config.js | 34 --------- .../0-create-dll/test.config.js | 1 - .../0-create-dll/test.filter.js | 1 - .../1-use-dll/index.js | 9 --- .../1-use-dll/rspack.config.js | 13 ---- .../1-use-dll/test.filter.js | 1 - .../configCases/dll-plugin/0-create-dll/_d.js | 1 - .../configCases/dll-plugin/0-create-dll/_e.js | 3 - .../configCases/dll-plugin/0-create-dll/a.js | 1 - .../configCases/dll-plugin/0-create-dll/b.js | 3 - .../configCases/dll-plugin/0-create-dll/c.js | 1 - .../configCases/dll-plugin/0-create-dll/d.js | 1 - .../configCases/dll-plugin/0-create-dll/e.js | 4 -- .../configCases/dll-plugin/0-create-dll/e1.js | 3 - .../configCases/dll-plugin/0-create-dll/e2.js | 3 - .../dll-plugin/0-create-dll/ee1.js | 2 - .../dll-plugin/0-create-dll/ee2.js | 2 - .../configCases/dll-plugin/0-create-dll/f.jsx | 1 - .../dll-plugin/0-create-dll/g-loader.js | 4 -- .../dll-plugin/0-create-dll/g.abc.js | 1 - .../configCases/dll-plugin/0-create-dll/h.js | 1 - .../configCases/dll-plugin/0-create-dll/h1.js | 2 - .../configCases/dll-plugin/0-create-dll/ha.js | 1 - .../configCases/dll-plugin/0-create-dll/hb.js | 1 - .../dll-plugin/0-create-dll/rspack.config.js | 43 ------------ .../dll-plugin/0-create-dll/test.config.js | 1 - .../dll-plugin/0-create-dll/test.filter.js | 1 - .../dll-plugin/0-issue-10475/index.js | 5 -- .../dll-plugin/0-issue-10475/rspack.config.js | 23 ------- .../dll-plugin/0-issue-10475/test.config.js | 1 - .../dll-plugin/0-issue-10475/test.filter.js | 1 - .../dll-plugin/1-issue-10475/index.js | 3 - .../dll-plugin/1-issue-10475/rspack.config.js | 13 ---- .../dll-plugin/1-issue-10475/test.filter.js | 1 - .../configCases/dll-plugin/1-use-dll/e.js | 2 - .../configCases/dll-plugin/1-use-dll/index.js | 60 ---------------- .../dll-plugin/1-use-dll/rspack.config.js | 17 ----- .../dll-plugin/1-use-dll/test.filter.js | 1 - .../dll-plugin/2-use-dll-without-scope/e.js | 2 - .../2-use-dll-without-scope/index.js | 69 ------------------- .../2-use-dll-without-scope/rspack.config.js | 37 ---------- .../2-use-dll-without-scope/test.filter.js | 1 - .../dll-plugin/3-use-dll-with-hashid/e.js | 2 - .../dll-plugin/3-use-dll-with-hashid/e1.js | 2 - .../dll-plugin/3-use-dll-with-hashid/index.js | 29 -------- .../3-use-dll-with-hashid/rspack.config.js | 32 --------- .../3-use-dll-with-hashid/test.filter.js | 1 - .../3-use-dll-with-hashid/warnings.js | 3 - .../FlagAllModulesAsUsedPlugin.ts | 11 ++- packages/rspack/src/lib/DllPlugin.ts | 2 +- .../0-create-dll/test.filter.js | 1 - .../dll-plugin-entry/1-use-dll/test.filter.js | 1 - .../2-error-non-entry/test.filter.js | 1 - .../0-create-dll/test.filter.js | 1 - .../0-create-dll/test.filter.js | 1 - .../1-use-dll/test.filter.js | 1 - .../dll-plugin/0-create-dll/test.filter.js | 1 - .../node_modules/test-package/constants.js | 0 .../node_modules/test-package/index.js | 0 .../node_modules/test-package/package.json | 0 .../node_modules/test-package/someFunction.js | 0 .../test-package/working-constants.js | 0 .../dll-plugin/0-issue-10475/test.filter.js | 1 - .../dll-plugin/1-issue-10475/test.filter.js | 1 - .../dll-plugin/1-use-dll/test.filter.js | 1 - .../2-use-dll-without-scope/test.filter.js | 1 - .../3-use-dll-with-hashid/test.filter.js | 2 +- .../3-use-dll-with-hashid/webpack.config.js | 1 + 100 files changed, 111 insertions(+), 644 deletions(-) rename crates/rspack_plugin_dll/src/{flag_alll_modules_as_used_plugin.rs => flag_all_modules_as_used_plugin.rs} (52%) delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep2.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/errors.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/dep.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/utility.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency2.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/module.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_d.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_e.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/a.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/b.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/c.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/d.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e1.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e2.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee1.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee2.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/f.jsx delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g-loader.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g.abc.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h1.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ha.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/hb.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/e.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/e.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e1.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/index.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/rspack.config.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js delete mode 100644 packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/warnings.js delete mode 100644 tests/webpack-test/configCases/dll-plugin-entry/0-create-dll/test.filter.js delete mode 100644 tests/webpack-test/configCases/dll-plugin-entry/1-use-dll/test.filter.js delete mode 100644 tests/webpack-test/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js delete mode 100644 tests/webpack-test/configCases/dll-plugin-format/0-create-dll/test.filter.js delete mode 100644 tests/webpack-test/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js delete mode 100644 tests/webpack-test/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js delete mode 100644 tests/webpack-test/configCases/dll-plugin/0-create-dll/test.filter.js rename {packages/rspack-test-tools/tests => tests/webpack-test}/configCases/dll-plugin/0-issue-10475/node_modules/test-package/constants.js (100%) rename {packages/rspack-test-tools/tests => tests/webpack-test}/configCases/dll-plugin/0-issue-10475/node_modules/test-package/index.js (100%) rename {packages/rspack-test-tools/tests => tests/webpack-test}/configCases/dll-plugin/0-issue-10475/node_modules/test-package/package.json (100%) rename {packages/rspack-test-tools/tests => tests/webpack-test}/configCases/dll-plugin/0-issue-10475/node_modules/test-package/someFunction.js (100%) rename {packages/rspack-test-tools/tests => tests/webpack-test}/configCases/dll-plugin/0-issue-10475/node_modules/test-package/working-constants.js (100%) delete mode 100644 tests/webpack-test/configCases/dll-plugin/0-issue-10475/test.filter.js delete mode 100644 tests/webpack-test/configCases/dll-plugin/1-issue-10475/test.filter.js delete mode 100644 tests/webpack-test/configCases/dll-plugin/1-use-dll/test.filter.js delete mode 100644 tests/webpack-test/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 9edf1ee5dc4..10dbf4f7f02 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -1378,6 +1378,10 @@ export interface RawFallbackCacheGroupOptions { automaticNameDelimiter?: string } +export interface RawFlagAllModulesAsUsedPluginOptions { + explanation: string +} + export interface RawFuncUseCtx { resource?: string realResource?: string diff --git a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs index 88f878695e3..89fa520579b 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs @@ -16,7 +16,7 @@ mod raw_swc_js_minimizer; use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown}; use napi_derive::napi; -use raw_dll::RawDllReferencePluginAgencyOptions; +use raw_dll::{RawDllReferencePluginAgencyOptions, RawFlagAllModulesAsUsedPluginOptions}; use raw_lightning_css_minimizer::RawLightningCssMinimizerRspackPluginOptions; use rspack_binding_values::entry::JsEntryPluginOptions; use rspack_core::{BoxPlugin, Plugin, PluginExt}; @@ -541,7 +541,8 @@ impl BuiltinPlugin { plugins.push(LibManifestPlugin::new(options).boxed()); } BuiltinPluginName::FlagAllModulesAsUsedPlugin => { - plugins.push(FlagAllModulesAsUsedPlugin::default().boxed()) + let raw_options = downcast_into::(self.options)?; + plugins.push(FlagAllModulesAsUsedPlugin::new(raw_options.explanation).boxed()) } BuiltinPluginName::DllReferenceAgencyPlugin => { let raw_options = downcast_into::(self.options)?; diff --git a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs index a159b15ed76..1fae3cdf227 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs @@ -99,3 +99,9 @@ impl From for DllReferencePluginAgencyOption } } } + +#[derive(Debug)] +#[napi(object)] +pub struct RawFlagAllModulesAsUsedPluginOptions { + pub explanation: String, +} diff --git a/crates/rspack_core/src/module_graph/connection.rs b/crates/rspack_core/src/module_graph/connection.rs index c86fca4c3a2..24be0190076 100644 --- a/crates/rspack_core/src/module_graph/connection.rs +++ b/crates/rspack_core/src/module_graph/connection.rs @@ -1,4 +1,6 @@ -use std::hash::Hash; +use std::{collections::HashSet, hash::Hash}; + +use itertools::Itertools; use crate::{DependencyId, ModuleGraph, ModuleIdentifier, RuntimeSpec}; @@ -14,6 +16,8 @@ pub struct ModuleGraphConnection { pub active: bool, pub conditional: bool, + + explanations: HashSet, } impl Hash for ModuleGraphConnection { @@ -43,6 +47,19 @@ impl ModuleGraphConnection { active, conditional, resolved_original_module_identifier: original_module_identifier, + explanations: Default::default(), + } + } + + pub fn add_explanation(&mut self, explanation: String) { + self.explanations.insert(explanation); + } + + pub fn explanation(&self) -> Option { + if self.explanations.is_empty() { + None + } else { + Some(self.explanations.iter().join(" ")) } } diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index f435426c83a..0e635b2238b 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -467,6 +467,23 @@ impl<'a> ModuleGraph<'a> { } } + pub fn add_extra_reason(&mut self, module_identifier: &ModuleIdentifier, explanation: String) { + let dependency_id = DependencyId::new(); + + let mut connection = + ModuleGraphConnection::new(dependency_id, None, module_identifier.clone(), true, false); + + connection.add_explanation(explanation); + + let mg = self + .module_graph_module_by_identifier_mut(module_identifier) + .expect("Should have moduleGraphModule"); + + mg.add_incoming_connection(dependency_id); + + self.add_connection(connection, None); + } + pub fn get_depth(&self, module_id: &ModuleIdentifier) -> Option { self .module_graph_module_by_identifier(module_id) diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs index 83d1fa4df16..3887f9ff235 100644 --- a/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs @@ -60,7 +60,7 @@ impl Plugin for DelegatedPlugin { .context .normal_module_factory_hooks .module - .tap(module::new(self)); + .tap(nmf_module::new(self)); Ok(()) } @@ -126,7 +126,7 @@ async fn factorize(&self, data: &mut ModuleFactoryCreateData) -> Result Self { + Self::new_inner(explanation) + } +} impl Plugin for FlagAllModulesAsUsedPlugin { fn name(&self) -> &'static str { @@ -22,6 +31,12 @@ impl Plugin for FlagAllModulesAsUsedPlugin { .optimize_dependencies .tap(optimize_dependencies::new(self)); + ctx + .context + .normal_module_factory_hooks + .module + .tap(nmf_module::new(self)); + Ok(()) } } @@ -43,15 +58,24 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result Result<()> { + // set all modules have effects. To avoid any module remove by tree shakeing. + // see: https://github.com/webpack/webpack/blob/4b4ca3bb53f36a5b8fc6bc1bd976ed7af161bd80/lib/FlagAllModulesAsUsedPlugin.js#L43-L47 + module.set_factory_meta(FactoryMeta { + side_effect_free: Some(false), + }); + + Ok(()) +} diff --git a/crates/rspack_plugin_dll/src/lib.rs b/crates/rspack_plugin_dll/src/lib.rs index 66edce75359..9e69f03d4c8 100644 --- a/crates/rspack_plugin_dll/src/lib.rs +++ b/crates/rspack_plugin_dll/src/lib.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; mod dll_entry; mod dll_reference; -mod flag_alll_modules_as_used_plugin; +mod flag_all_modules_as_used_plugin; mod lib_manifest_plugin; pub(crate) type DllManifestContent = HashMap; @@ -38,5 +38,5 @@ pub use dll_entry::dll_entry_plugin::{DllEntryPlugin, DllEntryPluginOptions}; pub use dll_reference::dll_reference_agency_plugin::{ DllReferenceAgencyPlugin, DllReferencePluginAgencyOptions, }; -pub use flag_alll_modules_as_used_plugin::FlagAllModulesAsUsedPlugin; +pub use flag_all_modules_as_used_plugin::FlagAllModulesAsUsedPlugin; pub use lib_manifest_plugin::{LibManifestPlugin, LibManifestPluginOptions}; 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 313598e4794..52fd4160fca 100644 --- a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs @@ -279,24 +279,22 @@ impl ModuleConcatenationPlugin { .filter(|&connection| connection.is_active(&module_graph, runtime)) .collect::>(); - // TODO: ADD module connection explanations if !active_non_modules_connections.is_empty() { let problem = { - // let importing_explanations: HashSet<_> = active_non_modules_connections - // .iter() - // .flat_map(|&c| c.explanation.as_ref()) - // .cloned() - // .collect(); - // let mut explanations: Vec<_> = importing_explanations.into_iter().collect(); - // explanations.sort(); + let importing_explanations = active_non_modules_connections + .iter() + .flat_map(|&c| c.explanation()) + .collect::>(); + let mut explanations: Vec<_> = importing_explanations.into_iter().collect(); + explanations.sort(); format!( - "Module {} is referenced", + "Module {} is referenced {}", module_readable_identifier, - // if !explanations.is_empty() { - // format!("by: {}", explanations.join(", ")) - // } else { - // "in an unsupported way".to_string() - // } + if !explanations.is_empty() { + format!("by: {}", explanations.join(", ")) + } else { + "in an unsupported way".to_string() + } ) }; let problem = Warning::Problem(problem); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep.js deleted file mode 100644 index bca818d4d63..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep.js +++ /dev/null @@ -1,5 +0,0 @@ -export function foo() { - console.log("foo"); -} - -export const bar = "bar"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep2.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep2.js deleted file mode 100644 index e7134e7006d..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/dep2.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = "foo"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/index.js deleted file mode 100644 index 80cdc870f74..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/index.js +++ /dev/null @@ -1,4 +0,0 @@ -export { bar } from "./dep"; -export default 42; - -require("./dep2"); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/rspack.config.js deleted file mode 100644 index 22151bc3c02..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/rspack.config.js +++ /dev/null @@ -1,20 +0,0 @@ -var path = require("path"); -var rspack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - entry: ["."], - output: { - filename: "dll.js", - chunkFilename: "[id].dll.js", - libraryTarget: "commonjs2" - }, - plugins: [ - new rspack.DllPlugin({ - path: path.resolve( - __dirname, - "../../../js/config/dll-plugin-entry/manifest0.json" - ) - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.config.js deleted file mode 100644 index 08ea6c319c8..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.config.js +++ /dev/null @@ -1 +0,0 @@ -exports.noTests = true; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.filter.js deleted file mode 100644 index 8e5c51af343..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/0-create-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/index.js deleted file mode 100644 index c9818abdef7..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/index.js +++ /dev/null @@ -1,25 +0,0 @@ -import Answer, { bar } from "dll/index"; - -it("should load a module from dll", function() { - expect(require("dll/index")).toEqual(nsObj({ bar: "bar", default: 42 })); -}); - -it("should load an ES module from dll (default export)", function() { - expect(Answer).toBe(42); -}); - -it("should load an ES module from dll (star export)", function() { - expect(bar).toBe("bar"); -}); - -it("should give modules the correct ids", function() { - expect( - Object.keys(__webpack_modules__) - .filter(m => !m.startsWith("../..")) - .sort() - ).toEqual([ - "./index.js", - "dll-reference ../0-create-dll/dll.js", - "dll/index.js" - ]); -}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/rspack.config.js deleted file mode 100644 index 53f0ebf8f04..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/rspack.config.js +++ /dev/null @@ -1,17 +0,0 @@ -var rspack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - entry: './index.js', - optimization: { - moduleIds: "named" - }, - plugins: [ - new rspack.DllReferencePlugin({ - manifest: require("../../../js/config/dll-plugin-entry/manifest0.json"), // eslint-disable-line node/no-missing-require - name: "../0-create-dll/dll.js", - scope: "dll", - sourceType: "commonjs2" - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/test.filter.js deleted file mode 100644 index 8e5c51af343..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/1-use-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/errors.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/errors.js deleted file mode 100644 index 857282ec937..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/errors.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = [[/Can't resolve 'dll\/dep2'/]]; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/index.js deleted file mode 100644 index 901f17038f8..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/index.js +++ /dev/null @@ -1 +0,0 @@ -require("dll/dep2"); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/rspack.config.js deleted file mode 100644 index c9ae2bf5352..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/rspack.config.js +++ /dev/null @@ -1,16 +0,0 @@ -var webpack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - optimization: { - moduleIds: "named" - }, - plugins: [ - new webpack.DllReferencePlugin({ - manifest: require("../../../js/config/dll-plugin-entry/manifest0.json"), // eslint-disable-line node/no-missing-require - name: "../0-create-dll/dll.js", - scope: "dll", - sourceType: "commonjs2" - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js deleted file mode 100644 index 8e5c51af343..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/dep.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/dep.js deleted file mode 100644 index e7134e7006d..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/dep.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = "foo"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/index.js deleted file mode 100644 index 59ef4a4cd38..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/index.js +++ /dev/null @@ -1,4 +0,0 @@ -export { add } from "./utility"; -export default "Format"; - -require("./dep"); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/rspack.config.js deleted file mode 100644 index 1ff1f56b10e..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/rspack.config.js +++ /dev/null @@ -1,24 +0,0 @@ -var path = require("path"); -var rspack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - entry: ["."], - resolve: { - extensions: [".js"] - }, - output: { - filename: "dll.js", - chunkFilename: "[id].dll.js", - libraryTarget: "commonjs2" - }, - plugins: [ - new rspack.DllPlugin({ - path: path.resolve( - __dirname, - "../../../js/config/dll-plugin-format/manifest0.json" - ), - format: true - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.config.js deleted file mode 100644 index 08ea6c319c8..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.config.js +++ /dev/null @@ -1 +0,0 @@ -exports.noTests = true; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.filter.js deleted file mode 100644 index 8e5c51af343..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/utility.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/utility.js deleted file mode 100644 index cbed57e2c2d..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-format/0-create-dll/utility.js +++ /dev/null @@ -1,7 +0,0 @@ -export function add(a, b) { - return a + b; -} - -export function diff(a, b) { - return a - b; -} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency.js deleted file mode 100644 index 1946cd07cb8..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency.js +++ /dev/null @@ -1,3 +0,0 @@ -export default function createB() { - return "b"; -} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency2.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency2.js deleted file mode 100644 index 0809fdd0180..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/dependency2.js +++ /dev/null @@ -1,3 +0,0 @@ -export default function createC() { - return "c"; -} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/index.js deleted file mode 100644 index 3a02fe46bff..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/index.js +++ /dev/null @@ -1,3 +0,0 @@ -import { a } from "./module"; - -export default a(); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/module.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/module.js deleted file mode 100644 index 257c5b97d93..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/module.js +++ /dev/null @@ -1,12 +0,0 @@ -import createB from "./dependency"; -import createC from "./dependency2"; - -export function a() { - return "a"; -} - -export { createB as b }; - -export function c() { - return createC(); -} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/rspack.config.js deleted file mode 100644 index ff80a8e1a6f..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/rspack.config.js +++ /dev/null @@ -1,34 +0,0 @@ -var path = require("path"); -var rspack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - entry: ["./index"], - output: { - filename: "dll.js", - chunkFilename: "[id].dll.js", - libraryTarget: "commonjs2" - }, - module: { - rules: [ - { - test: /0-create-dll.(module|dependency)/, - sideEffects: false - } - ] - }, - optimization: { - usedExports: true, - sideEffects: true, - concatenateModules: false - }, - plugins: [ - new rspack.DllPlugin({ - path: path.resolve( - __dirname, - "../../../js/config/dll-plugin-side-effects/manifest0.json", - ), - entryOnly: false - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.config.js deleted file mode 100644 index 08ea6c319c8..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.config.js +++ /dev/null @@ -1 +0,0 @@ -exports.noTests = true; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js deleted file mode 100644 index 8e5c51af343..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/index.js deleted file mode 100644 index db354f9f5d6..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/index.js +++ /dev/null @@ -1,9 +0,0 @@ -it("should include all exports and modules in the dll", function() { - const { a, b, c } = require("dll/module"); - expect(typeof a).toBe("function"); - expect(a()).toBe("a"); - expect(typeof b).toBe("function"); - expect(b()).toBe("b"); - expect(typeof c).toBe("function"); - expect(c()).toBe("c"); -}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/rspack.config.js deleted file mode 100644 index b8b98d05050..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/rspack.config.js +++ /dev/null @@ -1,13 +0,0 @@ -var rspack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - plugins: [ - new rspack.DllReferencePlugin({ - manifest: require("../../../js/config/dll-plugin-side-effects/manifest0.json"), // eslint-disable-line node/no-missing-require - name: "../0-create-dll/dll.js", - scope: "dll", - sourceType: "commonjs2" - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js deleted file mode 100644 index 8e5c51af343..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return true} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_d.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_d.js deleted file mode 100644 index d108c9a3722..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_d.js +++ /dev/null @@ -1 +0,0 @@ -import "./d"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_e.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_e.js deleted file mode 100644 index 586eb3aa06b..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/_e.js +++ /dev/null @@ -1,3 +0,0 @@ -import "./e1"; -import "./e2"; -import "./e"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/a.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/a.js deleted file mode 100644 index 6cd1d0075d4..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/a.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = "a"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/b.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/b.js deleted file mode 100644 index 58a90d8f841..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/b.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = function() { - return import("./c"); -} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/c.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/c.js deleted file mode 100644 index b2091de76d6..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/c.js +++ /dev/null @@ -1 +0,0 @@ -export default "c"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/d.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/d.js deleted file mode 100644 index 987d6d7e401..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/d.js +++ /dev/null @@ -1 +0,0 @@ -export default "d"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e.js deleted file mode 100644 index 9fbe80f85cf..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e.js +++ /dev/null @@ -1,4 +0,0 @@ -export * from "./e1"; -export * from "./ee2"; - -console.log.bind(console); // side effect to avoid removing module diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e1.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e1.js deleted file mode 100644 index 23709cd95ff..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e1.js +++ /dev/null @@ -1,3 +0,0 @@ -export * from "./ee1"; - -console.log.bind(console); // side effect to avoid removing module diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e2.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e2.js deleted file mode 100644 index 25612746b57..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/e2.js +++ /dev/null @@ -1,3 +0,0 @@ -export * from "./ee2"; - -console.log.bind(console); // side effect to avoid removing module diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee1.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee1.js deleted file mode 100644 index 359c69fe3e7..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee1.js +++ /dev/null @@ -1,2 +0,0 @@ -export var x1 = 123; -export var y1 = 456; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee2.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee2.js deleted file mode 100644 index 634e1a91947..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ee2.js +++ /dev/null @@ -1,2 +0,0 @@ -export var x2 = 123; -export var y2 = 456; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/f.jsx b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/f.jsx deleted file mode 100644 index 61445975b07..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/f.jsx +++ /dev/null @@ -1 +0,0 @@ -module.exports = 'f'; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g-loader.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g-loader.js deleted file mode 100644 index 3ae53b4dca3..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g-loader.js +++ /dev/null @@ -1,4 +0,0 @@ -/** @type {import("@rspack/core").LoaderDefinition} */ -module.exports = function (source) { - return source; -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g.abc.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g.abc.js deleted file mode 100644 index 483352ffbff..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/g.abc.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = typeof module.id; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h.js deleted file mode 100644 index 1fa89a4fb1c..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h.js +++ /dev/null @@ -1 +0,0 @@ -export { B } from "./h1.js"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h1.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h1.js deleted file mode 100644 index a392743d956..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/h1.js +++ /dev/null @@ -1,2 +0,0 @@ -export { A } from "./ha.js"; -export { B } from "./hb.js"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ha.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ha.js deleted file mode 100644 index 6506d8d86b2..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/ha.js +++ /dev/null @@ -1 +0,0 @@ -export const A = "A"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/hb.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/hb.js deleted file mode 100644 index f3c1f2c5d79..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/hb.js +++ /dev/null @@ -1 +0,0 @@ -export const B = "B"; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/rspack.config.js deleted file mode 100644 index ac8d019e125..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/rspack.config.js +++ /dev/null @@ -1,43 +0,0 @@ -var path = require("path"); -var webpack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - entry: ["./a", "./b", "./_d", "./_e", "./f", "./g.abc", "./h"], - resolve: { - extensions: [".js", ".jsx"] - }, - output: { - filename: "dll.js", - chunkFilename: "[id].dll.js", - libraryTarget: "commonjs2" - }, - module: { - rules: [ - { - test: /\.abc\.js$/, - loader: "./g-loader.js", - options: { - test: 1 - } - }, - { - test: /0-create-dll.h/, - sideEffects: false - } - ] - }, - optimization: { - usedExports: true, - sideEffects: true - }, - plugins: [ - new webpack.DllPlugin({ - path: path.resolve( - __dirname, - "../../../js/config/dll-plugin/manifest0.json" - ), - entryOnly: false - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.config.js deleted file mode 100644 index 08ea6c319c8..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.config.js +++ /dev/null @@ -1 +0,0 @@ -exports.noTests = true; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.filter.js deleted file mode 100644 index 13ddc93dc9b..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-create-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => { return true } diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/index.js deleted file mode 100644 index a88d1863e69..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/index.js +++ /dev/null @@ -1,5 +0,0 @@ -import { constants } from "test-package"; - -var x = constants; - -export {x} diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/rspack.config.js deleted file mode 100644 index f79631d2360..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/rspack.config.js +++ /dev/null @@ -1,23 +0,0 @@ -var path = require("path"); -var rspack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - entry: ["./index.js"], - output: { - filename: "dll.js", - chunkFilename: "[id].dll.js", - library: { - type: 'commonjs2', - } - }, - - plugins: [ - new rspack.DllPlugin({ - path: path.resolve( - __dirname, - "../../../js/config/dll-plugin/issue-10475.json" - ) - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.config.js deleted file mode 100644 index 08ea6c319c8..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.config.js +++ /dev/null @@ -1 +0,0 @@ -exports.noTests = true; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.filter.js deleted file mode 100644 index 13ddc93dc9b..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => { return true } diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/index.js deleted file mode 100644 index ae5367529b1..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/index.js +++ /dev/null @@ -1,3 +0,0 @@ -it("should have all modules", () => { - require("dll/index.js"); -}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/rspack.config.js deleted file mode 100644 index ec154d0c098..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/rspack.config.js +++ /dev/null @@ -1,13 +0,0 @@ -var rspack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - plugins: [ - new rspack.DllReferencePlugin({ - manifest: require("../../../js/config/dll-plugin/issue-10475.json"), // eslint-disable-line node/no-missing-require - name: "../0-issue-10475/dll.js", - scope: "dll", - sourceType: "commonjs2", - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/test.filter.js deleted file mode 100644 index 13ddc93dc9b..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-issue-10475/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => { return true } diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/e.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/e.js deleted file mode 100644 index f490fc4645b..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/e.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from "dll/e1"; -export * from "dll/e2"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/index.js deleted file mode 100644 index 18431f91483..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/index.js +++ /dev/null @@ -1,60 +0,0 @@ -import d from "dll/d"; -import { x1, y2 } from "./e"; -import { x2, y1 } from "dll/e"; -import { B } from "dll/h"; - -it("should load a module from dll", function() { - expect(require("dll/a")).toBe("a"); -}); - -it("should load a module of non-default type without extension from dll", function() { - expect(require("dll/f")).toBe("f"); -}); - -it("should load an async module from dll", function(done) { - require("dll/b")() - .then(function(c) { - expect(c).toEqual(nsObj({ default: "c" })); - done(); - }) - .catch(done); -}); - -it("should load an ES module from dll (default export)", function() { - expect(d).toBe("d"); -}); - -it("should load an ES module from dll (star export)", function() { - expect(x1).toBe(123); - expect(x2).toBe(123); - expect(y1).toBe(456); - expect(y2).toBe(456); -}); - -it("should load a module with loader applied", function() { - expect(require("dll/g.abc.js")).toBe("number"); -}); - -it("should give modules the correct ids", function() { - expect( - Object.keys(__webpack_modules__) - .filter(m => !m.startsWith("../..")) - .sort() - ).toEqual([ - "./index.js", - "dll-reference ../0-create-dll/dll.js", - "dll/a.js", - "dll/b.js", - "dll/d.js", - "dll/e.js", - "dll/e1.js", - "dll/e2.js", - "dll/f.jsx", - "dll/g.abc.js", - "dll/h.js" - ]); -}); - -it("should not crash on side-effect-free modules", function() { - expect(B).toBe("B"); -}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/rspack.config.js deleted file mode 100644 index e3b0daffa9c..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/rspack.config.js +++ /dev/null @@ -1,17 +0,0 @@ -var rspack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - optimization: { - moduleIds: "named" - }, - plugins: [ - new rspack.DllReferencePlugin({ - manifest: require("../../../js/config/dll-plugin/manifest0.json"), // eslint-disable-line node/no-missing-require - name: "../0-create-dll/dll.js", - scope: "dll", - sourceType: "commonjs2", - extensions: [".js", ".jsx"] - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/test.filter.js deleted file mode 100644 index 687e4d88602..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/1-use-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => { return false } diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/e.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/e.js deleted file mode 100644 index 106e272113d..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/e.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from "../0-create-dll/e1"; -export * from "../0-create-dll/e2"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/index.js deleted file mode 100644 index 977c65d2aa5..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/index.js +++ /dev/null @@ -1,69 +0,0 @@ -import d from "../0-create-dll/d"; -import { x1, y2 } from "./e"; -import { x2, y1 } from "../0-create-dll/e"; -import { B } from "../0-create-dll/h"; -import { A } from "../0-create-dll/h1"; - -it("should load a module from dll", function () { - expect(require("../0-create-dll/a")).toBe("a"); -}); - -it("should load a module of non-default type without extension from dll", function () { - expect(require("../0-create-dll/f")).toBe("f"); -}); - -it("should load an async module from dll", function (done) { - require("../0-create-dll/b")() - .then(function (c) { - expect(c).toEqual(nsObj({ default: "c" })); - done(); - }) - .catch(done); -}); - -it("should load an ES module from dll (default export)", function () { - expect(d).toBe("d"); -}); - -it("should load an ES module from dll (star export)", function () { - expect(x1).toBe(123); - expect(x2).toBe(123); - expect(y1).toBe(456); - expect(y2).toBe(456); -}); - -it("should load a module with loader applied", function () { - expect(require("../0-create-dll/g.abc.js")).toBe("number"); -}); - -it("should give modules the correct ids", function () { - expect( - Object.keys(__webpack_modules__) - .filter(m => !m.startsWith("../..")) - .sort() - ).toEqual([ - "../0-create-dll/a.js", - "../0-create-dll/b.js", - "../0-create-dll/d.js", - "../0-create-dll/e.js", - "../0-create-dll/e1.js", - "../0-create-dll/e2.js", - "../0-create-dll/ee1.js", - "../0-create-dll/ee2.js", - "../0-create-dll/f.jsx", - "../0-create-dll/g.abc.js", - "../0-create-dll/h.js", - "../0-create-dll/ha.js", - "./e.js", - "./index.js", - "dll-reference ../0-create-dll/dll.js" - ]); -}); - -it("should not crash on side-effect-free modules", function () { - expect(B).toBe("B"); -}); - -it("should be able to reference side-effect-free reexport-only module", function () { - expect(A).toBe("A"); -}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/rspack.config.js deleted file mode 100644 index 461b7443b26..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/rspack.config.js +++ /dev/null @@ -1,37 +0,0 @@ -var path = require("path"); -var rspack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - entry: './index.js', - module: { - rules: [ - { - oneOf: [ - { - test: /\.abc\.js$/, - loader: "../0-create-dll/g-loader.js", - options: { - test: 1 - } - } - ] - } - ] - }, - optimization: { - moduleIds: "named", - concatenateModules: false, - }, - resolve: { - extensions: [".js", ".jsx"] - }, - plugins: [ - new rspack.DllReferencePlugin({ - manifest: require("../../../js/config/dll-plugin/manifest0.json"), // eslint-disable-line node/no-missing-require - name: "../0-create-dll/dll.js", - context: path.resolve(__dirname, "../0-create-dll"), - sourceType: "commonjs2", - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js deleted file mode 100644 index 13ddc93dc9b..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => { return true } diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e.js deleted file mode 100644 index 106e272113d..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from "../0-create-dll/e1"; -export * from "../0-create-dll/e2"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e1.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e1.js deleted file mode 100644 index f490fc4645b..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/e1.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from "dll/e1"; -export * from "dll/e2"; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/index.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/index.js deleted file mode 100644 index d4fe59e07e7..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/index.js +++ /dev/null @@ -1,29 +0,0 @@ -import d from "../0-create-dll/d"; -import { x1, y2 } from "./e"; -import { x2, y1 } from "../0-create-dll/e"; - -it("should load a module from dll", function() { - expect(require("../0-create-dll/a")).toBe("a"); -}); - -it("should load an async module from dll", function(done) { - require("../0-create-dll/b")().then(function(c) { - expect(c).toEqual(nsObj({ default: "c" })); - done(); - }).catch(done); -}); - -it("should load an ES module from dll (default export)", function() { - expect(d).toBe("d"); -}); - -it("should load an ES module from dll (star export)", function() { - expect(x1).toBe(123); - expect(x2).toBe(123); - expect(y1).toBe(456); - expect(y2).toBe(456); -}); - -it("should load a module with loader applied", function() { - expect(require("../0-create-dll/g.abc.js")).toBe("number"); -}); diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/rspack.config.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/rspack.config.js deleted file mode 100644 index 73538730cf4..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/rspack.config.js +++ /dev/null @@ -1,32 +0,0 @@ -var path = require("path"); -var webpack = require("@rspack/core"); - -/** @type {import("@rspack/core").Configuration} */ -module.exports = { - module: { - rules: [ - { - oneOf: [ - { - test: /\.abc\.js$/, - loader: "../0-create-dll/g-loader.js", - options: { - test: 1 - } - } - ] - } - ] - }, - optimization: { - moduleIds: "hashed" - }, - plugins: [ - new webpack.DllReferencePlugin({ - manifest: require("../../../js/config/dll-plugin/manifest0.json"), // eslint-disable-line node/no-missing-require - name: "../0-create-dll/dll.js", - context: path.resolve(__dirname, "../0-create-dll"), - sourceType: "commonjs2" - }) - ] -}; diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/warnings.js b/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/warnings.js deleted file mode 100644 index 5d0640d1c37..00000000000 --- a/packages/rspack-test-tools/tests/configCases/dll-plugin/3-use-dll-with-hashid/warnings.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = [ - [/hashed/, /deprecated/] -]; diff --git a/packages/rspack/src/builtin-plugin/FlagAllModulesAsUsedPlugin.ts b/packages/rspack/src/builtin-plugin/FlagAllModulesAsUsedPlugin.ts index d4f6f2a0f8a..897b1c8c2cb 100644 --- a/packages/rspack/src/builtin-plugin/FlagAllModulesAsUsedPlugin.ts +++ b/packages/rspack/src/builtin-plugin/FlagAllModulesAsUsedPlugin.ts @@ -1,7 +1,14 @@ -import { BuiltinPluginName } from "@rspack/binding"; +import { + BuiltinPluginName, + type RawFlagAllModulesAsUsedPluginOptions +} from "@rspack/binding"; import { create } from "./base"; export const FlagAllModulesAsUsedPlugin = create( BuiltinPluginName.FlagAllModulesAsUsedPlugin, - (): undefined => {} + (explanation: string): RawFlagAllModulesAsUsedPluginOptions => { + return { + explanation + }; + } ); diff --git a/packages/rspack/src/lib/DllPlugin.ts b/packages/rspack/src/lib/DllPlugin.ts index e0e710b20f5..1ec0f480137 100644 --- a/packages/rspack/src/lib/DllPlugin.ts +++ b/packages/rspack/src/lib/DllPlugin.ts @@ -93,7 +93,7 @@ export class DllPlugin { new LibManifestPlugin(this.options).apply(compiler); if (!this.options.entryOnly) { - new FlagAllModulesAsUsedPlugin().apply(compiler); + new FlagAllModulesAsUsedPlugin("DllPlugin").apply(compiler); } } } diff --git a/tests/webpack-test/configCases/dll-plugin-entry/0-create-dll/test.filter.js b/tests/webpack-test/configCases/dll-plugin-entry/0-create-dll/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin-entry/0-create-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/tests/webpack-test/configCases/dll-plugin-entry/1-use-dll/test.filter.js b/tests/webpack-test/configCases/dll-plugin-entry/1-use-dll/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin-entry/1-use-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/tests/webpack-test/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js b/tests/webpack-test/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin-entry/2-error-non-entry/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/tests/webpack-test/configCases/dll-plugin-format/0-create-dll/test.filter.js b/tests/webpack-test/configCases/dll-plugin-format/0-create-dll/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin-format/0-create-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/tests/webpack-test/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js b/tests/webpack-test/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin-side-effects/0-create-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/tests/webpack-test/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js b/tests/webpack-test/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin-side-effects/1-use-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/tests/webpack-test/configCases/dll-plugin/0-create-dll/test.filter.js b/tests/webpack-test/configCases/dll-plugin/0-create-dll/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin/0-create-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/constants.js b/tests/webpack-test/configCases/dll-plugin/0-issue-10475/node_modules/test-package/constants.js similarity index 100% rename from packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/constants.js rename to tests/webpack-test/configCases/dll-plugin/0-issue-10475/node_modules/test-package/constants.js diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/index.js b/tests/webpack-test/configCases/dll-plugin/0-issue-10475/node_modules/test-package/index.js similarity index 100% rename from packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/index.js rename to tests/webpack-test/configCases/dll-plugin/0-issue-10475/node_modules/test-package/index.js diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/package.json b/tests/webpack-test/configCases/dll-plugin/0-issue-10475/node_modules/test-package/package.json similarity index 100% rename from packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/package.json rename to tests/webpack-test/configCases/dll-plugin/0-issue-10475/node_modules/test-package/package.json diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/someFunction.js b/tests/webpack-test/configCases/dll-plugin/0-issue-10475/node_modules/test-package/someFunction.js similarity index 100% rename from packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/someFunction.js rename to tests/webpack-test/configCases/dll-plugin/0-issue-10475/node_modules/test-package/someFunction.js diff --git a/packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/working-constants.js b/tests/webpack-test/configCases/dll-plugin/0-issue-10475/node_modules/test-package/working-constants.js similarity index 100% rename from packages/rspack-test-tools/tests/configCases/dll-plugin/0-issue-10475/node_modules/test-package/working-constants.js rename to tests/webpack-test/configCases/dll-plugin/0-issue-10475/node_modules/test-package/working-constants.js diff --git a/tests/webpack-test/configCases/dll-plugin/0-issue-10475/test.filter.js b/tests/webpack-test/configCases/dll-plugin/0-issue-10475/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin/0-issue-10475/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/tests/webpack-test/configCases/dll-plugin/1-issue-10475/test.filter.js b/tests/webpack-test/configCases/dll-plugin/1-issue-10475/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin/1-issue-10475/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/tests/webpack-test/configCases/dll-plugin/1-use-dll/test.filter.js b/tests/webpack-test/configCases/dll-plugin/1-use-dll/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin/1-use-dll/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/tests/webpack-test/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js b/tests/webpack-test/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js deleted file mode 100644 index 3be456dcd23..00000000000 --- a/tests/webpack-test/configCases/dll-plugin/2-use-dll-without-scope/test.filter.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = () => {return false} \ No newline at end of file diff --git a/tests/webpack-test/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js b/tests/webpack-test/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js index 3be456dcd23..687e4d88602 100644 --- a/tests/webpack-test/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js +++ b/tests/webpack-test/configCases/dll-plugin/3-use-dll-with-hashid/test.filter.js @@ -1 +1 @@ -module.exports = () => {return false} \ No newline at end of file +module.exports = () => { return false } diff --git a/tests/webpack-test/configCases/dll-plugin/3-use-dll-with-hashid/webpack.config.js b/tests/webpack-test/configCases/dll-plugin/3-use-dll-with-hashid/webpack.config.js index 73538730cf4..6954b4715c3 100644 --- a/tests/webpack-test/configCases/dll-plugin/3-use-dll-with-hashid/webpack.config.js +++ b/tests/webpack-test/configCases/dll-plugin/3-use-dll-with-hashid/webpack.config.js @@ -19,6 +19,7 @@ module.exports = { ] }, optimization: { + // TODO: moduleIds: "hashed" is not implements moduleIds: "hashed" }, plugins: [ From 772f5c1b1e7a9544133177c3b9b00f3da43d613a Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Tue, 5 Nov 2024 17:24:19 +0800 Subject: [PATCH 07/22] chore: rename some vars --- crates/node_binding/binding.d.ts | 2 +- .../src/options/raw_builtins/mod.rs | 4 ++-- .../src/options/raw_builtins/raw_dll.rs | 10 +++++----- crates/rspack_core/src/module.rs | 2 +- .../src/dll_reference/dll_reference_agency_plugin.rs | 6 +++--- crates/rspack_plugin_dll/src/lib.rs | 2 +- .../src/builtin-plugin/DllReferenceAgencyPlugin.ts | 10 +++++----- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 10dbf4f7f02..e37dc8be6ca 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -1289,7 +1289,7 @@ export interface RawDllEntyPluginOptions { name: string } -export interface RawDllReferencePluginAgencyOptions { +export interface RawDllReferenceAgencyPluginOptions { context?: string name?: string extensions: Array diff --git a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs index 89fa520579b..8fa9f2df63e 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs @@ -16,7 +16,7 @@ mod raw_swc_js_minimizer; use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown}; use napi_derive::napi; -use raw_dll::{RawDllReferencePluginAgencyOptions, RawFlagAllModulesAsUsedPluginOptions}; +use raw_dll::{RawDllReferenceAgencyPluginOptions, RawFlagAllModulesAsUsedPluginOptions}; use raw_lightning_css_minimizer::RawLightningCssMinimizerRspackPluginOptions; use rspack_binding_values::entry::JsEntryPluginOptions; use rspack_core::{BoxPlugin, Plugin, PluginExt}; @@ -545,7 +545,7 @@ impl BuiltinPlugin { plugins.push(FlagAllModulesAsUsedPlugin::new(raw_options.explanation).boxed()) } BuiltinPluginName::DllReferenceAgencyPlugin => { - let raw_options = downcast_into::(self.options)?; + let raw_options = downcast_into::(self.options)?; let options = raw_options.into(); plugins.push(DllReferenceAgencyPlugin::new(options).boxed()); } diff --git a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs index 1fae3cdf227..21b009799ed 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs @@ -1,7 +1,7 @@ use napi_derive::napi; use rspack_binding_values::JsFilename; use rspack_plugin_dll::{ - DllEntryPluginOptions, DllReferencePluginAgencyOptions, LibManifestPluginOptions, + DllEntryPluginOptions, DllReferenceAgencyPluginOptions, LibManifestPluginOptions, }; #[derive(Debug)] @@ -63,7 +63,7 @@ impl From for LibManifestPluginOptions { #[derive(Debug)] #[napi(object)] -pub struct RawDllReferencePluginAgencyOptions { +pub struct RawDllReferenceAgencyPluginOptions { pub context: Option, pub name: Option, pub extensions: Vec, @@ -74,9 +74,9 @@ pub struct RawDllReferencePluginAgencyOptions { pub manifest: Option, } -impl From for DllReferencePluginAgencyOptions { - fn from(value: RawDllReferencePluginAgencyOptions) -> Self { - let RawDllReferencePluginAgencyOptions { +impl From for DllReferenceAgencyPluginOptions { + fn from(value: RawDllReferenceAgencyPluginOptions) -> Self { + let RawDllReferenceAgencyPluginOptions { context, name, extensions, diff --git a/crates/rspack_core/src/module.rs b/crates/rspack_core/src/module.rs index 6e290cef26e..7dfbb0cff3c 100644 --- a/crates/rspack_core/src/module.rs +++ b/crates/rspack_core/src/module.rs @@ -89,7 +89,7 @@ pub enum BuildMetaExportsType { Dynamic, } -#[derive(Debug, Clone, Copy, Hash, Deserialize)] +#[derive(Debug, Clone, Copy, Hash)] pub enum ExportsType { DefaultOnly, Namespace, diff --git a/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs b/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs index 5cde49c1199..8f947d8e88a 100644 --- a/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs +++ b/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs @@ -11,7 +11,7 @@ use super::delegated_plugin::{DelegatedPlugin, DelegatedPluginOptions}; use crate::{DllManifest, DllManifestContent}; #[derive(Debug, Clone)] -pub struct DllReferencePluginAgencyOptions { +pub struct DllReferenceAgencyPluginOptions { pub context: Option, pub name: Option, pub content: Option, @@ -25,11 +25,11 @@ pub struct DllReferencePluginAgencyOptions { #[plugin] #[derive(Debug)] pub struct DllReferenceAgencyPlugin { - options: DllReferencePluginAgencyOptions, + options: DllReferenceAgencyPluginOptions, } impl DllReferenceAgencyPlugin { - pub fn new(options: DllReferencePluginAgencyOptions) -> Self { + pub fn new(options: DllReferenceAgencyPluginOptions) -> Self { Self::new_inner(options) } } diff --git a/crates/rspack_plugin_dll/src/lib.rs b/crates/rspack_plugin_dll/src/lib.rs index 9e69f03d4c8..2d54982be7a 100644 --- a/crates/rspack_plugin_dll/src/lib.rs +++ b/crates/rspack_plugin_dll/src/lib.rs @@ -36,7 +36,7 @@ pub(crate) struct DllManifest { pub use dll_entry::dll_entry_plugin::{DllEntryPlugin, DllEntryPluginOptions}; pub use dll_reference::dll_reference_agency_plugin::{ - DllReferenceAgencyPlugin, DllReferencePluginAgencyOptions, + DllReferenceAgencyPlugin, DllReferenceAgencyPluginOptions, }; pub use flag_all_modules_as_used_plugin::FlagAllModulesAsUsedPlugin; pub use lib_manifest_plugin::{LibManifestPlugin, LibManifestPluginOptions}; diff --git a/packages/rspack/src/builtin-plugin/DllReferenceAgencyPlugin.ts b/packages/rspack/src/builtin-plugin/DllReferenceAgencyPlugin.ts index 80440ab9b0a..ed7750eb9d8 100644 --- a/packages/rspack/src/builtin-plugin/DllReferenceAgencyPlugin.ts +++ b/packages/rspack/src/builtin-plugin/DllReferenceAgencyPlugin.ts @@ -1,15 +1,15 @@ import { BuiltinPluginName, - type RawDllReferencePluginAgencyOptions + type RawDllReferenceAgencyPluginOptions } from "@rspack/binding"; import { create } from "./base"; -export type DllReferencePluginAgencyOptions = - RawDllReferencePluginAgencyOptions; +export type DllReferenceAgencyPluginOptions = + RawDllReferenceAgencyPluginOptions; export const DllReferenceAgencyPlugin = create( BuiltinPluginName.DllReferenceAgencyPlugin, ( - options: DllReferencePluginAgencyOptions - ): RawDllReferencePluginAgencyOptions => options + options: DllReferenceAgencyPluginOptions + ): RawDllReferenceAgencyPluginOptions => options ); From 8f25d296ac35a477d37b2b4488e203e4e5004718 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Tue, 5 Nov 2024 17:32:43 +0800 Subject: [PATCH 08/22] fix: typo --- crates/node_binding/binding.d.ts | 2 +- .../src/options/raw_builtins/mod.rs | 4 ++-- .../src/options/raw_builtins/raw_dll.rs | 8 ++++---- .../src/dll_entry/dll_entry_dependency.rs | 2 +- .../src/dll_reference/delegated_module.rs | 10 +++++----- packages/rspack/src/builtin-plugin/DllEntryPlugin.ts | 4 ++-- .../docs/en/plugins/webpack/dll-reference-plugin.mdx | 2 +- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index e37dc8be6ca..46bc9a13919 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -1283,7 +1283,7 @@ export interface RawCssParserOptions { namedExports?: boolean } -export interface RawDllEntyPluginOptions { +export interface RawDllEntryPluginOptions { context: string entries: Array name: string diff --git a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs index 8fa9f2df63e..89c706df72f 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs @@ -85,7 +85,7 @@ use rspack_plugin_worker::WorkerPlugin; pub use self::{ raw_banner::RawBannerPluginOptions, raw_copy::RawCopyRspackPluginOptions, - raw_dll::{RawDllEntyPluginOptions, RawLibManifestPluginOptions}, + raw_dll::{RawDllEntryPluginOptions, RawLibManifestPluginOptions}, raw_html::RawHtmlRspackPluginOptions, raw_ignore::RawIgnorePluginOptions, raw_limit_chunk_count::RawLimitChunkCountPluginOptions, @@ -531,7 +531,7 @@ impl BuiltinPlugin { plugins.push(ContextReplacementPlugin::new(options).boxed()); } BuiltinPluginName::DllEntryPlugin => { - let raw_options = downcast_into::(self.options)?; + let raw_options = downcast_into::(self.options)?; let options = raw_options.into(); plugins.push(DllEntryPlugin::new(options).boxed()); } diff --git a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs index 21b009799ed..bbff839ef38 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs @@ -6,15 +6,15 @@ use rspack_plugin_dll::{ #[derive(Debug)] #[napi(object)] -pub struct RawDllEntyPluginOptions { +pub struct RawDllEntryPluginOptions { pub context: String, pub entries: Vec, pub name: String, } -impl From for DllEntryPluginOptions { - fn from(value: RawDllEntyPluginOptions) -> Self { - let RawDllEntyPluginOptions { +impl From for DllEntryPluginOptions { + fn from(value: RawDllEntryPluginOptions) -> Self { + let RawDllEntryPluginOptions { name, context, entries, diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs index 3d8dbf94a6e..2b367fde8ed 100644 --- a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs @@ -11,7 +11,7 @@ pub struct DllEntryDependency { pub entries: Vec, - // TODO: The fiedls `name` for serialize & deserialize. + // TODO: The fields `name` for serialize & deserialize. pub name: String, id: DependencyId, diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs index 9c25ee2dcd0..252d4752db3 100644 --- a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs @@ -120,7 +120,7 @@ impl Module for DelegatedModule { let mut runtime_requirements = RuntimeGlobals::default(); runtime_requirements.insert(RuntimeGlobals::REQUIRE); runtime_requirements.insert(RuntimeGlobals::MODULE); - let mut code_generation_reuslt = CodeGenerationResult { + let mut code_generation_result = CodeGenerationResult { runtime_requirements, ..Default::default() }; @@ -131,7 +131,7 @@ impl Module for DelegatedModule { let dependency = mg .dependency_by_id(&dep) .and_then(|dep| dep.downcast_ref::()) - .expect("Should be module depdency"); + .expect("Should be module dependency"); let str = match source_module { Some(_) => { @@ -139,7 +139,7 @@ impl Module for DelegatedModule { "module.exports = {}", module_raw( compilation, - &mut code_generation_reuslt.runtime_requirements, + &mut code_generation_result.runtime_requirements, &dep, dependency.request(), false, @@ -177,9 +177,9 @@ impl Module for DelegatedModule { Arc::new(raw_source) }; - code_generation_reuslt = code_generation_reuslt.with_javascript(source); + code_generation_result = code_generation_result.with_javascript(source); - Ok(code_generation_reuslt) + Ok(code_generation_result) } fn need_build(&self) -> bool { diff --git a/packages/rspack/src/builtin-plugin/DllEntryPlugin.ts b/packages/rspack/src/builtin-plugin/DllEntryPlugin.ts index c3f7532f039..593ac273675 100644 --- a/packages/rspack/src/builtin-plugin/DllEntryPlugin.ts +++ b/packages/rspack/src/builtin-plugin/DllEntryPlugin.ts @@ -1,6 +1,6 @@ import { BuiltinPluginName, - type RawDllEntyPluginOptions + type RawDllEntryPluginOptions } from "@rspack/binding"; import { create } from "./base"; @@ -14,7 +14,7 @@ export const DllEntryPlugin = create( context: string, entries: string[], options: DllEntryPluginOptions - ): RawDllEntyPluginOptions => { + ): RawDllEntryPluginOptions => { return { context, entries, diff --git a/website/docs/en/plugins/webpack/dll-reference-plugin.mdx b/website/docs/en/plugins/webpack/dll-reference-plugin.mdx index 81c4f0c28d7..abb4a99dbe7 100644 --- a/website/docs/en/plugins/webpack/dll-reference-plugin.mdx +++ b/website/docs/en/plugins/webpack/dll-reference-plugin.mdx @@ -150,7 +150,7 @@ Application require dependencies will reference to pre-built using `DllPlugin`. ### With Scope -The content of the dll is accessible under a module prefix when setted scope. +The content of the dll is accessible under a module prefix when set scope. ```js new rspack.DllReferencePlugin({ From e4246b010f40d20bed349cf2e3997f48b23be38b Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Tue, 5 Nov 2024 17:40:55 +0800 Subject: [PATCH 09/22] fix: remove the unnecessary generics --- crates/rspack_plugin_dll/src/dll_entry/dll_module.rs | 2 +- .../src/dll_reference/delegated_module.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs index bb50cef3d73..df212d4c055 100644 --- a/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_module.rs @@ -78,7 +78,7 @@ impl Module for DllModule { async fn build( &mut self, - _build_context: BuildContext<'_>, + _build_context: BuildContext, _compilation: Option<&Compilation>, ) -> Result { let dependencies = self diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs index 252d4752db3..600ed2bd4bd 100644 --- a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs @@ -5,10 +5,10 @@ use rspack_collections::{Identifiable, Identifier}; use rspack_core::{ impl_module_meta_info, impl_source_map_config, module_raw, module_update_hash, rspack_sources::{BoxSource, OriginalSource, RawSource, Source}, - throw_missing_module_error_block, AsyncDependenciesBlockIdentifier, BuildInfo, BuildMeta, - BuildResult, CodeGenerationResult, Compilation, ConcatenationScope, Context, DependenciesBlock, - DependencyId, FactoryMeta, LibIdentOptions, Module, ModuleDependency, ModuleType, RuntimeGlobals, - RuntimeSpec, SourceType, StaticExportsDependency, StaticExportsSpec, + throw_missing_module_error_block, AsyncDependenciesBlockIdentifier, BuildContext, BuildInfo, + BuildMeta, BuildResult, CodeGenerationResult, Compilation, ConcatenationScope, Context, + DependenciesBlock, DependencyId, FactoryMeta, LibIdentOptions, Module, ModuleDependency, + ModuleType, RuntimeGlobals, RuntimeSpec, SourceType, StaticExportsDependency, StaticExportsSpec, }; use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result}; use rspack_util::source_map::ModuleSourceMapConfig; @@ -92,7 +92,7 @@ impl Module for DelegatedModule { async fn build( &mut self, - _build_context: rspack_core::BuildContext<'_>, + _build_context: BuildContext, _compilation: Option<&Compilation>, ) -> Result { Ok(BuildResult { From 43555e67cbae96b4f93488624d8a703f0fc364a6 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Tue, 5 Nov 2024 17:44:54 +0800 Subject: [PATCH 10/22] fix: typo --- .../rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs index 2b367fde8ed..29fa5a25636 100644 --- a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs @@ -34,8 +34,8 @@ impl DllEntryDependency { } } -// It would not create module by rspack,if depdency is not ModuleDependency. -// So we impl ModuleDepedency for [DllEntryDependency] +// It would not create module by rspack,if dependency is not ModuleDependency. +// So we impl ModuleDependency for [DllEntryDependency] impl ModuleDependency for DllEntryDependency { fn request(&self) -> &str { "dll main" From 1f225e886b1d518ca626284b82d895d65bd0081b Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Tue, 5 Nov 2024 18:04:00 +0800 Subject: [PATCH 11/22] fix: cargo clippy test --- crates/rspack_core/src/module_graph/connection.rs | 3 ++- crates/rspack_core/src/module_graph/mod.rs | 2 +- .../rspack_plugin_dll/src/dll_reference/delegated_plugin.rs | 6 +----- .../src/flag_all_modules_as_used_plugin.rs | 2 +- crates/rspack_plugin_dll/src/lib_manifest_plugin.rs | 6 +++--- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/crates/rspack_core/src/module_graph/connection.rs b/crates/rspack_core/src/module_graph/connection.rs index 24be0190076..bf96f96b1c9 100644 --- a/crates/rspack_core/src/module_graph/connection.rs +++ b/crates/rspack_core/src/module_graph/connection.rs @@ -1,6 +1,7 @@ -use std::{collections::HashSet, hash::Hash}; +use std::hash::Hash; use itertools::Itertools; +use rustc_hash::FxHashSet as HashSet; use crate::{DependencyId, ModuleGraph, ModuleIdentifier, RuntimeSpec}; diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index 0e635b2238b..f76fd7c8c38 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -471,7 +471,7 @@ impl<'a> ModuleGraph<'a> { let dependency_id = DependencyId::new(); let mut connection = - ModuleGraphConnection::new(dependency_id, None, module_identifier.clone(), true, false); + ModuleGraphConnection::new(dependency_id, None, *module_identifier, true, false); connection.add_explanation(explanation); diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs index 3887f9ff235..05760e35400 100644 --- a/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_plugin.rs @@ -88,11 +88,7 @@ async fn factorize(&self, data: &mut ModuleFactoryCreateData) -> Result() + &request.chars().skip(scope.len()).collect::() ); if let Some(resolved) = self.options.content.get(&inner_request) { diff --git a/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs b/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs index c2ae61517e5..6f118877031 100644 --- a/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs +++ b/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs @@ -53,7 +53,7 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result Result<()> { }, )?; - if use_paths.get(&target_path).is_some() { + if use_paths.contains(&target_path) { return Err(Error::msg("each chunk must have a unique path")); } @@ -142,7 +142,7 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { ident.into_owned(), DllManifestContentItem { id: id.map(|id| id.to_string()), - build_meta: build_meta.map(|meta| meta.clone()), + build_meta: build_meta.cloned(), exports: provided_exports, }, ); @@ -184,5 +184,5 @@ where return true; } } - return false; + false } From 0ba5e3182aeffe19f9c22230b03ec09b08c85890 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Tue, 5 Nov 2024 18:07:56 +0800 Subject: [PATCH 12/22] fix: remove unused dependency rspack_macros --- Cargo.lock | 1 - crates/rspack_plugin_dll/Cargo.toml | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1374dcea3c2..3600ee08e0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4074,7 +4074,6 @@ dependencies = [ "rspack_core", "rspack_error", "rspack_hook", - "rspack_macros", "rspack_plugin_externals", "rspack_util", "rustc-hash 1.1.0", diff --git a/crates/rspack_plugin_dll/Cargo.toml b/crates/rspack_plugin_dll/Cargo.toml index 5b7cd82466b..5abf4e6c256 100644 --- a/crates/rspack_plugin_dll/Cargo.toml +++ b/crates/rspack_plugin_dll/Cargo.toml @@ -12,7 +12,6 @@ rspack_collections = { version = "0.1.0", path = "../rspack_collections" } rspack_core = { version = "0.1.0", path = "../rspack_core" } rspack_error = { version = "0.1.0", path = "../rspack_error" } rspack_hook = { version = "0.1.0", path = "../rspack_hook" } -rspack_macros = { version = "0.1.0", path = "../rspack_macros" } rspack_plugin_externals = { version = "0.1.0", path = "../rspack_plugin_externals" } rspack_util = { version = "0.1.0", path = "../rspack_util" } @@ -24,3 +23,6 @@ tracing = { workspace = true } [lints] workspace = true + +[package.metadata.cargo-shear] +ignored = ["tracing"] From 2c044ecb0fe5db69c583361df888c20dd8018e13 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Wed, 6 Nov 2024 11:21:57 +0800 Subject: [PATCH 13/22] docs: add dll & dll-reference plugin zh docs --- website/docs/en/plugins/webpack/_meta.json | 6 +- website/docs/zh/plugins/webpack/_meta.json | 4 +- .../docs/zh/plugins/webpack/dll-plugin.mdx | 98 +++++++++++ .../plugins/webpack/dll-reference-plugin.mdx | 165 ++++++++++++++++++ 4 files changed, 269 insertions(+), 4 deletions(-) create mode 100644 website/docs/zh/plugins/webpack/dll-plugin.mdx create mode 100644 website/docs/zh/plugins/webpack/dll-reference-plugin.mdx diff --git a/website/docs/en/plugins/webpack/_meta.json b/website/docs/en/plugins/webpack/_meta.json index da5d5b6de15..ae8a1f117cb 100644 --- a/website/docs/en/plugins/webpack/_meta.json +++ b/website/docs/en/plugins/webpack/_meta.json @@ -19,8 +19,6 @@ "module-federation-plugin-v1", "no-emit-on-errors-plugin", "context-replacement-plugin", - "dll-plugin", - "dll-reference-plugin", "node-target-plugin", "node-template-plugin", "normal-module-replacement-plugin", @@ -29,5 +27,7 @@ "runtime-chunk-plugin", "source-map-dev-tool-plugin", "split-chunks-plugin", - "internal-plugins" + "internal-plugins", + "dll-plugin", + "dll-reference-plugin" ] diff --git a/website/docs/zh/plugins/webpack/_meta.json b/website/docs/zh/plugins/webpack/_meta.json index 23091b0b540..cbd7e28af29 100644 --- a/website/docs/zh/plugins/webpack/_meta.json +++ b/website/docs/zh/plugins/webpack/_meta.json @@ -26,5 +26,7 @@ "runtime-chunk-plugin", "source-map-dev-tool-plugin", "split-chunks-plugin", - "internal-plugins" + "internal-plugins", + "dll-plugin", + "dll-reference-plugin" ] diff --git a/website/docs/zh/plugins/webpack/dll-plugin.mdx b/website/docs/zh/plugins/webpack/dll-plugin.mdx new file mode 100644 index 00000000000..f47a1bf815f --- /dev/null +++ b/website/docs/zh/plugins/webpack/dll-plugin.mdx @@ -0,0 +1,98 @@ +import { Table } from '@builtIns'; +import WebpackLicense from '@components/WebpackLicense'; + + + +# DllPlugin + +Dll 插件用于在一个单独的 rspack 配置中生成一个 dll 库,DllReference 插件通过该插件生成的 manifest.json 能够将依赖映射到对应的位置上。 + +## 选项 + +- **类型:** + +```ts +type DllPluginOptions = { + context?: string; + entryOnly?: boolean; + format?: boolean; + name?: string; + path: string; + type?: string; +}; +``` + +
+ +## 示例 + +```js +new rspack.DllPlugin({ + path: path.resolve(__dirname, 'manifest.json'), + name: '[name]_dll_lib', +}); +``` + +插件将会生成 `manifest.json` 文件并将其输出到指定路径。 +manifest 文件包含了从 `require` 和 `import` 请求到模块id的映射。 + +`manifest.json` 将会被 [DllReferencePlugin](/plugins/webpack/dll-reference-plugin) 消费,通过"动态链接库"的方式将依赖链接起来。 + +此插件与 `output.library` 的选项相结合可以暴露出dll 函数。 diff --git a/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx b/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx new file mode 100644 index 00000000000..b38d264a4fa --- /dev/null +++ b/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx @@ -0,0 +1,165 @@ +import WebpackLicense from '@components/WebpackLicense'; + + + +# DllReferencePlugin + +`DllReferencePlugin` 将引用的模块链接到预构建的模块。 + +## 选项 + +- **类型:** + +```ts +type DllReferencePluginOptionsContent = { + /** + * Module info. + */ + [k: string]: { + /** + * Meta information about the module. + */ + buildMeta?: { + [k: string]: any; + }; + /** + * Information about the provided exports of the module. + */ + exports?: string[] | true; + /** + * Module ID. + */ + id: number | string; + }; +}; + +type DllReferencePluginOptionsManifest = { + /** + * The mappings from request to module info. + */ + content: DllReferencePluginOptionsContent; + /** + * The name where the dll is exposed (external name). + */ + name?: string; + /** + * The type how the dll is exposed (external type). + */ + type?: DllReferencePluginOptionsSourceType; +}; + +/** + * The type how the dll is exposed (external type). + */ +type DllReferencePluginOptionsSourceType = + | 'var' + | 'assign' + | 'this' + | 'window' + | 'global' + | 'commonjs' + | 'commonjs2' + | 'commonjs-module' + | 'amd' + | 'amd-require' + | 'umd' + | 'umd2' + | 'jsonp' + | 'system'; + +type DllReferencePluginOptions = + | { + /** + * Context of requests in the manifest (or content property) as absolute path. + */ + context?: string; + /** + * Extensions used to resolve modules in the dll bundle (only used when using 'scope'). + */ + extensions?: string[]; + /** + * An object containing content and name or a string to the absolute path of the JSON manifest to be loaded upon compilation. + */ + manifest: string | DllReferencePluginOptionsManifest; + /** + * The name where the dll is exposed (external name, defaults to manifest.name). + */ + name?: string; + /** + * Prefix which is used for accessing the content of the dll. + */ + scope?: string; + /** + * How the dll is exposed (libraryTarget, defaults to manifest.type). + */ + sourceType?: DllReferencePluginOptionsSourceType; + /** + * The way how the export of the dll bundle is used. + */ + type?: 'require' | 'object'; + } + | { + /** + * The mappings from request to module info. + */ + content: DllReferencePluginOptionsContent; + /** + * Context of requests in the manifest (or content property) as absolute path. + */ + context?: string; + /** + * Extensions used to resolve modules in the dll bundle (only used when using 'scope'). + */ + extensions?: string[]; + /** + * The name where the dll is exposed (external name). + */ + name: string; + /** + * Prefix which is used for accessing the content of the dll. + */ + scope?: string; + /** + * How the dll is exposed (libraryTarget). + */ + sourceType?: DllReferencePluginOptionsSourceType; + /** + * The way how the export of the dll bundle is used. + */ + type?: 'require' | 'object'; + }; +``` + +当依赖被引用的时候,插件通过 dll manifest 文件将依赖名和预打包的模块链接起来。 + +## 示例 + +### 基础示例 + +```js +new rspack.DllReferencePlugin({ + // 这里引用的 manifest 文件应该是由 Dll 插件生成的 + manifest: require('../lib/manifest.json'), + + name: '[name]_dll_lib', +}); +``` + +应用需要的依赖项将链接到由 `DllPlugin` 预构建的模块。 + +### 使用 Scope + +通过配置 `scope`, dll 中的内容可以使用模块前缀的方式引用 + +```js +new rspack.DllReferencePlugin({ + // 这里引用的 manifest 文件应该是由 Dll 插件生成的 + manifest: require('../lib/manifest.json'), + + name: '[name]_dll_lib', + + scope: 'xyz', +}); +``` + +举例来说,通过 `require('xzy/abc')` 可以获取预构建模块中 `abc` 模块。 From 0c05841f0a38e46226e24a107e9b66b6fbd554a9 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Wed, 6 Nov 2024 11:23:49 +0800 Subject: [PATCH 14/22] fix: resolved merge conflict --- website/docs/en/plugins/webpack/_meta.json | 1 - 1 file changed, 1 deletion(-) diff --git a/website/docs/en/plugins/webpack/_meta.json b/website/docs/en/plugins/webpack/_meta.json index ae8a1f117cb..cbd7e28af29 100644 --- a/website/docs/en/plugins/webpack/_meta.json +++ b/website/docs/en/plugins/webpack/_meta.json @@ -18,7 +18,6 @@ "module-federation-plugin", "module-federation-plugin-v1", "no-emit-on-errors-plugin", - "context-replacement-plugin", "node-target-plugin", "node-template-plugin", "normal-module-replacement-plugin", From 71811aa3ea9c660074a0f1b7e155612c14d4b1ce Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Fri, 8 Nov 2024 10:34:34 +0800 Subject: [PATCH 15/22] fix: resolved some issues - dll-reference-plugin should given a manifest struct, rather than json-string - we should not add a null module_graph_module_connection like webpack --- crates/node_binding/binding.d.ts | 36 +++++- .../src/options/raw_builtins/raw_dll.rs | 67 ++++++++-- crates/rspack_binding_values/src/module.rs | 121 +++++++++++++++++- crates/rspack_core/src/module.rs | 18 ++- .../src/module_graph/connection.rs | 18 --- crates/rspack_core/src/module_graph/mod.rs | 17 --- crates/rspack_core/src/module_graph/module.rs | 6 + .../dll_reference_agency_plugin.rs | 26 ++-- .../src/flag_all_modules_as_used_plugin.rs | 6 +- crates/rspack_plugin_dll/src/lib.rs | 12 +- .../src/plugin/module_concatenation_plugin.rs | 42 ++++-- packages/rspack/etc/core.api.md | 5 +- packages/rspack/src/lib/DllPlugin.ts | 4 +- packages/rspack/src/lib/DllReferencePlugin.ts | 109 +++++++++++----- 14 files changed, 360 insertions(+), 127 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 8fdac3a7c91..18ee5ccfd14 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -450,6 +450,22 @@ export interface JsBeforeResolveArgs { issuer: string } +export interface JsBuildMeta { + strictEsmModule: boolean + hasTopLevelAwait: boolean + esm: boolean + exportsType: 'unset' | 'default' | 'namespace' | 'flagged' | 'dynamic' + defaultObject: 'false' | 'redirect' | JsBuildMetaDefaultObjectRedirectWarn + moduleArgument: 'module' | 'webpackModule' + exportsArgument: 'exports' | 'webpackExports' + sideEffectFree?: boolean + exportsFinalName?: Array<[string, string]> | undefined +} + +export interface JsBuildMetaDefaultObjectRedirectWarn { + redirectWarn: JsDefaultObjectRedirectWarnObject +} + export interface JsBuildTimeExecutionOption { publicPath?: string baseUri?: string @@ -523,6 +539,10 @@ export interface JsCreateData { resource: string } +export interface JsDefaultObjectRedirectWarnObject { + ignore: boolean +} + export interface JsDiagnostic { message: string help?: string @@ -1279,6 +1299,18 @@ export interface RawDllEntryPluginOptions { name: string } +export interface RawDllManifest { + content: RawDllManifestContent + name?: string + type?: string +} + +export interface RawDllManifestContentItem { + buildMeta?: JsBuildMeta + exports?: Array + id?: string +} + export interface RawDllReferenceAgencyPluginOptions { context?: string name?: string @@ -1286,8 +1318,8 @@ export interface RawDllReferenceAgencyPluginOptions { scope?: string sourceType?: string type: string - content?: string - manifest?: string + content?: RawDllManifestContent + manifest?: RawDllManifest } export interface RawDraft { diff --git a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs index bbff839ef38..d48a7c20cfb 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs @@ -1,8 +1,11 @@ use napi_derive::napi; -use rspack_binding_values::JsFilename; +use rspack_binding_values::{JsBuildMeta, JsFilename}; use rspack_plugin_dll::{ - DllEntryPluginOptions, DllReferenceAgencyPluginOptions, LibManifestPluginOptions, + DllEntryPluginOptions, DllManifest, DllManifestContent, DllManifestContentItem, + DllReferenceAgencyPluginOptions, LibManifestPluginOptions, }; +use rustc_hash::FxHashMap as HashMap; +use swc_core::atoms::Atom; #[derive(Debug)] #[napi(object)] @@ -61,8 +64,7 @@ impl From for LibManifestPluginOptions { } } -#[derive(Debug)] -#[napi(object)] +#[napi(object, object_to_js = false)] pub struct RawDllReferenceAgencyPluginOptions { pub context: Option, pub name: Option, @@ -70,8 +72,53 @@ pub struct RawDllReferenceAgencyPluginOptions { pub scope: Option, pub source_type: Option, pub r#type: String, - pub content: Option, - pub manifest: Option, + pub content: Option, + pub manifest: Option, +} + +type RawDllManifestContent = HashMap; + +#[napi(object, object_to_js = false)] +pub struct RawDllManifestContentItem { + pub build_meta: Option, + pub exports: Option>, + pub id: Option, +} + +impl From for DllManifestContentItem { + fn from(value: RawDllManifestContentItem) -> Self { + Self { + build_meta: value.build_meta.map(|meta| meta.into()), + exports: value.exports.map(|exports| { + exports + .into_iter() + .map(|export| Atom::from(export)) + .collect::>() + }), + id: value.id.into(), + } + } +} + +#[napi(object, object_to_js = false)] +pub struct RawDllManifest { + pub content: RawDllManifestContent, + pub name: Option, + pub r#type: Option, +} + +impl From for DllManifest { + fn from(value: RawDllManifest) -> Self { + Self { + content: value + .content + .into_iter() + .map(|(k, v)| (k, v.into())) + .collect::(), + name: value.name.map(|n| n.into()), + r#type: value.r#type.into(), + } + } } impl From for DllReferenceAgencyPluginOptions { @@ -94,8 +141,12 @@ impl From for DllReferenceAgencyPluginOption scope, source_type, r#type, - content, - manifest, + content: content.map(|c| { + c.into_iter() + .map(|(k, v)| (k, v.into())) + .collect::() + }), + manifest: manifest.map(|m| m.into()), } } } diff --git a/crates/rspack_binding_values/src/module.rs b/crates/rspack_binding_values/src/module.rs index ff027732f9f..974145da189 100644 --- a/crates/rspack_binding_values/src/module.rs +++ b/crates/rspack_binding_values/src/module.rs @@ -1,10 +1,12 @@ use std::{cell::RefCell, ptr::NonNull, sync::Arc}; +use napi::bindgen_prelude::ToNapiValue; use napi_derive::napi; use rspack_collections::IdentifierMap; use rspack_core::{ - AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, Compilation, CompilationId, - DependenciesBlock, Module, ModuleGraph, ModuleIdentifier, RuntimeModuleStage, SourceType, + AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, BuildMeta, BuildMetaDefaultObject, + BuildMetaExportsType, Compilation, CompilationId, DependenciesBlock, ExportsArgument, Module, + ModuleArgument, ModuleGraph, ModuleIdentifier, RuntimeModuleStage, SourceType, }; use rspack_napi::{napi::bindgen_prelude::*, threadsafe_function::ThreadsafeFunction, OneShotRef}; use rspack_plugin_runtime::RuntimeModuleFromJs; @@ -534,3 +536,118 @@ impl From for RuntimeModuleFromJs { } } } + +#[napi(object, object_to_js = false)] +pub struct JsBuildMeta { + pub strict_esm_module: bool, + pub has_top_level_await: bool, + pub esm: bool, + #[napi(ts_type = "'unset' | 'default' | 'namespace' | 'flagged' | 'dynamic'")] + pub exports_type: String, + #[napi(ts_type = "'false' | 'redirect' | JsBuildMetaDefaultObjectRedirectWarn")] + pub default_object: JsBuildMetaDefaultObject, + #[napi(ts_type = "'module' | 'webpackModule'")] + pub module_argument: String, + #[napi(ts_type = "'exports' | 'webpackExports'")] + pub exports_argument: String, + pub side_effect_free: Option, + #[napi(ts_type = "Array<[string, string]> | undefined")] + pub exports_final_name: Option>>, +} + +impl From for BuildMeta { + fn from(value: JsBuildMeta) -> Self { + let JsBuildMeta { + strict_esm_module, + has_top_level_await, + esm, + exports_argument: raw_exports_argument, + default_object: raw_default_object, + module_argument: raw_module_argument, + exports_final_name: raw_exports_final_name, + side_effect_free, + exports_type: raw_exports_type, + } = value; + + let default_object = match raw_default_object { + Either::A(s) => match s.as_str() { + "false" => BuildMetaDefaultObject::False, + "redirect" => BuildMetaDefaultObject::Redirect, + _ => unreachable!(), + }, + Either::B(default_object) => BuildMetaDefaultObject::RedirectWarn { + ignore: default_object.redirect_warn.ignore, + }, + }; + + let exports_type = match raw_exports_type.as_str() { + "unset" => BuildMetaExportsType::Unset, + "default" => BuildMetaExportsType::Default, + "namespace" => BuildMetaExportsType::Namespace, + "flagged" => BuildMetaExportsType::Flagged, + "dynamic" => BuildMetaExportsType::Dynamic, + _ => unreachable!(), + }; + + let module_argument = match raw_module_argument.as_str() { + "module" => ModuleArgument::Module, + "webpackModule" => ModuleArgument::WebpackModule, + _ => unreachable!(), + }; + + let exports_argument = match raw_exports_argument.as_str() { + "exports" => ExportsArgument::Exports, + "webpackExports" => ExportsArgument::WebpackExports, + _ => unreachable!(), + }; + + let exports_final_name = raw_exports_final_name.map(|exports_name| { + exports_name + .into_iter() + .map(|export_name| { + let first = export_name + .get(0) + .expect("The buildMeta exportsFinalName item should have first value") + .clone(); + let second = export_name + .get(1) + .expect("The buildMeta exportsFinalName item should have second value") + .clone(); + (first, second) + }) + .collect::>() + }); + + Self { + strict_esm_module, + has_top_level_await, + esm, + exports_type, + default_object, + module_argument, + exports_argument, + side_effect_free, + exports_final_name, + } + } +} + +#[napi(object)] +pub struct JsBuildMetaDefaultObjectRedirectWarn { + pub redirect_warn: JsDefaultObjectRedirectWarnObject, +} + +impl From for BuildMetaDefaultObject { + fn from(value: JsBuildMetaDefaultObjectRedirectWarn) -> Self { + Self::RedirectWarn { + ignore: value.redirect_warn.ignore, + } + } +} + +#[napi(object)] +pub struct JsDefaultObjectRedirectWarnObject { + pub ignore: bool, +} + +pub type JsBuildMetaDefaultObject = Either; diff --git a/crates/rspack_core/src/module.rs b/crates/rspack_core/src/module.rs index af14e62360f..bbeae61ecf6 100644 --- a/crates/rspack_core/src/module.rs +++ b/crates/rspack_core/src/module.rs @@ -15,7 +15,7 @@ use rspack_util::atom::Atom; use rspack_util::ext::{AsAny, DynHash}; use rspack_util::source_map::ModuleSourceMapConfig; use rustc_hash::FxHashSet as HashSet; -use serde::{Deserialize, Serialize}; +use serde::Serialize; use crate::concatenated_module::ConcatenatedModule; use crate::dependencies_block::dependencies_block_update_hash; @@ -79,7 +79,8 @@ impl Default for BuildInfo { } } -#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] pub enum BuildMetaExportsType { #[default] Unset, @@ -97,7 +98,8 @@ pub enum ExportsType { Dynamic, } -#[derive(Debug, Default, Clone, Copy, Hash, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, Copy, Hash, Serialize)] +#[serde(rename_all = "camelCase")] pub enum BuildMetaDefaultObject { #[default] False, @@ -111,7 +113,8 @@ pub enum BuildMetaDefaultObject { }, } -#[derive(Debug, Default, Clone, Copy, Hash, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, Copy, Hash, Serialize)] +#[serde(rename_all = "camelCase")] pub enum ModuleArgument { #[default] Module, @@ -127,7 +130,8 @@ impl Display for ModuleArgument { } } -#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] pub enum ExportsArgument { #[default] Exports, @@ -143,7 +147,7 @@ impl Display for ExportsArgument { } } -#[derive(Debug, Default, Clone, Hash, Serialize, Deserialize)] +#[derive(Debug, Default, Clone, Hash, Serialize)] #[serde(rename_all = "camelCase")] pub struct BuildMeta { pub strict_esm_module: bool, @@ -153,7 +157,9 @@ pub struct BuildMeta { pub default_object: BuildMetaDefaultObject, pub module_argument: ModuleArgument, pub exports_argument: ExportsArgument, + #[serde(skip_serializing_if = "Option::is_none")] pub side_effect_free: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub exports_final_name: Option>, } diff --git a/crates/rspack_core/src/module_graph/connection.rs b/crates/rspack_core/src/module_graph/connection.rs index bf96f96b1c9..c86fca4c3a2 100644 --- a/crates/rspack_core/src/module_graph/connection.rs +++ b/crates/rspack_core/src/module_graph/connection.rs @@ -1,8 +1,5 @@ use std::hash::Hash; -use itertools::Itertools; -use rustc_hash::FxHashSet as HashSet; - use crate::{DependencyId, ModuleGraph, ModuleIdentifier, RuntimeSpec}; #[derive(Debug, Clone, Eq)] @@ -17,8 +14,6 @@ pub struct ModuleGraphConnection { pub active: bool, pub conditional: bool, - - explanations: HashSet, } impl Hash for ModuleGraphConnection { @@ -48,19 +43,6 @@ impl ModuleGraphConnection { active, conditional, resolved_original_module_identifier: original_module_identifier, - explanations: Default::default(), - } - } - - pub fn add_explanation(&mut self, explanation: String) { - self.explanations.insert(explanation); - } - - pub fn explanation(&self) -> Option { - if self.explanations.is_empty() { - None - } else { - Some(self.explanations.iter().join(" ")) } } diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index f76fd7c8c38..f435426c83a 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -467,23 +467,6 @@ impl<'a> ModuleGraph<'a> { } } - pub fn add_extra_reason(&mut self, module_identifier: &ModuleIdentifier, explanation: String) { - let dependency_id = DependencyId::new(); - - let mut connection = - ModuleGraphConnection::new(dependency_id, None, *module_identifier, true, false); - - connection.add_explanation(explanation); - - let mg = self - .module_graph_module_by_identifier_mut(module_identifier) - .expect("Should have moduleGraphModule"); - - mg.add_incoming_connection(dependency_id); - - self.add_connection(connection, None); - } - pub fn get_depth(&self, module_id: &ModuleIdentifier) -> Option { self .module_graph_module_by_identifier(module_id) diff --git a/crates/rspack_core/src/module_graph/module.rs b/crates/rspack_core/src/module_graph/module.rs index ef8004fe826..e0442fc8bb6 100644 --- a/crates/rspack_core/src/module_graph/module.rs +++ b/crates/rspack_core/src/module_graph/module.rs @@ -22,6 +22,7 @@ pub struct ModuleGraphModule { pub profile: Option>, pub depth: Option, pub optimization_bailout: Vec, + pub concatenation_bail: String, } impl ModuleGraphModule { @@ -39,6 +40,7 @@ impl ModuleGraphModule { profile: None, depth: None, optimization_bailout: vec![], + concatenation_bail: Default::default(), } } @@ -79,6 +81,10 @@ impl ModuleGraphModule { self.profile.as_deref() } + pub fn add_concatenation_bail_reason(&mut self, reason: &str) { + self.concatenation_bail += &format!(", {}", reason); + } + pub fn set_issuer_if_unset(&mut self, issuer: Option) { if matches!(self.issuer, ModuleIssuer::Unset) { self.issuer = ModuleIssuer::from_identifier(issuer); diff --git a/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs b/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs index 8f947d8e88a..98e2cb4445d 100644 --- a/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs +++ b/crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs @@ -14,8 +14,8 @@ use crate::{DllManifest, DllManifestContent}; pub struct DllReferenceAgencyPluginOptions { pub context: Option, pub name: Option, - pub content: Option, - pub manifest: Option, + pub content: Option, + pub manifest: Option, pub extensions: Vec, pub scope: Option, pub source_type: Option, @@ -42,29 +42,19 @@ impl Plugin for DllReferenceAgencyPlugin { fn apply(&self, ctx: PluginContext<&mut ApplyContext>, options: &CompilerOptions) -> Result<()> { let mut name = self.options.name.clone(); let mut source_type = self.options.source_type.clone(); - let mut resolved_content = self - .options - .content - .as_ref() - .and_then(|content| serde_json::from_str::(content).ok()); - - let manifest = self - .options - .manifest - .as_ref() - .and_then(|manifest| serde_json::from_str::(manifest).ok()); - - if let Some(manifest) = manifest { + let mut resolved_content = self.options.content.clone(); + + if let Some(manifest) = &self.options.manifest { if name.is_none() { - name = manifest.name; + name = manifest.name.clone(); } if source_type.is_none() { - source_type = manifest.r#type; + source_type = manifest.r#type.clone(); } if resolved_content.is_none() { - resolved_content = Some(manifest.content); + resolved_content = Some(manifest.content.clone()); } } diff --git a/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs b/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs index 6f118877031..7298e221501 100644 --- a/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs +++ b/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs @@ -58,7 +58,9 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result Result<()> { - // set all modules have effects. To avoid any module remove by tree shakeing. + // set all modules have effects. To avoid any module remove by tree shaking. // see: https://github.com/webpack/webpack/blob/4b4ca3bb53f36a5b8fc6bc1bd976ed7af161bd80/lib/FlagAllModulesAsUsedPlugin.js#L43-L47 module.set_factory_meta(FactoryMeta { side_effect_free: Some(false), diff --git a/crates/rspack_plugin_dll/src/lib.rs b/crates/rspack_plugin_dll/src/lib.rs index 2d54982be7a..02cac598ef0 100644 --- a/crates/rspack_plugin_dll/src/lib.rs +++ b/crates/rspack_plugin_dll/src/lib.rs @@ -1,18 +1,18 @@ use rspack_core::{BuildMeta, LibraryType}; use rspack_util::atom::Atom; use rustc_hash::FxHashMap as HashMap; -use serde::{Deserialize, Serialize}; +use serde::Serialize; mod dll_entry; mod dll_reference; mod flag_all_modules_as_used_plugin; mod lib_manifest_plugin; -pub(crate) type DllManifestContent = HashMap; +pub type DllManifestContent = HashMap; -#[derive(Debug, Default, Clone, Deserialize, Serialize)] +#[derive(Debug, Default, Clone, Serialize)] #[serde(rename_all = "camelCase")] -pub(crate) struct DllManifestContentItem { +pub struct DllManifestContentItem { #[serde(skip_serializing_if = "Option::is_none")] pub build_meta: Option, @@ -23,8 +23,8 @@ pub(crate) struct DllManifestContentItem { pub id: Option, } -#[derive(Debug, Clone, Deserialize, Serialize)] -pub(crate) struct DllManifest { +#[derive(Debug, Clone, Serialize)] +pub struct DllManifest { pub content: DllManifestContent, #[serde(skip_serializing_if = "Option::is_none")] 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 50e62529bd2..e36c8c65924 100644 --- a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs @@ -278,22 +278,23 @@ impl ModuleConcatenationPlugin { .filter(|&connection| connection.is_active(&module_graph, runtime)) .collect::>(); + // TODO: ADD module connection explanations if !active_non_modules_connections.is_empty() { let problem = { - let importing_explanations = active_non_modules_connections - .iter() - .flat_map(|&c| c.explanation()) - .collect::>(); - let mut explanations: Vec<_> = importing_explanations.into_iter().collect(); - explanations.sort(); + // let importing_explanations = active_non_modules_connections + // .iter() + // .flat_map(|&c| c.explanation()) + // .collect::>(); + // let mut explanations: Vec<_> = importing_explanations.into_iter().collect(); + // explanations.sort(); format!( - "Module {} is referenced {}", + "Module {} is referenced", module_readable_identifier, - if !explanations.is_empty() { - format!("by: {}", explanations.join(", ")) - } else { - "in an unsupported way".to_string() - } + // if !explanations.is_empty() { + // format!("by: {}", explanations.join(", ")) + // } else { + // "in an unsupported way".to_string() + // } ) }; let problem = Warning::Problem(problem); @@ -302,6 +303,23 @@ impl ModuleConcatenationPlugin { return Some(problem); } } + + // Every module_graph_module that tags concatenaion_bail should be return a warning problem to avoid this module concate. + if let Some(concatenation_bail) = module_graph + .module_graph_module_by_identifier(module_id) + .map(|mgm| &mgm.concatenation_bail) + { + if !concatenation_bail.is_empty() { + let problem = Warning::Problem(format!( + "Module {} is bail by: {}", + module_readable_identifier, concatenation_bail, + )); + statistics.incorrect_dependency += 1; + failure_cache.insert(*module_id, problem.clone()); + return Some(problem); + } + }; + let mut incoming_connections_from_modules = HashMap::default(); for (origin_module, connections) in incoming_connections.iter() { if let Some(origin_module) = origin_module { diff --git a/packages/rspack/etc/core.api.md b/packages/rspack/etc/core.api.md index 082ec86bbc8..0899115ab72 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -32,6 +32,7 @@ import { JsAlterAssetTagsData } from '@rspack/binding'; import type { JsAssetInfo } from '@rspack/binding'; import { JsBeforeAssetTagGenerationData } from '@rspack/binding'; import { JsBeforeEmitData } from '@rspack/binding'; +import type { JsBuildMeta } from '@rspack/binding'; import { JsChunk } from '@rspack/binding'; import { JsChunkGroup } from '@rspack/binding'; import { JsChunkGroupOrigin } from '@rspack/binding'; @@ -1448,9 +1449,7 @@ export type DllReferencePluginOptions = { // @public export interface DllReferencePluginOptionsContent { [k: string]: { - buildMeta?: { - [k: string]: any; - }; + buildMeta?: JsBuildMeta; exports?: string[] | true; id: number | string; }; diff --git a/packages/rspack/src/lib/DllPlugin.ts b/packages/rspack/src/lib/DllPlugin.ts index 1ec0f480137..26ab533342f 100644 --- a/packages/rspack/src/lib/DllPlugin.ts +++ b/packages/rspack/src/lib/DllPlugin.ts @@ -57,8 +57,6 @@ const dllPluginOptions = z.object({ type: z.string().optional() }) satisfies z.ZodType; -const DLL_PLUGIN_NAME = "DllPlugin"; - export class DllPlugin { private options: DllPluginOptions; @@ -71,7 +69,7 @@ export class DllPlugin { } apply(compiler: Compiler) { - compiler.hooks.entryOption.tap(DLL_PLUGIN_NAME, (context, entry) => { + compiler.hooks.entryOption.tap(DllPlugin.name, (context, entry) => { if (typeof entry === "function") { throw new Error( "DllPlugin doesn't support dynamic entry (function) yet" diff --git a/packages/rspack/src/lib/DllReferencePlugin.ts b/packages/rspack/src/lib/DllReferencePlugin.ts index 42d78a0d4c3..95a82e333a2 100644 --- a/packages/rspack/src/lib/DllReferencePlugin.ts +++ b/packages/rspack/src/lib/DllReferencePlugin.ts @@ -8,10 +8,14 @@ * https://github.com/webpack/webpack/blob/main/LICENSE */ +import type { JsBuildMeta } from "@rspack/binding"; import z from "zod"; +import type { CompilationParams } from "../Compilation"; import type { Compiler } from "../Compiler"; import { DllReferenceAgencyPlugin } from "../builtin-plugin"; +import { makePathsRelative } from "../util/identifier"; import { validate } from "../util/validate"; +import WebpackError from "./WebpackError"; export type DllReferencePluginOptions = | { @@ -121,9 +125,7 @@ export interface DllReferencePluginOptionsContent { /** * Meta information about the module. */ - buildMeta?: { - [k: string]: any; - }; + buildMeta?: JsBuildMeta; /** * Information about the provided exports of the module. */ @@ -136,7 +138,7 @@ export interface DllReferencePluginOptionsContent { } const dllReferencePluginOptionsContentItem = z.object({ - buildMeta: z.record(z.any()).optional(), + buildMeta: z.custom().optional(), exports: z.array(z.string()).or(z.literal(true)).optional(), id: z.union([z.number(), z.string()]) }); @@ -189,40 +191,75 @@ const dllReferencePluginOptions = z.union([ }) ]) satisfies z.ZodType; -const DLL_REFERENCE_PLUGIN_NAME = "DllReferencePlugin"; - export class DllReferencePlugin { private options: DllReferencePluginOptions; + private errors: WeakMap; + constructor(options: DllReferencePluginOptions) { validate(options, dllReferencePluginOptions); this.options = options; + this.errors = new WeakMap(); } apply(compiler: Compiler) { - compiler.hooks.beforeCompile.tapAsync( - DLL_REFERENCE_PLUGIN_NAME, - (_, callback) => { - if ("manifest" in this.options) { - const manifest = this.options.manifest; - - let manifest_content: string | undefined = undefined; - - if (typeof manifest === "string") { - compiler.inputFileSystem?.readFile( - manifest, - "utf8", - (err, result) => { - if (err) return callback(err); - - manifest_content = result; - } - ); + compiler.hooks.beforeCompile.tapPromise( + DllReferencePlugin.name, + async params => { + const manifest = await new Promise< + DllReferencePluginOptionsManifest | undefined + >((resolve, reject) => { + if ("manifest" in this.options) { + const manifest = this.options.manifest; + + if (typeof manifest === "string") { + const manifestParameter = manifest; + + compiler.inputFileSystem?.readFile( + manifestParameter, + "utf8", + (err, result) => { + if (err) return reject(err); + + if (!result) + return reject( + new DllManifestError( + manifestParameter, + `Can't read anything from ${manifestParameter}` + ) + ); + + try { + const manifest: DllReferencePluginOptionsManifest = + JSON.parse(result); + resolve(manifest); + } catch (parseError) { + const manifestPath = makePathsRelative( + compiler.context, + manifestParameter, + compiler.root + ); + + this.errors.set( + params, + new DllManifestError( + manifestPath, + (parseError as Error).message + ) + ); + } + } + ); + } else { + resolve(manifest); + } } else { - manifest_content = JSON.stringify(manifest); + resolve(undefined); } + }); + if (!this.errors.has(params)) { new DllReferenceAgencyPlugin({ ...this.options, type: this.options.type || "require", @@ -232,24 +269,36 @@ export class DllReferencePlugin { ".json", ".wasm" ], - manifest: manifest_content + manifest }).apply(compiler); - - callback(); } } ); compiler.hooks.compilation.tap( - DLL_REFERENCE_PLUGIN_NAME, - (compilation, _) => { + DllReferencePlugin.name, + (compilation, params) => { if ( "manifest" in this.options && typeof this.options.manifest === "string" ) { + const error = this.errors.get(params); + if (error) { + compilation.errors.push(error); + } + compilation.fileDependencies.add(this.options.manifest); } } ); } } + +class DllManifestError extends WebpackError { + constructor(filename: string, message: string) { + super(); + + this.name = "DllManifestError"; + this.message = `Dll manifest ${filename}\n${message}`; + } +} From 3ac63018698126e5b84fff514094b245460a463c Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Fri, 8 Nov 2024 10:46:27 +0800 Subject: [PATCH 16/22] chore: add some annotations --- crates/rspack_binding_values/src/module.rs | 1 - .../rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs | 2 -- .../rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs | 4 ++++ .../src/plugin/module_concatenation_plugin.rs | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/rspack_binding_values/src/module.rs b/crates/rspack_binding_values/src/module.rs index 974145da189..343daa9f312 100644 --- a/crates/rspack_binding_values/src/module.rs +++ b/crates/rspack_binding_values/src/module.rs @@ -1,6 +1,5 @@ use std::{cell::RefCell, ptr::NonNull, sync::Arc}; -use napi::bindgen_prelude::ToNapiValue; use napi_derive::napi; use rspack_collections::IdentifierMap; use rspack_core::{ diff --git a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs index 29fa5a25636..157c1a9546e 100644 --- a/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs +++ b/crates/rspack_plugin_dll/src/dll_entry/dll_entry_dependency.rs @@ -56,8 +56,6 @@ impl Dependency for DllEntryDependency { } } -// impl AsModuleDependency for DllEntryDependency {} - impl AsContextDependency for DllEntryDependency {} impl AsDependencyTemplate for DllEntryDependency {} diff --git a/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs b/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs index 7298e221501..f062b472d43 100644 --- a/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs +++ b/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs @@ -58,6 +58,10 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result Date: Fri, 8 Nov 2024 10:50:18 +0800 Subject: [PATCH 17/22] chore: add some annotations --- crates/rspack_core/src/module_graph/module.rs | 2 ++ .../rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/rspack_core/src/module_graph/module.rs b/crates/rspack_core/src/module_graph/module.rs index e0442fc8bb6..16e3f7dd5b3 100644 --- a/crates/rspack_core/src/module_graph/module.rs +++ b/crates/rspack_core/src/module_graph/module.rs @@ -22,6 +22,8 @@ pub struct ModuleGraphModule { pub profile: Option>, pub depth: Option, pub optimization_bailout: Vec, + + // reason why this module can't be concatenated, empty string means it can be concatenated pub concatenation_bail: String, } diff --git a/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs b/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs index f062b472d43..243dbdd4b44 100644 --- a/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs +++ b/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs @@ -58,10 +58,10 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result Date: Fri, 8 Nov 2024 11:51:02 +0800 Subject: [PATCH 18/22] fix: update manifest content item exports type --- crates/node_binding/binding.d.ts | 6 +-- .../src/options/raw_builtins/raw_dll.rs | 37 +++++++++++++------ crates/rspack_binding_values/src/module.rs | 2 +- .../src/dll_reference/delegated_module.rs | 32 +++++++++------- crates/rspack_plugin_dll/src/lib.rs | 29 ++++++++++++++- .../src/lib_manifest_plugin.rs | 13 +++---- packages/rspack/etc/core.api.md | 2 +- packages/rspack/src/lib/DllReferencePlugin.ts | 4 +- .../plugins/webpack/dll-reference-plugin.mdx | 2 +- .../plugins/webpack/dll-reference-plugin.mdx | 2 +- 10 files changed, 84 insertions(+), 45 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 18ee5ccfd14..3b59c8fd945 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -1300,14 +1300,14 @@ export interface RawDllEntryPluginOptions { } export interface RawDllManifest { - content: RawDllManifestContent + content: Record name?: string type?: string } export interface RawDllManifestContentItem { buildMeta?: JsBuildMeta - exports?: Array + exports?: string[] | true id?: string } @@ -1318,7 +1318,7 @@ export interface RawDllReferenceAgencyPluginOptions { scope?: string sourceType?: string type: string - content?: RawDllManifestContent + content?: Record manifest?: RawDllManifest } diff --git a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs index d48a7c20cfb..1de804260d4 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs @@ -1,8 +1,9 @@ +use napi::Either; use napi_derive::napi; use rspack_binding_values::{JsBuildMeta, JsFilename}; use rspack_plugin_dll::{ DllEntryPluginOptions, DllManifest, DllManifestContent, DllManifestContentItem, - DllReferenceAgencyPluginOptions, LibManifestPluginOptions, + DllManifestContentItemExports, DllReferenceAgencyPluginOptions, LibManifestPluginOptions, }; use rustc_hash::FxHashMap as HashMap; use swc_core::atoms::Atom; @@ -72,29 +73,41 @@ pub struct RawDllReferenceAgencyPluginOptions { pub scope: Option, pub source_type: Option, pub r#type: String, - pub content: Option, + pub content: Option>, pub manifest: Option, } -type RawDllManifestContent = HashMap; - #[napi(object, object_to_js = false)] pub struct RawDllManifestContentItem { pub build_meta: Option, - pub exports: Option>, + #[napi(ts_type = "string[] | true")] + pub exports: Option, bool>>, pub id: Option, } impl From for DllManifestContentItem { fn from(value: RawDllManifestContentItem) -> Self { - Self { - build_meta: value.build_meta.map(|meta| meta.into()), - exports: value.exports.map(|exports| { - exports + let raw_exports = value.exports; + + let exports = raw_exports.map(|exports| match exports { + Either::A(seq) => DllManifestContentItemExports::Vec( + seq .into_iter() .map(|export| Atom::from(export)) - .collect::>() - }), + .collect::>(), + ), + Either::B(bool) => { + if bool { + DllManifestContentItemExports::True + } else { + unreachable!() + } + } + }); + + Self { + build_meta: value.build_meta.map(|meta| meta.into()), + exports, id: value.id.into(), } } @@ -102,7 +115,7 @@ impl From for DllManifestContentItem { #[napi(object, object_to_js = false)] pub struct RawDllManifest { - pub content: RawDllManifestContent, + pub content: HashMap, pub name: Option, pub r#type: Option, } diff --git a/crates/rspack_binding_values/src/module.rs b/crates/rspack_binding_values/src/module.rs index 343daa9f312..d96dc8de09c 100644 --- a/crates/rspack_binding_values/src/module.rs +++ b/crates/rspack_binding_values/src/module.rs @@ -605,7 +605,7 @@ impl From for BuildMeta { .into_iter() .map(|export_name| { let first = export_name - .get(0) + .first() .expect("The buildMeta exportsFinalName item should have first value") .clone(); let second = export_name diff --git a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs index 600ed2bd4bd..2c660f7f6f0 100644 --- a/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs +++ b/crates/rspack_plugin_dll/src/dll_reference/delegated_module.rs @@ -5,16 +5,16 @@ use rspack_collections::{Identifiable, Identifier}; use rspack_core::{ impl_module_meta_info, impl_source_map_config, module_raw, module_update_hash, rspack_sources::{BoxSource, OriginalSource, RawSource, Source}, - throw_missing_module_error_block, AsyncDependenciesBlockIdentifier, BuildContext, BuildInfo, - BuildMeta, BuildResult, CodeGenerationResult, Compilation, ConcatenationScope, Context, - DependenciesBlock, DependencyId, FactoryMeta, LibIdentOptions, Module, ModuleDependency, + throw_missing_module_error_block, AsyncDependenciesBlockIdentifier, BoxDependency, BuildContext, + BuildInfo, BuildMeta, BuildResult, CodeGenerationResult, Compilation, ConcatenationScope, + Context, DependenciesBlock, DependencyId, FactoryMeta, LibIdentOptions, Module, ModuleDependency, ModuleType, RuntimeGlobals, RuntimeSpec, SourceType, StaticExportsDependency, StaticExportsSpec, }; use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result}; use rspack_util::source_map::ModuleSourceMapConfig; use super::delegated_source_dependency::DelegatedSourceDependency; -use crate::DllManifestContentItem; +use crate::{DllManifestContentItem, DllManifestContentItemExports}; pub type SourceRequest = String; @@ -95,17 +95,21 @@ impl Module for DelegatedModule { _build_context: BuildContext, _compilation: Option<&Compilation>, ) -> Result { - Ok(BuildResult { - dependencies: vec![ - Box::new(DelegatedSourceDependency::new(self.source_request.clone())), - Box::new(StaticExportsDependency::new( - match self.delegate_data.exports.clone() { - Some(exports) => StaticExportsSpec::Array(exports), - None => StaticExportsSpec::True, + let dependencies = vec![ + Box::new(DelegatedSourceDependency::new(self.source_request.clone())), + Box::new(StaticExportsDependency::new( + match self.delegate_data.exports.clone() { + Some(exports) => match exports { + DllManifestContentItemExports::True => StaticExportsSpec::True, + DllManifestContentItemExports::Vec(vec) => StaticExportsSpec::Array(vec), }, - false, - )), - ], + None => StaticExportsSpec::True, + }, + false, + )) as BoxDependency, + ]; + Ok(BuildResult { + dependencies, build_meta: self.delegate_data.build_meta.clone().unwrap_or_default(), ..Default::default() }) diff --git a/crates/rspack_plugin_dll/src/lib.rs b/crates/rspack_plugin_dll/src/lib.rs index 02cac598ef0..71c146ce3fb 100644 --- a/crates/rspack_plugin_dll/src/lib.rs +++ b/crates/rspack_plugin_dll/src/lib.rs @@ -1,7 +1,7 @@ use rspack_core::{BuildMeta, LibraryType}; use rspack_util::atom::Atom; use rustc_hash::FxHashMap as HashMap; -use serde::Serialize; +use serde::{ser::SerializeSeq, Serialize}; mod dll_entry; mod dll_reference; @@ -10,6 +10,31 @@ mod lib_manifest_plugin; pub type DllManifestContent = HashMap; +#[derive(Debug, Default, Clone)] +pub enum DllManifestContentItemExports { + #[default] + True, + Vec(Vec), +} + +impl Serialize for DllManifestContentItemExports { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match self { + DllManifestContentItemExports::True => serializer.serialize_bool(true), + DllManifestContentItemExports::Vec(vec) => { + let mut seq = serializer.serialize_seq(Some(vec.len()))?; + for item in vec { + seq.serialize_element(item)?; + } + seq.end() + } + } + } +} + #[derive(Debug, Default, Clone, Serialize)] #[serde(rename_all = "camelCase")] pub struct DllManifestContentItem { @@ -17,7 +42,7 @@ pub struct DllManifestContentItem { pub build_meta: Option, #[serde(skip_serializing_if = "Option::is_none")] - pub exports: Option>, + pub exports: Option, #[serde(skip_serializing_if = "Option::is_none")] pub id: Option, diff --git a/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs b/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs index adae941c9e5..bda19d5f31f 100644 --- a/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs +++ b/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs @@ -11,7 +11,9 @@ use rspack_error::{Error, Result}; use rspack_hook::{plugin, plugin_hook}; use rustc_hash::FxHashMap as HashMap; -use crate::{DllManifest, DllManifestContent, DllManifestContentItem}; +use crate::{ + DllManifest, DllManifestContent, DllManifestContentItem, DllManifestContentItemExports, +}; #[derive(Debug, Clone)] pub struct LibManifestPluginOptions { @@ -124,13 +126,8 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { let exports_info = module_graph.get_exports_info(&module.identifier()); let provided_exports = match exports_info.get_provided_exports(&module_graph) { - ProvidedExports::Vec(vec) => { - if vec.is_empty() { - None - } else { - Some(vec) - } - } + ProvidedExports::Vec(vec) => Some(DllManifestContentItemExports::Vec(vec)), + ProvidedExports::True => Some(DllManifestContentItemExports::True), _ => None, }; diff --git a/packages/rspack/etc/core.api.md b/packages/rspack/etc/core.api.md index 0899115ab72..072239baaff 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -1451,7 +1451,7 @@ export interface DllReferencePluginOptionsContent { [k: string]: { buildMeta?: JsBuildMeta; exports?: string[] | true; - id: number | string; + id: string; }; } diff --git a/packages/rspack/src/lib/DllReferencePlugin.ts b/packages/rspack/src/lib/DllReferencePlugin.ts index 95a82e333a2..b7be3735d1d 100644 --- a/packages/rspack/src/lib/DllReferencePlugin.ts +++ b/packages/rspack/src/lib/DllReferencePlugin.ts @@ -133,14 +133,14 @@ export interface DllReferencePluginOptionsContent { /** * Module ID. */ - id: number | string; + id: string; }; } const dllReferencePluginOptionsContentItem = z.object({ buildMeta: z.custom().optional(), exports: z.array(z.string()).or(z.literal(true)).optional(), - id: z.union([z.number(), z.string()]) + id: z.string() }); const dllReferencePluginOptionsContent = z.record( diff --git a/website/docs/en/plugins/webpack/dll-reference-plugin.mdx b/website/docs/en/plugins/webpack/dll-reference-plugin.mdx index abb4a99dbe7..73fa97a6337 100644 --- a/website/docs/en/plugins/webpack/dll-reference-plugin.mdx +++ b/website/docs/en/plugins/webpack/dll-reference-plugin.mdx @@ -30,7 +30,7 @@ type DllReferencePluginOptionsContent = { /** * Module ID. */ - id: number | string; + id: string; }; }; diff --git a/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx b/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx index b38d264a4fa..e0089e6e21e 100644 --- a/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx +++ b/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx @@ -29,7 +29,7 @@ type DllReferencePluginOptionsContent = { /** * Module ID. */ - id: number | string; + id: string; }; }; From a34740c6ad16d7e82b6117e9dcd17a6f54319566 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Fri, 8 Nov 2024 11:53:01 +0800 Subject: [PATCH 19/22] fix: update dll manifest types --- packages/rspack/src/lib/DllReferencePlugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rspack/src/lib/DllReferencePlugin.ts b/packages/rspack/src/lib/DllReferencePlugin.ts index b7be3735d1d..c312128fd69 100644 --- a/packages/rspack/src/lib/DllReferencePlugin.ts +++ b/packages/rspack/src/lib/DllReferencePlugin.ts @@ -133,14 +133,14 @@ export interface DllReferencePluginOptionsContent { /** * Module ID. */ - id: string; + id?: string; }; } const dllReferencePluginOptionsContentItem = z.object({ buildMeta: z.custom().optional(), exports: z.array(z.string()).or(z.literal(true)).optional(), - id: z.string() + id: z.string().optional() }); const dllReferencePluginOptionsContent = z.record( From 7015b948210752994ace15557f47f8a50b23117e Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Fri, 8 Nov 2024 11:53:51 +0800 Subject: [PATCH 20/22] chore:update docs --- packages/rspack/etc/core.api.md | 2 +- website/docs/en/plugins/webpack/dll-reference-plugin.mdx | 2 +- website/docs/zh/plugins/webpack/dll-reference-plugin.mdx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/rspack/etc/core.api.md b/packages/rspack/etc/core.api.md index 072239baaff..96d157df13d 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -1451,7 +1451,7 @@ export interface DllReferencePluginOptionsContent { [k: string]: { buildMeta?: JsBuildMeta; exports?: string[] | true; - id: string; + id?: string; }; } diff --git a/website/docs/en/plugins/webpack/dll-reference-plugin.mdx b/website/docs/en/plugins/webpack/dll-reference-plugin.mdx index 73fa97a6337..1555dfd6886 100644 --- a/website/docs/en/plugins/webpack/dll-reference-plugin.mdx +++ b/website/docs/en/plugins/webpack/dll-reference-plugin.mdx @@ -30,7 +30,7 @@ type DllReferencePluginOptionsContent = { /** * Module ID. */ - id: string; + id?: string; }; }; diff --git a/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx b/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx index e0089e6e21e..864a7fbcf39 100644 --- a/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx +++ b/website/docs/zh/plugins/webpack/dll-reference-plugin.mdx @@ -29,7 +29,7 @@ type DllReferencePluginOptionsContent = { /** * Module ID. */ - id: string; + id?: string; }; }; From 7a03d0c4d13bccb331b6b1de1009b55ccc691fff Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Fri, 8 Nov 2024 11:56:30 +0800 Subject: [PATCH 21/22] chore: clippy fix --- .../src/options/raw_builtins/raw_dll.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs index 1de804260d4..89f886bfe16 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs @@ -90,12 +90,9 @@ impl From for DllManifestContentItem { let raw_exports = value.exports; let exports = raw_exports.map(|exports| match exports { - Either::A(seq) => DllManifestContentItemExports::Vec( - seq - .into_iter() - .map(|export| Atom::from(export)) - .collect::>(), - ), + Either::A(seq) => { + DllManifestContentItemExports::Vec(seq.into_iter().map(Atom::from).collect::>()) + } Either::B(bool) => { if bool { DllManifestContentItemExports::True @@ -108,7 +105,7 @@ impl From for DllManifestContentItem { Self { build_meta: value.build_meta.map(|meta| meta.into()), exports, - id: value.id.into(), + id: value.id, } } } @@ -128,8 +125,8 @@ impl From for DllManifest { .into_iter() .map(|(k, v)| (k, v.into())) .collect::(), - name: value.name.map(|n| n.into()), - r#type: value.r#type.into(), + name: value.name, + r#type: value.r#type, } } } From d075bcc4994e56077c6fee2b507b7e2a943b300b Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Mon, 11 Nov 2024 10:41:26 +0800 Subject: [PATCH 22/22] fix: bailout use build_info.module_concatenation_bailout --- .../src/compiler/make/repair/build.rs | 35 +++++++++------ crates/rspack_core/src/module_graph/module.rs | 8 ---- .../src/flag_all_modules_as_used_plugin.rs | 45 +++++++++++-------- .../src/plugin/module_concatenation_plugin.rs | 16 ------- 4 files changed, 47 insertions(+), 57 deletions(-) diff --git a/crates/rspack_core/src/compiler/make/repair/build.rs b/crates/rspack_core/src/compiler/make/repair/build.rs index be4bfe52ee8..0df775b3c97 100644 --- a/crates/rspack_core/src/compiler/make/repair/build.rs +++ b/crates/rspack_core/src/compiler/make/repair/build.rs @@ -6,7 +6,7 @@ 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, BuildInfo, BuildResult, CompilerOptions, DependencyParents, Module, ModuleProfile, ResolverFactory, SharedPluginDriver, }; @@ -56,12 +56,6 @@ impl Task for BuildTask { ) .await; - plugin_driver - .compilation_hooks - .succeed_module - .call(&mut module) - .await?; - let build_result = result.map(|t| { let diagnostics = module .clone_diagnostics() @@ -80,6 +74,7 @@ impl Task for BuildTask { vec![Box::new(BuildResultTask { module, build_result: Box::new(build_result), + plugin_driver, diagnostics, current_profile, })] @@ -92,6 +87,7 @@ struct BuildResultTask { pub module: Box, pub build_result: Box, pub diagnostics: Vec, + pub plugin_driver: SharedPluginDriver, pub current_profile: Option>, } #[async_trait::async_trait] @@ -105,8 +101,22 @@ impl Task for BuildResultTask { build_result, diagnostics, current_profile, + plugin_driver, } = *self; + module.set_build_info(build_result.build_info); + module.set_build_meta(build_result.build_meta); + + plugin_driver + .compilation_hooks + .succeed_module + .call(&mut module) + .await?; + + let build_info_default = BuildInfo::default(); + + let build_info = module.build_info().unwrap_or(&build_info_default); + let artifact = &mut context.artifact; let module_graph = &mut MakeTaskContext::get_module_graph_mut(&mut artifact.module_graph_partial); @@ -122,16 +132,16 @@ impl Task for BuildResultTask { .extend(build_result.optimization_bailouts); artifact .file_dependencies - .add_batch_file(&build_result.build_info.file_dependencies); + .add_batch_file(&build_info.file_dependencies); artifact .context_dependencies - .add_batch_file(&build_result.build_info.context_dependencies); + .add_batch_file(&build_info.context_dependencies); artifact .missing_dependencies - .add_batch_file(&build_result.build_info.missing_dependencies); + .add_batch_file(&build_info.missing_dependencies); artifact .build_dependencies - .add_batch_file(&build_result.build_info.build_dependencies); + .add_batch_file(&build_info.build_dependencies); let mut queue = VecDeque::new(); let mut all_dependencies = vec![]; @@ -181,9 +191,6 @@ impl Task for BuildResultTask { let module_identifier = module.identifier(); - module.set_build_info(build_result.build_info); - module.set_build_meta(build_result.build_meta); - module_graph.add_module(module); Ok(vec![Box::new(ProcessDependenciesTask { diff --git a/crates/rspack_core/src/module_graph/module.rs b/crates/rspack_core/src/module_graph/module.rs index 16e3f7dd5b3..ef8004fe826 100644 --- a/crates/rspack_core/src/module_graph/module.rs +++ b/crates/rspack_core/src/module_graph/module.rs @@ -22,9 +22,6 @@ pub struct ModuleGraphModule { pub profile: Option>, pub depth: Option, pub optimization_bailout: Vec, - - // reason why this module can't be concatenated, empty string means it can be concatenated - pub concatenation_bail: String, } impl ModuleGraphModule { @@ -42,7 +39,6 @@ impl ModuleGraphModule { profile: None, depth: None, optimization_bailout: vec![], - concatenation_bail: Default::default(), } } @@ -83,10 +79,6 @@ impl ModuleGraphModule { self.profile.as_deref() } - pub fn add_concatenation_bail_reason(&mut self, reason: &str) { - self.concatenation_bail += &format!(", {}", reason); - } - pub fn set_issuer_if_unset(&mut self, issuer: Option) { if matches!(self.issuer, ModuleIssuer::Unset) { self.issuer = ModuleIssuer::from_identifier(issuer); diff --git a/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs b/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs index 243dbdd4b44..4646e618d51 100644 --- a/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs +++ b/crates/rspack_plugin_dll/src/flag_all_modules_as_used_plugin.rs @@ -1,8 +1,8 @@ use rspack_collections::IdentifierSet; use rspack_core::{ get_entry_runtime, merge_runtime, ApplyContext, BoxModule, Compilation, - CompilationOptimizeDependencies, CompilerOptions, FactoryMeta, ModuleFactoryCreateData, - NormalModuleCreateData, NormalModuleFactoryModule, Plugin, PluginContext, RuntimeSpec, + CompilationOptimizeDependencies, CompilationSucceedModule, CompilerOptions, FactoryMeta, Plugin, + PluginContext, RuntimeSpec, }; use rspack_error::Result; use rspack_hook::{plugin, plugin_hook}; @@ -33,9 +33,9 @@ impl Plugin for FlagAllModulesAsUsedPlugin { ctx .context - .normal_module_factory_hooks - .module - .tap(nmf_module::new(self)); + .compilation_hooks + .succeed_module + .tap(succeed_module::new(self)); Ok(()) } @@ -58,30 +58,37 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result Result<()> { +#[plugin_hook(CompilationSucceedModule for FlagAllModulesAsUsedPlugin)] +async fn succeed_module(&self, module: &mut BoxModule) -> Result<()> { // set all modules have effects. To avoid any module remove by tree shaking. // see: https://github.com/webpack/webpack/blob/4b4ca3bb53f36a5b8fc6bc1bd976ed7af161bd80/lib/FlagAllModulesAsUsedPlugin.js#L43-L47 module.set_factory_meta(FactoryMeta { side_effect_free: Some(false), }); + let module_concatenation_bailout = module + .build_info() + .and_then(|info| info.module_concatenation_bailout.as_ref()); + + if module_concatenation_bailout.is_none() { + // webpack avoid those modules be concatenated using add a virtual module_graph_connection. + // see: https://github.com/webpack/webpack/blob/4b4ca3bb53f36a5b8fc6bc1bd976ed7af161bd80/lib/FlagAllModulesAsUsedPlugin.js#L42 + // Rspack need incremental build, so we should not add virtual connection to module. + // We can add a bail reason to avoid those modules be concatenated. + let mut build_info = module.build_info().cloned().unwrap_or_default(); + build_info.module_concatenation_bailout = Some(format!( + "Module {} is referenced by {}", + module.identifier(), + &self.explanation + )); + + module.set_build_info(build_info); + } + Ok(()) } 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 d5c89d507a3..daab3e61b2b 100644 --- a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs @@ -304,22 +304,6 @@ impl ModuleConcatenationPlugin { } } - // Every module_graph_module that tags concatenation_bail should be return a warning problem to avoid this module concate. - if let Some(concatenation_bail) = module_graph - .module_graph_module_by_identifier(module_id) - .map(|mgm| &mgm.concatenation_bail) - { - if !concatenation_bail.is_empty() { - let problem = Warning::Problem(format!( - "Module {} is bail by: {}", - module_readable_identifier, concatenation_bail, - )); - statistics.incorrect_dependency += 1; - failure_cache.insert(*module_id, problem.clone()); - return Some(problem); - } - }; - let mut incoming_connections_from_modules = HashMap::default(); for (origin_module, connections) in incoming_connections.iter() { if let Some(origin_module) = origin_module {