web-infra-dev / rspack

The fast Rust-based web bundler with webpack-compatible API 🦀️
https://rspack.dev
MIT License
10.13k stars 575 forks source link

[Bug]: `chunks` Regex produces different results when used in a function #8529

Open MindFreeze opened 20 hours ago

MindFreeze commented 20 hours ago

System Info

System: OS: macOS 15.1.1 CPU: (12) arm64 Apple M3 Pro Memory: 10.48 GB / 36.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 22.9.0 - ~/.nvm/versions/node/v22.9.0/bin/node Yarn: 4.5.1 - ~/.nvm/versions/node/v22.9.0/bin/yarn npm: 10.8.3 - ~/.nvm/versions/node/v22.9.0/bin/npm Browsers: Chrome: 131.0.6778.86 Safari: 18.1.1 npmPackages: @rspack/cli: 1.1.3 => 1.1.3 @rspack/core: 1.1.3 => 1.1.3

Details

According to the docs a RegExp passed as chunks is a short for (chunk) => regex.test(chunk.name) but you get a different between a regex and a function with the same regex.

After some debugging I found that I have chunks with name: undefined. This passes the regex check in JS but apparently not in Rust. If I change the chunks function to (chunk) => typeof chunk.name === "string" && regex.test(chunk.name) the result is aligned with the regex variant.

Reproduce link

No response

Reproduce Steps

  1. Set any RegExp for splitChunks.chunks, build and check the generated assets
  2. Set splitChunks.chunks to the same RegExp but in a function like (chunk) => regex.test(chunk.name), build and see different assets