Skip to content

Commit a6f3280

Browse files
committed
feat: split chunks add deps for esm module
1 parent 06ef168 commit a6f3280

File tree

20 files changed

+347
-210
lines changed

20 files changed

+347
-210
lines changed

crates/node_binding/napi-binding.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,7 @@ export interface RawEnvironment {
21062106

21072107
export interface RawEsmLibraryPlugin {
21082108
preserveModules?: string
2109+
splitChunks?: RawSplitChunksOptions
21092110
}
21102111

21112112
export interface RawEvalDevToolModulePluginOptions {

crates/rspack_binding_api/src/raw_options/raw_builtins/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl<'a> BuiltinPlugin<'a> {
419419
plugins.push(
420420
EsmLibraryPlugin::new(
421421
options.preserve_modules.as_deref().map(Into::into),
422-
options.split_chunks.as_deref().map(Into::into),
422+
options.split_chunks.map(Into::into),
423423
)
424424
.boxed(),
425425
);

crates/rspack_binding_api/src/raw_options/raw_builtins/raw_esm_lib.rs

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,34 @@ use std::sync::Arc;
33
use napi::bindgen_prelude::Either3;
44
use rayon::iter::Either;
55
use rspack_core::CompilerId;
6+
use rspack_error::ToStringResultToRspackResultExt;
67
use rspack_napi::threadsafe_function::ThreadsafeFunction;
7-
use rspack_plugin_esm_library::{CacheGroup, EsmLibraryPlugin};
8+
use rspack_plugin_esm_library::EsmLibraryPlugin;
9+
use rspack_plugin_split_chunks::{CacheGroup, SplitChunksPlugin};
810
use rspack_regex::RspackRegex;
911

10-
use crate::module::ModuleObject;
12+
use crate::{
13+
module::ModuleObject,
14+
raw_options::{self, RawSplitChunksOptions},
15+
};
1116

1217
pub type RawNameGetter = Either<String, ThreadsafeFunction<ModuleObject, Option<String>>>;
1318

1419
pub type RawCacheGroupTest =
1520
Either3<String, RspackRegex, ThreadsafeFunction<ModuleObject, Option<bool>>>;
1621

1722
#[napi(object, object_to_js = false)]
18-
pub struct RawCacheGroup {
19-
pub name: Option<RawNameGetter>,
20-
pub test: Option<RawCacheGroupTest>,
21-
pub key: String,
22-
pub r#type: Option<String>,
23-
pub filename: Option<String>,
24-
pub priority: Option<f64>,
25-
pub min_size: Option<f64>,
26-
}
27-
28-
#[napi(object, object_to_js = false)]
29-
pub struct RawEsmLibraryPlugin {
23+
pub struct RawEsmLibraryPlugin<'a> {
3024
pub preserve_modules: Option<String>,
31-
pub split_chunks: Option<Vec<RawCacheGroup>>,
25+
pub split_chunks: Option<RawSplitChunksOptions<'a>>,
3226
}
3327

34-
impl From<RawCacheGroup> for EsmLibraryPlugin::CacheGroup {
35-
fn from(value: RawCacheGroup) -> Self {
36-
CacheGroup {
37-
name: match value.name {
38-
Some(Either::Right(name_getter)) => {
39-
Either::Right(Arc::new(async move |module, compilation| {
40-
Box::pin(async move {
41-
name_getter
42-
.call_with_sync(ModuleObject::with_ref(module, compilation.compiler_id()))
43-
.await
44-
})
45-
}))
46-
}
47-
Some(Either::Left(name)) => Either::Left(Some(name)),
48-
None => Either::Left(None),
49-
},
50-
key: value.key,
51-
test: match value.test {
52-
Some(Either3::A(string)) => {
53-
Arc::new(async move |module, _| module.identifier().contains(&string))
54-
}
55-
Some(Either3::B(regex)) => Arc::new(|module| regex.test(module.identifier().as_str())),
56-
Some(Either3::C(test_fn)) => Arc::new(async |module, compilation| {
57-
test_fn
58-
.call_with_sync(ModuleObject::with_ref(module, compilation.compiler_id()))
59-
.await
60-
.unwrap_or(false)
61-
}),
62-
None => Arc::new(|_| true),
63-
},
64-
r#type: match value.r#type {
65-
Some(raw) => crate::split_chunks::normalize_raw_cache_group_type(raw),
66-
None => raw_split_chunks::default_cache_group_type(),
67-
},
68-
filename: value.filename,
69-
priority: value.priority.unwrap_or(0.0),
70-
min_size: value
71-
.min_size
72-
.map(|s| crate::split_chunks::SplitChunkSizes {
73-
javascript: s,
74-
..Default::default()
75-
}),
76-
index: 0, // will be set later
77-
}
28+
impl<'a> From<RawSplitChunksOptions<'a>> for Vec<CacheGroup> {
29+
fn from(value: RawSplitChunksOptions<'a>) -> Self {
30+
let mut groups = rspack_plugin_split_chunks::PluginOptions::from(value).cache_groups;
31+
32+
groups.sort_by(|a, b| a.priority.total_cmp(&b.priority));
33+
34+
groups
7835
}
7936
}

crates/rspack_plugin_esm_library/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ mod render;
99
mod runtime;
1010
mod split_chunks;
1111
pub use plugin::EsmLibraryPlugin;
12-
pub use split_chunks::{CacheGroup, GetNameGetter, ModuleFilter, ModuleTypeFilter};
12+
pub use split_chunks::{GetNameGetter, ModuleFilter, ModuleTypeFilter};

crates/rspack_plugin_esm_library/src/plugin.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ use rspack_plugin_javascript::{
2323
JavascriptModulesRenderChunkContent, JsPlugin, RenderSource,
2424
dependency::ImportDependencyTemplate, parser_and_generator::JavaScriptParserAndGenerator,
2525
};
26+
use rspack_plugin_split_chunks::CacheGroup;
2627
use rspack_util::fx_hash::FxHashMap;
2728
use sugar_path::SugarPath;
2829
use tokio::sync::RwLock;
2930

3031
use crate::{
3132
chunk_link::ChunkLinkContext, dependency::dyn_import::DynamicImportDependencyTemplate,
3233
ensure_entry_exports::ensure_entry_exports, esm_lib_parser_plugin::EsmLibParserPlugin,
33-
preserve_modules::preserve_modules, runtime::RegisterModuleRuntime, split_chunks::CacheGroup,
34+
preserve_modules::preserve_modules, runtime::RegisterModuleRuntime,
3435
};
3536

3637
pub static RSPACK_ESM_RUNTIME_CHUNK: &str = "RSPACK_ESM_RUNTIME";
@@ -487,13 +488,12 @@ async fn optimize_chunks(&self, compilation: &mut Compilation) -> Result<Option<
487488
if !errors.is_empty() {
488489
compilation.extend_diagnostics(errors);
489490
}
490-
} else {
491-
if let Some(cache_groups) = &self.split_chunks {
492-
crate::split_chunks::split(cache_groups, compilation, true);
493-
}
494-
ensure_entry_exports(compilation);
491+
} else if let Some(cache_groups) = &self.split_chunks {
492+
crate::split_chunks::split(cache_groups, compilation).await?;
495493
}
496494

495+
ensure_entry_exports(compilation);
496+
497497
Ok(None)
498498
}
499499

0 commit comments

Comments
 (0)