Skip to content

Commit

Permalink
fix: only register SplitChunkPlugin once (#4766)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi authored Nov 24, 2023
1 parent f5713bb commit ea50644
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 26 deletions.
2 changes: 2 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ export const enum BuiltinPluginName {
WebWorkerTemplatePlugin = 'WebWorkerTemplatePlugin',
MergeDuplicateChunksPlugin = 'MergeDuplicateChunksPlugin',
ContainerPlugin = 'ContainerPlugin',
SplitChunksPlugin = 'SplitChunksPlugin',
OldSplitChunksPlugin = 'OldSplitChunksPlugin',
HttpExternalsRspackPlugin = 'HttpExternalsRspackPlugin',
CopyRspackPlugin = 'CopyRspackPlugin',
HtmlRspackPlugin = 'HtmlRspackPlugin',
Expand Down
14 changes: 13 additions & 1 deletion crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub use self::{
};
use crate::{
RawEntryPluginOptions, RawExternalItemWrapper, RawExternalsPluginOptions,
RawHttpExternalsRspackPluginOptions, RawOptionsApply,
RawHttpExternalsRspackPluginOptions, RawOptionsApply, RawSplitChunksOptions,
};

#[napi(string_enum)]
Expand All @@ -72,6 +72,8 @@ pub enum BuiltinPluginName {
WebWorkerTemplatePlugin,
MergeDuplicateChunksPlugin,
ContainerPlugin,
SplitChunksPlugin,
OldSplitChunksPlugin,

// rspack specific plugins
HttpExternalsRspackPlugin,
Expand Down Expand Up @@ -184,6 +186,16 @@ impl RawOptionsApply for BuiltinPlugin {
.boxed(),
);
}
BuiltinPluginName::SplitChunksPlugin => {
use rspack_plugin_split_chunks_new::SplitChunksPlugin;
let options = downcast_into::<RawSplitChunksOptions>(self.options)?.into();
plugins.push(SplitChunksPlugin::new(options).boxed());
}
BuiltinPluginName::OldSplitChunksPlugin => {
use rspack_plugin_split_chunks::SplitChunksPlugin;
let options = downcast_into::<RawSplitChunksOptions>(self.options)?.into();
plugins.push(SplitChunksPlugin::new(options).boxed());
}

// rspack specific plugins
BuiltinPluginName::HttpExternalsRspackPlugin => {
Expand Down
12 changes: 0 additions & 12 deletions crates/rspack_binding_options/src/options/raw_optimization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use rspack_ids::{
DeterministicChunkIdsPlugin, DeterministicModuleIdsPlugin, NamedChunkIdsPlugin,
NamedModuleIdsPlugin,
};
use rspack_plugin_split_chunks::SplitChunksPlugin;
use serde::Deserialize;

use crate::{RawOptionsApply, RawSplitChunksOptions};
Expand Down Expand Up @@ -36,17 +35,6 @@ impl RawOptionsApply for RawOptimizationOptions {
self,
plugins: &mut Vec<Box<dyn rspack_core::Plugin>>,
) -> Result<Self::Options, rspack_error::Error> {
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(),
Expand Down
10 changes: 1 addition & 9 deletions crates/rspack_plugin_externals/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@ use rspack_core::{
static UNSPECIFIED_EXTERNAL_TYPE_REGEXP: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^[a-z0-9-]+ ").expect("Invalid regex"));

#[derive(Debug)]
pub struct ExternalsPlugin {
externals: Vec<ExternalItem>,
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<ExternalItem>) -> Self {
Self { externals, r#type }
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_plugin_javascript/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ impl JsPlugin {
let source = args
.compilation
.plugin_driver
.clone()
.render_chunk(RenderChunkArgs {
compilation: args.compilation,
chunk_ukey: &args.chunk_ukey,
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class Compiler {
options: RspackOptionsNormalized;
#disabledHooks: string[];
parentCompilation?: Compilation;

constructor(context: string, options: RspackOptionsNormalized) {
this.outputFileSystem = fs;
this.options = options;
Expand Down Expand Up @@ -194,6 +195,7 @@ class Compiler {
this.options.output.hashFunction
);
}

/**
* Lazy initialize instance so it could access the changed options
*/
Expand Down
22 changes: 22 additions & 0 deletions packages/rspack/src/builtin-plugin/SplitChunksPlugin.ts
Original file line number Diff line number Diff line change
@@ -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;
}
);
4 changes: 3 additions & 1 deletion packages/rspack/src/builtin-plugin/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/builtin-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ function getRawOptimization(
};
}

function toRawSplitChunksOptions(
export function toRawSplitChunksOptions(
sc?: OptimizationSplitChunksOptions
): RawOptions["optimization"]["splitChunks"] | undefined {
if (!sc) {
Expand Down
16 changes: 15 additions & 1 deletion packages/rspack/src/rspackOptionsApply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
ModuleChunkFormatPlugin,
NodeTargetPlugin,
DefinePlugin,
MergeDuplicateChunksPlugin
MergeDuplicateChunksPlugin,
SplitChunksPlugin,
OldSplitChunksPlugin
} from "./builtin-plugin";

export function optionsApply_compat(
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("should never run this file because child compiler only compile not emit", () => {
expect(1).toBe(2);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("should build success", () => {
expect(1).toBe(1);
});
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
};

0 comments on commit ea50644

Please sign in to comment.