Skip to content

Commit 7b22e3a

Browse files
JSerFengCopilot
andcommitted
Update crates/rspack_plugin_esm_library/src/split_chunks.rs
Co-authored-by: Copilot <[email protected]>
1 parent e9bd3df commit 7b22e3a

File tree

8 files changed

+45
-28
lines changed

8 files changed

+45
-28
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@ use std::sync::Arc;
22

33
use napi::bindgen_prelude::Either3;
44
use rayon::iter::Either;
5-
use rspack_core::CompilerId;
6-
use rspack_error::ToStringResultToRspackResultExt;
75
use rspack_napi::threadsafe_function::ThreadsafeFunction;
86
use rspack_plugin_esm_library::EsmLibraryPlugin;
9-
use rspack_plugin_split_chunks::{CacheGroup, SplitChunksPlugin};
7+
use rspack_plugin_split_chunks::CacheGroup;
108
use rspack_regex::RspackRegex;
119

1210
use crate::{
1311
module::ModuleObject,
1412
raw_options::{self, RawSplitChunksOptions},
1513
};
1614

17-
pub type RawNameGetter = Either<String, ThreadsafeFunction<ModuleObject, Option<String>>>;
18-
19-
pub type RawCacheGroupTest =
20-
Either3<String, RspackRegex, ThreadsafeFunction<ModuleObject, Option<bool>>>;
21-
2215
#[napi(object, object_to_js = false)]
2316
pub struct RawEsmLibraryPlugin<'a> {
2417
pub preserve_modules: Option<String>,

crates/rspack_plugin_esm_library/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ rspack_util = { workspace = true }
2626

2727
async-trait = { workspace = true }
2828
atomic_refcell = { workspace = true }
29-
derive_more = { workspace = true }
3029
futures = { workspace = true }
3130
rayon = { workspace = true }
3231
regex = { workspace = true }

crates/rspack_plugin_esm_library/src/split_chunks.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ pub(crate) async fn split(groups: &[CacheGroup], compilation: &mut Compilation)
266266
for m in match_group.modules.clone() {
267267
// merge dependencies
268268

269-
let mut stack = get_module_deps(m, &module_graph);
269+
let mut stack = get_module_deps(m, module_graph);
270270
while let Some(m) = stack.pop() {
271271
// if module is already in any group, skip
272272
if modules_in_group.contains(&m) {
@@ -278,7 +278,7 @@ pub(crate) async fn split(groups: &[CacheGroup], compilation: &mut Compilation)
278278
continue;
279279
}
280280

281-
stack.extend(get_module_deps(m, &module_graph));
281+
stack.extend(get_module_deps(m, module_graph));
282282
}
283283
}
284284
});
@@ -384,8 +384,6 @@ pub(crate) async fn split(groups: &[CacheGroup], compilation: &mut Compilation)
384384
unreachable!()
385385
};
386386

387-
// chunk.set_filename_template();
388-
389387
orig_chunk.split(chunk, &mut compilation.chunk_group_by_ukey);
390388
}
391389

crates/rspack_plugin_split_chunks/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub fn get_module_sizes<T: ParallelIterator<Item = ModuleIdentifier>>(
203203
.module_by_identifier(&module)
204204
.expect("should have module");
205205
let sizes = module
206-
.source_types(&module_graph)
206+
.source_types(module_graph)
207207
.iter()
208208
.map(|ty| (*ty, module.size(Some(ty), Some(compilation))))
209209
.collect::<FxHashMap<_, _>>();

crates/rspack_plugin_split_chunks/src/plugin/max_size.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ impl SplitChunksPlugin {
598598
compilation_ref,
599599
&compilation
600600
.chunk_graph
601-
.get_chunk_modules(chunk, &module_graph),
601+
.get_chunk_modules(chunk, module_graph),
602602
allow_max_size,
603603
min_size,
604604
automatic_name_delimiter,

packages/rspack/etc/core.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,7 @@ class EsmLibraryPlugin {
23032303
// (undocumented)
23042304
options?: {
23052305
preserveModules?: string;
2306+
splitChunks?: OptimizationSplitChunksOptions;
23062307
};
23072308
// (undocumented)
23082309
static PLUGIN_NAME: string;

packages/rspack/src/builtin-plugin/EsmLibraryPlugin.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import { BuiltinPluginName } from '@rspack/binding';
22
import rspack from '..';
33
import type { Compiler } from '../Compiler';
4-
import type { OptimizationSplitChunksOptions, RspackOptionsNormalized } from '../config';
4+
import type {
5+
OptimizationSplitChunksOptions,
6+
RspackOptionsNormalized,
7+
} from '../config';
58
import type { Logger } from '../logging/Logger';
69
import { RemoveDuplicateModulesPlugin } from './RemoveDuplicateModulesPlugin';
7-
import { toRawSplitChunksOptions } from "./SplitChunksPlugin";
10+
import { toRawSplitChunksOptions } from './SplitChunksPlugin';
811

9-
const NOT_SUPPORTED_CONFIG = ['maxSize', 'minSizeReduction', 'maxAsyncSize', 'maxInitialSize', 'maxAsyncRequests', 'maxInitialRequests'] as const;
12+
const NOT_SUPPORTED_CONFIG = [
13+
'maxSize',
14+
'minSizeReduction',
15+
'maxAsyncSize',
16+
'maxInitialSize',
17+
'maxAsyncRequests',
18+
'maxInitialRequests',
19+
] as const;
1020

1121
function applyLimits(options: RspackOptionsNormalized, logger: Logger) {
1222
// concatenateModules is not supported in ESM library mode, it has its own scope hoist algorithm
@@ -38,11 +48,18 @@ function applyLimits(options: RspackOptionsNormalized, logger: Logger) {
3848

3949
let { splitChunks } = options.optimization;
4050
if (splitChunks === undefined) {
41-
splitChunks = false;
51+
splitChunks = options.optimization.splitChunks = false;
4252
}
4353

4454
if (splitChunks !== false) {
45-
const notSupportedConfig = ['maxSize', 'minSizeReduction', 'maxAsyncSize', 'maxInitialSize', 'maxAsyncRequests', 'maxInitialRequests'] as const;
55+
const notSupportedConfig = [
56+
'maxSize',
57+
'minSizeReduction',
58+
'maxAsyncSize',
59+
'maxInitialSize',
60+
'maxAsyncRequests',
61+
'maxInitialRequests',
62+
] as const;
4663
const invalidConfig = [];
4764

4865
for (const field of notSupportedConfig) {
@@ -52,7 +69,9 @@ function applyLimits(options: RspackOptionsNormalized, logger: Logger) {
5269
}
5370

5471
if (invalidConfig.length > 0) {
55-
logger.warn(`Currently \`${invalidConfig.join(', ')}\` are not supported in esm library mode`);
72+
logger.warn(
73+
`Currently \`${invalidConfig.join(', ')}\` are not supported in esm library mode`,
74+
);
5675
}
5776

5877
splitChunks.cacheGroups ??= {};
@@ -61,10 +80,13 @@ function applyLimits(options: RspackOptionsNormalized, logger: Logger) {
6180
}
6281
}
6382

64-
function fieldUsed(field: (typeof NOT_SUPPORTED_CONFIG)[number], splitChunks: OptimizationSplitChunksOptions): boolean {
83+
function fieldUsed(
84+
field: (typeof NOT_SUPPORTED_CONFIG)[number],
85+
splitChunks: OptimizationSplitChunksOptions,
86+
): boolean {
6587
if (splitChunks[field] !== undefined) {
6688
return true;
67-
};
89+
}
6890

6991
const cacheGroups = splitChunks.cacheGroups;
7092
if (cacheGroups === undefined) {
@@ -87,9 +109,12 @@ function fieldUsed(field: (typeof NOT_SUPPORTED_CONFIG)[number], splitChunks: Op
87109

88110
export class EsmLibraryPlugin {
89111
static PLUGIN_NAME = 'EsmLibraryPlugin';
90-
options?: { preserveModules?: string, splitChunks?: OptimizationSplitChunksOptions };
112+
options?: {
113+
preserveModules?: string;
114+
splitChunks?: OptimizationSplitChunksOptions;
115+
};
91116

92-
constructor(options?: { preserveModules?: string }) {
117+
constructor(options: { preserveModules?: string } = {}) {
93118
this.options = options;
94119
}
95120

@@ -118,8 +143,10 @@ export class EsmLibraryPlugin {
118143
name: BuiltinPluginName.EsmLibraryPlugin,
119144
options: {
120145
preserveModules: this.options?.preserveModules,
121-
splitChunks: this.options?.splitChunks && toRawSplitChunksOptions(this.options?.splitChunks, compiler)
122-
}
146+
splitChunks:
147+
this.options?.splitChunks &&
148+
toRawSplitChunksOptions(this.options?.splitChunks, compiler),
149+
},
123150
});
124151
}
125152
}

0 commit comments

Comments
 (0)