-
-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
size
option for optimization.chunkIds config (#8642)
- Loading branch information
Showing
16 changed files
with
174 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
crates/rspack_binding_options/src/options/raw_builtins/raw_ids.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use napi_derive::napi; | ||
use rspack_ids::OccurrenceChunkIdsPluginOptions; | ||
|
||
#[derive(Debug)] | ||
#[napi(object, object_to_js = false)] | ||
pub struct RawOccurrenceChunkIdsPluginOptions { | ||
pub prioritise_initial: Option<bool>, | ||
} | ||
|
||
impl From<RawOccurrenceChunkIdsPluginOptions> for OccurrenceChunkIdsPluginOptions { | ||
fn from(value: RawOccurrenceChunkIdsPluginOptions) -> Self { | ||
Self { | ||
prioritise_initial: value.prioritise_initial.unwrap_or_default(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::collections::HashMap; | ||
|
||
use itertools::Itertools; | ||
use rspack_collections::DatabaseItem; | ||
use rspack_core::{ | ||
ApplyContext, Chunk, CompilationChunkIds, CompilerOptions, Plugin, PluginContext, | ||
}; | ||
use rspack_error::Result; | ||
use rspack_hook::{plugin, plugin_hook}; | ||
|
||
use crate::id_helpers::{assign_ascending_chunk_ids, compare_chunks_natural}; | ||
|
||
pub struct OccurrenceChunkIdsPluginOptions { | ||
pub prioritise_initial: bool, | ||
} | ||
|
||
#[plugin] | ||
#[derive(Debug, Default)] | ||
pub struct OccurrenceChunkIdsPlugin { | ||
prioritise_initial: bool, | ||
} | ||
|
||
impl OccurrenceChunkIdsPlugin { | ||
pub fn new(option: OccurrenceChunkIdsPluginOptions) -> Self { | ||
Self::new_inner(option.prioritise_initial) | ||
} | ||
} | ||
|
||
#[plugin_hook(CompilationChunkIds for OccurrenceChunkIdsPlugin)] | ||
fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<()> { | ||
let chunk_graph = &compilation.chunk_graph; | ||
let module_graph = &compilation.get_module_graph(); | ||
let chunk_group_by_ukey = &compilation.chunk_group_by_ukey; | ||
let mut occurs_in_initial_chunks_map = HashMap::new(); | ||
|
||
for chunk in compilation.chunk_by_ukey.values() { | ||
let mut occurs = 0; | ||
for chunk_group_ukey in chunk.groups() { | ||
if let Some(chunk_group) = chunk_group_by_ukey.get(chunk_group_ukey) { | ||
for parent_ukey in &chunk_group.parents { | ||
if let Some(parent) = chunk_group_by_ukey.get(parent_ukey) { | ||
if parent.is_initial() { | ||
occurs += 1; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
occurs_in_initial_chunks_map.insert(chunk.ukey(), occurs); | ||
} | ||
|
||
let chunks = compilation | ||
.chunk_by_ukey | ||
.values() | ||
.map(|chunk| chunk as &Chunk) | ||
.sorted_unstable_by(|a, b| { | ||
if self.prioritise_initial { | ||
let a_entry_occurs = occurs_in_initial_chunks_map.get(&a.ukey()).unwrap_or(&0); | ||
let b_entry_occurs = occurs_in_initial_chunks_map.get(&b.ukey()).unwrap_or(&0); | ||
if a_entry_occurs != b_entry_occurs { | ||
return b_entry_occurs.cmp(a_entry_occurs); | ||
} | ||
} | ||
|
||
let a_occurs = a.get_number_of_groups(); | ||
let b_occurs = b.get_number_of_groups(); | ||
if a_occurs != b_occurs { | ||
return b_occurs.cmp(&a_occurs); | ||
} | ||
|
||
compare_chunks_natural(chunk_graph, module_graph, &compilation.module_ids, a, b) | ||
}) | ||
.map(|chunk| chunk.ukey()) | ||
.collect::<Vec<_>>(); | ||
|
||
assign_ascending_chunk_ids(&chunks, compilation); | ||
|
||
Ok(()) | ||
} | ||
|
||
impl Plugin for OccurrenceChunkIdsPlugin { | ||
fn name(&self) -> &'static str { | ||
"rspack.OccurrenceChunkIdsPlugin" | ||
} | ||
|
||
fn apply(&self, ctx: PluginContext<&mut ApplyContext>, _options: &CompilerOptions) -> Result<()> { | ||
ctx | ||
.context | ||
.compilation_hooks | ||
.chunk_ids | ||
.tap(chunk_ids::new(self)); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/rspack/src/builtin-plugin/OccurrenceChunkIdsPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { | ||
BuiltinPluginName, | ||
type RawOccurrenceChunkIdsPluginOptions | ||
} from "@rspack/binding"; | ||
|
||
import { create } from "./base"; | ||
|
||
export const OccurrenceChunkIdsPlugin = create( | ||
BuiltinPluginName.OccurrenceChunkIdsPlugin, | ||
(options?: RawOccurrenceChunkIdsPluginOptions) => ({ ...options }), | ||
"compilation" | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
tests/webpack-test/configCases/contenthash/include-chunk-id/test.filter.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52a3e42
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open
52a3e42
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open