From ea50644f82eb26445e6c504c9dc20565fcfff871 Mon Sep 17 00:00:00 2001 From: bohan Date: Fri, 24 Nov 2023 14:08:33 +0800 Subject: [PATCH] fix: only register `SplitChunkPlugin` once (#4766) --- crates/node_binding/binding.d.ts | 2 ++ .../src/options/raw_builtins/mod.rs | 14 +++++++++- .../src/options/raw_optimization.rs | 12 -------- crates/rspack_plugin_externals/src/plugin.rs | 10 +------ .../src/plugin/mod.rs | 1 - packages/rspack/src/Compiler.ts | 2 ++ .../src/builtin-plugin/SplitChunksPlugin.ts | 22 +++++++++++++++ packages/rspack/src/builtin-plugin/base.ts | 4 ++- packages/rspack/src/builtin-plugin/index.ts | 1 + packages/rspack/src/config/adapter.ts | 2 +- packages/rspack/src/rspackOptionsApply.ts | 16 ++++++++++- .../chunk-loading/issue-4754/child-entry.js | 3 ++ .../chunk-loading/issue-4754/index.js | 3 ++ .../issue-4754/webpack.config.js | 28 +++++++++++++++++++ 14 files changed, 94 insertions(+), 26 deletions(-) create mode 100644 packages/rspack/src/builtin-plugin/SplitChunksPlugin.ts create mode 100644 packages/rspack/tests/configCases/chunk-loading/issue-4754/child-entry.js create mode 100644 packages/rspack/tests/configCases/chunk-loading/issue-4754/index.js create mode 100644 packages/rspack/tests/configCases/chunk-loading/issue-4754/webpack.config.js diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index e8ce45a471f9..c903402644bb 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -143,6 +143,8 @@ export const enum BuiltinPluginName { WebWorkerTemplatePlugin = 'WebWorkerTemplatePlugin', MergeDuplicateChunksPlugin = 'MergeDuplicateChunksPlugin', ContainerPlugin = 'ContainerPlugin', + SplitChunksPlugin = 'SplitChunksPlugin', + OldSplitChunksPlugin = 'OldSplitChunksPlugin', HttpExternalsRspackPlugin = 'HttpExternalsRspackPlugin', CopyRspackPlugin = 'CopyRspackPlugin', HtmlRspackPlugin = 'HtmlRspackPlugin', 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 9fc732116807..1d50c4369ee1 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/mod.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/mod.rs @@ -46,7 +46,7 @@ pub use self::{ }; use crate::{ RawEntryPluginOptions, RawExternalItemWrapper, RawExternalsPluginOptions, - RawHttpExternalsRspackPluginOptions, RawOptionsApply, + RawHttpExternalsRspackPluginOptions, RawOptionsApply, RawSplitChunksOptions, }; #[napi(string_enum)] @@ -72,6 +72,8 @@ pub enum BuiltinPluginName { WebWorkerTemplatePlugin, MergeDuplicateChunksPlugin, ContainerPlugin, + SplitChunksPlugin, + OldSplitChunksPlugin, // rspack specific plugins HttpExternalsRspackPlugin, @@ -184,6 +186,16 @@ impl RawOptionsApply for BuiltinPlugin { .boxed(), ); } + BuiltinPluginName::SplitChunksPlugin => { + use rspack_plugin_split_chunks_new::SplitChunksPlugin; + let options = downcast_into::(self.options)?.into(); + plugins.push(SplitChunksPlugin::new(options).boxed()); + } + BuiltinPluginName::OldSplitChunksPlugin => { + use rspack_plugin_split_chunks::SplitChunksPlugin; + let options = downcast_into::(self.options)?.into(); + plugins.push(SplitChunksPlugin::new(options).boxed()); + } // rspack specific plugins BuiltinPluginName::HttpExternalsRspackPlugin => { diff --git a/crates/rspack_binding_options/src/options/raw_optimization.rs b/crates/rspack_binding_options/src/options/raw_optimization.rs index 60d96bb5678d..158a247f78ef 100644 --- a/crates/rspack_binding_options/src/options/raw_optimization.rs +++ b/crates/rspack_binding_options/src/options/raw_optimization.rs @@ -6,7 +6,6 @@ use rspack_ids::{ DeterministicChunkIdsPlugin, DeterministicModuleIdsPlugin, NamedChunkIdsPlugin, NamedModuleIdsPlugin, }; -use rspack_plugin_split_chunks::SplitChunksPlugin; use serde::Deserialize; use crate::{RawOptionsApply, RawSplitChunksOptions}; @@ -36,17 +35,6 @@ impl RawOptionsApply for RawOptimizationOptions { self, plugins: &mut Vec>, ) -> Result { - if let Some(options) = self.split_chunks { - let split_chunks_plugin = IS_ENABLE_NEW_SPLIT_CHUNKS.with(|is_enable_new_split_chunks| { - if *is_enable_new_split_chunks { - rspack_plugin_split_chunks_new::SplitChunksPlugin::new(options.into()).boxed() - } else { - SplitChunksPlugin::new(options.into()).boxed() - } - }); - - plugins.push(split_chunks_plugin); - } let chunk_ids_plugin = match self.chunk_ids.as_ref() { "named" => NamedChunkIdsPlugin::new(None, None).boxed(), "deterministic" => DeterministicChunkIdsPlugin::default().boxed(), diff --git a/crates/rspack_plugin_externals/src/plugin.rs b/crates/rspack_plugin_externals/src/plugin.rs index 1003b5d5390b..478d3f30561c 100644 --- a/crates/rspack_plugin_externals/src/plugin.rs +++ b/crates/rspack_plugin_externals/src/plugin.rs @@ -12,20 +12,12 @@ use rspack_core::{ static UNSPECIFIED_EXTERNAL_TYPE_REGEXP: Lazy = Lazy::new(|| Regex::new(r"^[a-z0-9-]+ ").expect("Invalid regex")); +#[derive(Debug)] pub struct ExternalsPlugin { externals: Vec, r#type: ExternalType, } -impl Debug for ExternalsPlugin { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("ExternalPlugin") - .field("externals", &"Function") - .field("r#type", &self.r#type) - .finish() - } -} - impl ExternalsPlugin { pub fn new(r#type: ExternalType, externals: Vec) -> Self { Self { externals, r#type } diff --git a/crates/rspack_plugin_javascript/src/plugin/mod.rs b/crates/rspack_plugin_javascript/src/plugin/mod.rs index 3ade24f4da30..db9a8efdff43 100644 --- a/crates/rspack_plugin_javascript/src/plugin/mod.rs +++ b/crates/rspack_plugin_javascript/src/plugin/mod.rs @@ -363,7 +363,6 @@ impl JsPlugin { let source = args .compilation .plugin_driver - .clone() .render_chunk(RenderChunkArgs { compilation: args.compilation, chunk_ukey: &args.chunk_ukey, diff --git a/packages/rspack/src/Compiler.ts b/packages/rspack/src/Compiler.ts index 7c10ae076a29..7997e9bf1466 100644 --- a/packages/rspack/src/Compiler.ts +++ b/packages/rspack/src/Compiler.ts @@ -122,6 +122,7 @@ class Compiler { options: RspackOptionsNormalized; #disabledHooks: string[]; parentCompilation?: Compilation; + constructor(context: string, options: RspackOptionsNormalized) { this.outputFileSystem = fs; this.options = options; @@ -194,6 +195,7 @@ class Compiler { this.options.output.hashFunction ); } + /** * Lazy initialize instance so it could access the changed options */ diff --git a/packages/rspack/src/builtin-plugin/SplitChunksPlugin.ts b/packages/rspack/src/builtin-plugin/SplitChunksPlugin.ts new file mode 100644 index 000000000000..67973cf0c9b1 --- /dev/null +++ b/packages/rspack/src/builtin-plugin/SplitChunksPlugin.ts @@ -0,0 +1,22 @@ +import assert from "assert"; +import { toRawSplitChunksOptions } from "../config/adapter"; +import { type OptimizationSplitChunksOptions } from "../config/zod"; +import { BuiltinPluginName, create } from "./base"; + +export const SplitChunksPlugin = create( + BuiltinPluginName.SplitChunksPlugin, + (options: OptimizationSplitChunksOptions) => { + let raw = toRawSplitChunksOptions(options); + assert(typeof raw !== "undefined"); + return raw; + } +); + +export const OldSplitChunksPlugin = create( + BuiltinPluginName.SplitChunksPlugin, + (options: OptimizationSplitChunksOptions) => { + let raw = toRawSplitChunksOptions(options); + assert(typeof raw !== "undefined"); + return raw; + } +); diff --git a/packages/rspack/src/builtin-plugin/base.ts b/packages/rspack/src/builtin-plugin/base.ts index 3b748585e87c..4807a16d112c 100644 --- a/packages/rspack/src/builtin-plugin/base.ts +++ b/packages/rspack/src/builtin-plugin/base.ts @@ -26,7 +26,9 @@ export enum BuiltinPluginName { LimitChunkCountPlugin = "LimitChunkCountPlugin", WebWorkerTemplatePlugin = "WebWorkerTemplatePlugin", MergeDuplicateChunksPlugin = "MergeDuplicateChunksPlugin", - ContainerPlugin = "ContainerPlugin" + ContainerPlugin = "ContainerPlugin", + SplitChunksPlugin = "SplitChunksPlugin", + OldSplitChunksPlugin = "OldSplitChunksPlugin" } export abstract class RspackBuiltinPlugin implements RspackPluginInstance { diff --git a/packages/rspack/src/builtin-plugin/index.ts b/packages/rspack/src/builtin-plugin/index.ts index 01e5a9811ab6..dc6ef7c038a7 100644 --- a/packages/rspack/src/builtin-plugin/index.ts +++ b/packages/rspack/src/builtin-plugin/index.ts @@ -19,6 +19,7 @@ export * from "./HotModuleReplacementPlugin"; export * from "./WebWorkerTemplatePlugin"; export * from "./LimitChunkCountPlugin"; export * from "./MergeDuplicateChunksPlugin"; +export * from "./SplitChunksPlugin"; export * from "./HtmlRspackPlugin"; export * from "./CopyRspackPlugin"; diff --git a/packages/rspack/src/config/adapter.ts b/packages/rspack/src/config/adapter.ts index 4f787b089d12..eab75d833b10 100644 --- a/packages/rspack/src/config/adapter.ts +++ b/packages/rspack/src/config/adapter.ts @@ -716,7 +716,7 @@ function getRawOptimization( }; } -function toRawSplitChunksOptions( +export function toRawSplitChunksOptions( sc?: OptimizationSplitChunksOptions ): RawOptions["optimization"]["splitChunks"] | undefined { if (!sc) { diff --git a/packages/rspack/src/rspackOptionsApply.ts b/packages/rspack/src/rspackOptionsApply.ts index 1f46de2611bb..4ce9cadbfbd2 100644 --- a/packages/rspack/src/rspackOptionsApply.ts +++ b/packages/rspack/src/rspackOptionsApply.ts @@ -34,7 +34,9 @@ import { ModuleChunkFormatPlugin, NodeTargetPlugin, DefinePlugin, - MergeDuplicateChunksPlugin + MergeDuplicateChunksPlugin, + SplitChunksPlugin, + OldSplitChunksPlugin } from "./builtin-plugin"; export function optionsApply_compat( @@ -189,6 +191,18 @@ export class RspackOptionsApply { if (options.builtins.devFriendlySplitChunks) { options.optimization.splitChunks = undefined; } + + if ( + options.optimization.splitChunks && + options.experiments.newSplitChunks === false + ) { + new OldSplitChunksPlugin(options.optimization.splitChunks).apply( + compiler + ); + } else if (options.optimization.splitChunks) { + new SplitChunksPlugin(options.optimization.splitChunks).apply(compiler); + } + if (options.optimization.nodeEnv) { new DefinePlugin({ "process.env.NODE_ENV": JSON.stringify(options.optimization.nodeEnv) diff --git a/packages/rspack/tests/configCases/chunk-loading/issue-4754/child-entry.js b/packages/rspack/tests/configCases/chunk-loading/issue-4754/child-entry.js new file mode 100644 index 000000000000..6c8d331c7177 --- /dev/null +++ b/packages/rspack/tests/configCases/chunk-loading/issue-4754/child-entry.js @@ -0,0 +1,3 @@ +it("should never run this file because child compiler only compile not emit", () => { + expect(1).toBe(2); +}); diff --git a/packages/rspack/tests/configCases/chunk-loading/issue-4754/index.js b/packages/rspack/tests/configCases/chunk-loading/issue-4754/index.js new file mode 100644 index 000000000000..588f31f0046f --- /dev/null +++ b/packages/rspack/tests/configCases/chunk-loading/issue-4754/index.js @@ -0,0 +1,3 @@ +it("should build success", () => { + expect(1).toBe(1); +}); diff --git a/packages/rspack/tests/configCases/chunk-loading/issue-4754/webpack.config.js b/packages/rspack/tests/configCases/chunk-loading/issue-4754/webpack.config.js new file mode 100644 index 000000000000..5ead7ff6f4d5 --- /dev/null +++ b/packages/rspack/tests/configCases/chunk-loading/issue-4754/webpack.config.js @@ -0,0 +1,28 @@ +const path = require("path"); + +module.exports = { + plugins: [ + { + apply(compiler) { + compiler.hooks.make.tap("make", compilation => { + const childEntry = path.resolve(__dirname, "./child-entry.js"); + const childCompiler = compilation.createChildCompiler("name", {}, [ + new compiler.webpack.EntryPlugin(compiler.context, childEntry) + ]); + childCompiler.compile(() => {}); + }); + } + } + ], + optimization: { + splitChunks: { + cacheGroups: { + singleVendor: { + chunks: "all", + enforce: true, + name: "vendor" + } + } + } + } +};