Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce SplitSharedModulesPlugin #8128

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export enum BuiltinPluginName {
WebWorkerTemplatePlugin = 'WebWorkerTemplatePlugin',
MergeDuplicateChunksPlugin = 'MergeDuplicateChunksPlugin',
SplitChunksPlugin = 'SplitChunksPlugin',
SplitSharedModulesPlugin = 'SplitSharedModulesPlugin',
ShareRuntimePlugin = 'ShareRuntimePlugin',
ContainerPlugin = 'ContainerPlugin',
ContainerReferencePlugin = 'ContainerReferencePlugin',
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_binding_options/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ rspack_plugin_runtime_chunk = { version = "0.1.0", path = "../rspack_p
rspack_plugin_schemes = { version = "0.1.0", path = "../rspack_plugin_schemes" }
rspack_plugin_size_limits = { version = "0.1.0", path = "../rspack_plugin_size_limits" }
rspack_plugin_split_chunks = { version = "0.1.0", path = "../rspack_plugin_split_chunks" }
rspack_plugin_split_shared_modules = { version = "0.1.0", path = "../rspack_plugin_split_shared_modules" }
rspack_plugin_swc_js_minimizer = { version = "0.1.0", path = "../rspack_plugin_swc_js_minimizer" }
rspack_plugin_warn_sensitive_module = { version = "0.1.0", path = "../rspack_plugin_warn_sensitive_module" }
rspack_plugin_wasm = { version = "0.1.0", path = "../rspack_plugin_wasm" }
Expand Down
5 changes: 5 additions & 0 deletions crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use rspack_plugin_runtime::{
use rspack_plugin_runtime_chunk::RuntimeChunkPlugin;
use rspack_plugin_schemes::{DataUriPlugin, FileUriPlugin};
use rspack_plugin_size_limits::SizeLimitsPlugin;
use rspack_plugin_split_shared_modules::SplitSharedModulesPlugin;
use rspack_plugin_swc_js_minimizer::SwcJsMinimizerRspackPlugin;
use rspack_plugin_warn_sensitive_module::WarnCaseSensitiveModulesPlugin;
use rspack_plugin_wasm::{
Expand Down Expand Up @@ -126,6 +127,7 @@ pub enum BuiltinPluginName {
WebWorkerTemplatePlugin,
MergeDuplicateChunksPlugin,
SplitChunksPlugin,
SplitSharedModulesPlugin,
ShareRuntimePlugin,
ContainerPlugin,
ContainerReferencePlugin,
Expand Down Expand Up @@ -300,6 +302,9 @@ impl BuiltinPlugin {
let options = downcast_into::<RawSplitChunksOptions>(self.options)?.into();
plugins.push(SplitChunksPlugin::new(options).boxed());
}
BuiltinPluginName::SplitSharedModulesPlugin => {
plugins.push(SplitSharedModulesPlugin::new().boxed());
}
BuiltinPluginName::ShareRuntimePlugin => {
plugins.push(ShareRuntimePlugin::new(downcast_into::<bool>(self.options)?).boxed())
}
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_plugin_split_chunks/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@

#[plugin_hook(CompilationOptimizeChunks for SplitChunksPlugin, stage = Compilation::OPTIMIZE_CHUNKS_STAGE_ADVANCED)]
fn optimize_chunks(&self, compilation: &mut Compilation) -> Result<Option<bool>> {
let now = std::time::Instant::now();
self.inner_impl(compilation)?;
dbg!(now.elapsed().as_millis());

Check failure on line 169 in crates/rspack_plugin_split_chunks/src/plugin/mod.rs

View workflow job for this annotation

GitHub Actions / Rust check

the `dbg!` macro is intended as a debugging tool
Ok(None)
}

Expand Down
22 changes: 22 additions & 0 deletions crates/rspack_plugin_split_shared_modules/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
edition = "2021"
name = "rspack_plugin_split_shared_modules"
version = "0.1.0"

[lints]
workspace = true

[dependencies]
dashmap = { workspace = true }
derivative = { workspace = true }
rayon = { workspace = true }
regex = { workspace = true }
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_hash = { version = "0.1.0", path = "../rspack_hash" }
rspack_hook = { version = "0.1.0", path = "../rspack_hook" }
rspack_regex = { version = "0.1.0", path = "../rspack_regex" }
rspack_util = { version = "0.1.0", path = "../rspack_util" }
rustc-hash = { workspace = true }
tracing = { workspace = true }
22 changes: 22 additions & 0 deletions crates/rspack_plugin_split_shared_modules/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
88 changes: 88 additions & 0 deletions crates/rspack_plugin_split_shared_modules/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::sync::Arc;

use rspack_core::{
Chunk, ChunkUkey, Compilation, CompilationOptimizeChunks, ModuleIdentifier, Plugin,
};
use rspack_error::Result;
use rspack_hook::{plugin, plugin_hook};
use rustc_hash::FxHashMap;

#[derive(Debug)]
#[plugin]
pub struct SplitSharedModulesPlugin {}

impl SplitSharedModulesPlugin {
pub fn new() -> Self {

Check failure on line 15 in crates/rspack_plugin_split_shared_modules/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust check

you should consider adding a `Default` implementation for `SplitSharedModulesPlugin`
Self {
inner: Arc::new(SplitSharedModulesPluginInner {}),
}
}
}

#[plugin_hook(CompilationOptimizeChunks for SplitSharedModulesPlugin)]
fn optimize_chunks(&self, compilation: &mut Compilation) -> Result<Option<bool>> {
let now = std::time::Instant::now();
let module_graph = compilation.get_module_graph();
let chunk_graph = &compilation.chunk_graph;

let mut chunk_map: FxHashMap<Vec<ChunkUkey>, Vec<ModuleIdentifier>> = FxHashMap::default();

for identifier in module_graph.modules().keys() {
let chunks = chunk_graph.get_module_chunks(*identifier);
let mut sorted_chunks = chunks.into_iter().copied().collect::<Vec<_>>();

Check failure on line 32 in crates/rspack_plugin_split_shared_modules/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust check

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `HashSet`
sorted_chunks.sort();
chunk_map
.entry(sorted_chunks)
.or_default()
.push(*identifier);
}

for (chunks, modules) in chunk_map {
if chunks.len() <= 1 {
continue;
}

// split chunks from original chunks and create new chunk
let mut new_chunk = Chunk::new(None, rspack_core::ChunkKind::Normal);
new_chunk.chunk_reason = Some("modules are shared across multiple chunks".into());
let new_chunk_ukey = new_chunk.ukey;
compilation.chunk_graph.add_chunk(new_chunk_ukey);

for chunk_ukey in &chunks {
let origin = compilation.chunk_by_ukey.expect_get_mut(chunk_ukey);
origin.split(&mut new_chunk, &mut compilation.chunk_group_by_ukey);
}

compilation.chunk_by_ukey.add(new_chunk);

for m in modules {
for chunk_ukey in &chunks {
compilation
.chunk_graph
.disconnect_chunk_and_module(chunk_ukey, m);
}

compilation
.chunk_graph
.connect_chunk_and_module(new_chunk_ukey, m);
}
}

dbg!(now.elapsed().as_millis());

Check failure on line 71 in crates/rspack_plugin_split_shared_modules/src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust check

the `dbg!` macro is intended as a debugging tool
Ok(None)
}

impl Plugin for SplitSharedModulesPlugin {
fn apply(
&self,
ctx: rspack_core::PluginContext<&mut rspack_core::ApplyContext>,
_options: &rspack_core::CompilerOptions,
) -> rspack_error::Result<()> {
ctx
.context
.compilation_hooks
.optimize_chunks
.tap(optimize_chunks::new(self));
Ok(())
}
}
14 changes: 14 additions & 0 deletions packages/rspack/src/builtin-plugin/SplitSharedModulesPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { BuiltinPluginName } from "@rspack/binding";

import { create } from "./base";

const map = {
foo: "dts"
};

export const SplitSharedModulesPlugin = create(
BuiltinPluginName.SplitSharedModulesPlugin,
() => {
return {};
}
);
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 @@ -57,6 +57,7 @@ export * from "./SideEffectsFlagPlugin";
export * from "./SizeLimitsPlugin";
export * from "./SourceMapDevToolPlugin";
export * from "./SplitChunksPlugin";
export * from "./SplitSharedModulesPlugin";
export * from "./LightningCssMiminizerRspackPlugin";
export * from "./SwcJsMinimizerPlugin";
export * from "./WarnCaseSensitiveModulesPlugin";
Expand Down
6 changes: 5 additions & 1 deletion packages/rspack/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,19 @@ export const webworker: Webworker = { WebWorkerTemplatePlugin };
import { LimitChunkCountPlugin } from "./builtin-plugin";
import { RuntimeChunkPlugin } from "./builtin-plugin";
import { SplitChunksPlugin } from "./builtin-plugin";
import { SplitSharedModulesPlugin } from "./builtin-plugin";

interface Optimize {
LimitChunkCountPlugin: typeof LimitChunkCountPlugin;
RuntimeChunkPlugin: typeof RuntimeChunkPlugin;
SplitChunksPlugin: typeof SplitChunksPlugin;
SplitSharedModulesPlugin: typeof SplitSharedModulesPlugin;
}
export const optimize: Optimize = {
LimitChunkCountPlugin,
RuntimeChunkPlugin,
SplitChunksPlugin
SplitChunksPlugin,
SplitSharedModulesPlugin
};

import { ModuleFederationPlugin } from "./container/ModuleFederationPlugin";
Expand Down
Loading