Skip to content

Commit

Permalink
fix: maxInitialRequests should exists in cache groups
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Aug 9, 2024
1 parent 6f19caa commit 2285168
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 64 deletions.
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.

8 changes: 2 additions & 6 deletions packages/rspack/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,12 +950,8 @@ const applyOptimizationDefaults = (
F(splitChunks, "minSize", () => (production ? 20000 : 10000));
// F(splitChunks, "minRemainingSize", () => (development ? 0 : undefined));
// F(splitChunks, "enforceSizeThreshold", () => (production ? 50000 : 30000));
F(splitChunks, "maxAsyncRequests", () =>
production ? 30 : Number.POSITIVE_INFINITY
);
F(splitChunks, "maxInitialRequests", () =>
production ? 30 : Number.POSITIVE_INFINITY
);
F(splitChunks, "maxAsyncRequests", () => (production ? 30 : Infinity));
F(splitChunks, "maxInitialRequests", () => (production ? 30 : Infinity));
D(splitChunks, "automaticNameDelimiter", "-");
const { cacheGroups } = splitChunks;
if (cacheGroups) {
Expand Down
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
13 changes: 0 additions & 13 deletions tests/webpack-test/__snapshots__/ConfigTestCases.basictest.js.snap

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,11 @@ runtime modules 2.59 KiB 3 modules
./a.js 18 bytes [built] [code generated]
Rspack x.x.x compiled successfully in X.23
asset b-runtime~main-eb1e55e9714495b68bff.js 4.39 KiB [emitted] [immutable] (name: runtime~main)
asset b-runtime~main-2a432c6b45b448fcde80.js 4.39 KiB [emitted] [immutable] (name: runtime~main)
asset b-all-b_js-468c0f551eefc9f96f0b.js 461 bytes [emitted] [immutable] (id hint: all)
asset b-main-72ac2a7c5326395bcc89.js 420 bytes [emitted] [immutable] (name: main)
asset b-main-236b715ba81f9430f0c7.js 420 bytes [emitted] [immutable] (name: main)
asset b-vendors-node_modules_vendor_js-97d43f84c65cc0e25938.js 173 bytes [emitted] [immutable] (id hint: vendors)
Entrypoint main 5.42 KiB = b-runtime~main-eb1e55e9714495b68bff.js 4.39 KiB b-vendors-node_modules_vendor_js-97d43f84c65cc0e25938.js 173 bytes b-all-b_js-468c0f551eefc9f96f0b.js 461 bytes b-main-72ac2a7c5326395bcc89.js 420 bytes
Entrypoint main 5.42 KiB = b-runtime~main-2a432c6b45b448fcde80.js 4.39 KiB b-vendors-node_modules_vendor_js-97d43f84c65cc0e25938.js 173 bytes b-all-b_js-468c0f551eefc9f96f0b.js 461 bytes b-main-236b715ba81f9430f0c7.js 420 bytes
runtime modules 3.17 KiB 5 modules
cacheable modules 40 bytes
./b.js 17 bytes [built] [code generated]
Expand Down

0 comments on commit 2285168

Please sign in to comment.