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

fix: maxInitialRequests should exists in cache groups #7512

Merged
merged 3 commits into from
Aug 9, 2024
Merged
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
2 changes: 2 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,8 @@ export interface RawCacheGroupOptions {
maxSize?: number | RawSplitChunkSizes
maxAsyncSize?: number | RawSplitChunkSizes
maxInitialSize?: number | RawSplitChunkSizes
maxAsyncRequests?: number
maxInitialRequests?: number
name?: string | false | Function
reuseExistingChunk?: boolean
enforce?: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub struct RawSplitChunksOptions {
pub chunks: Option<Chunks>,
pub used_exports: Option<bool>,
pub automatic_name_delimiter: Option<String>,
pub max_async_requests: Option<u32>,
pub max_initial_requests: Option<u32>,
pub max_async_requests: Option<f64>,
pub max_initial_requests: Option<f64>,
pub default_size_types: Vec<String>,
pub min_chunks: Option<u32>,
pub hide_path_info: Option<bool>,
Expand Down Expand Up @@ -90,6 +90,8 @@ pub struct RawCacheGroupOptions {
pub max_size: Option<Either<f64, RawSplitChunkSizes>>,
pub max_async_size: Option<Either<f64, RawSplitChunkSizes>>,
pub max_initial_size: Option<Either<f64, RawSplitChunkSizes>>,
pub max_async_requests: Option<f64>,
pub max_initial_requests: Option<f64>,
#[napi(ts_type = "string | false | Function")]
#[derivative(Debug = "ignore")]
pub name: Option<RawChunkOptionName>,
Expand Down Expand Up @@ -214,10 +216,8 @@ impl From<RawSplitChunksOptions> for rspack_plugin_split_chunks::PluginOptions {
.unwrap_or(overall_automatic_name_delimiter.clone()),
filename: v.filename.map(Filename::from),
reuse_existing_chunk: v.reuse_existing_chunk.unwrap_or(false),
// TODO(hyf0): the non-enforced default value should be 30
// I would set align default value with Webpack when the options is exposed to users
max_async_requests: u32::MAX,
max_initial_requests: u32::MAX,
max_async_requests: v.max_async_requests.unwrap_or(f64::INFINITY),
max_initial_requests: v.max_initial_requests.unwrap_or(f64::INFINITY),
max_async_size,
max_initial_size,
r#type,
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_split_chunks/src/options/cache_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub struct CacheGroup {
/// number of referenced chunks
pub min_chunks: u32,
pub id_hint: String,
pub max_initial_requests: u32,
pub max_async_requests: u32,
pub max_initial_requests: f64, // f64 for compat js Infinity
pub max_async_requests: f64, // f64 for compat js Infinity
pub max_async_size: SplitChunkSizes,
pub max_initial_size: SplitChunkSizes,
pub filename: Option<Filename>,
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_split_chunks/src/plugin/max_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl SplitChunksPlugin {
let allowed_max_request = if chunk.is_only_initial(chunk_group_db) {
cache_group.max_initial_requests
} else if chunk.can_be_initial(chunk_group_db) {
u32::max(
f64::max(
cache_group.max_initial_requests,
cache_group.max_async_requests,
)
Expand Down Expand Up @@ -54,7 +54,7 @@ impl SplitChunksPlugin {
.map(|requests| requests as u32)
.unwrap_or_default();

if actually_requests >= allowed_max_request {
if actually_requests as f64 >= allowed_max_request {
Some(chunk.ukey)
} else {
None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
it("should not split chunk when maxInitialRequests set to only 1", function () {
const a = require("./node_modules/a");
expect(a).toBe("a");
const b = require("./node_modules/b");
expect(b).toBe("b");
const c = require("./node_modules/c");
expect(c).toBe("c");
const d = require("./node_modules/d");
expect(d).toBe("d");

const fs = require('fs')
const files = fs.readdirSync(__dirname)
expect(files.filter(file => file.endsWith('.js')).length).toBe(1)
});

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

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
optimization: {
chunkIds: 'named',
splitChunks: {
maxInitialRequests: 10,
cacheGroups: {
vendors: {
chunks: 'all',
test: /node_modules/,
minSize: 0,
filename: 'split-[name].js',

// should override the splitChunks.maxInitialRequests
maxInitialRequests: 1,
}
}
}
}
}
108 changes: 78 additions & 30 deletions packages/rspack/etc/api.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,8 @@ const sharedOptimizationSplitChunksCacheGroup = {
maxSize: optimizationSplitChunksSizes.optional(),
maxAsyncSize: optimizationSplitChunksSizes.optional(),
maxInitialSize: optimizationSplitChunksSizes.optional(),
maxAsyncRequests: z.number().optional(),
maxInitialRequests: z.number().optional(),
automaticNameDelimiter: z.string().optional()
};
const optimizationSplitChunksCacheGroup = z.strictObject({
Expand Down Expand Up @@ -1272,8 +1274,6 @@ const optimizationSplitChunksOptions = z.strictObject({
cacheGroups: z
.record(z.literal(false).or(optimizationSplitChunksCacheGroup))
.optional(),
maxAsyncRequests: z.number().optional(),
maxInitialRequests: z.number().optional(),
fallbackCacheGroup: z
.strictObject({
chunks: optimizationSplitChunksChunks.optional(),
Expand Down
Loading