rspack icon indicating copy to clipboard operation
rspack copied to clipboard

[Feature]: Support custom module identifiers (ids)

Open dinofx opened this issue 8 months ago • 3 comments

What problem does this feature solve?

In Webpack we were able to hook the compilation's beforeModuleIds hook and use our own method for generating the module ids and assigning them to each module. We do this because an import to a module like "foo/bar" resolves (using our own custom resolution strategy) to a path that includes the exact version of the package "foo". So the path to a module can change between builds, even if its source hasn't.

The default/current behavior for generating module ids in Rspack includes these unstable paths. Since the module identifier changes between builds, several other things seem affected. "deterministic" chunk ids are changing, even when the entire source content of the chunk is unchanged (other than the string containing its id at the beginning, and source map URL at the end).

As a result, all of our assets are cache-busted between builds. Our apps have minimal CSS that rarely changes, and code splits for packages like React. With webpack these assets are often cacheable from previous builds of an app.

What does the proposed API of configuration look like?

One option is to support the beforeModuleIds AND allow the relevant Module properties that are updated by the hook to be copied back to the Rust data structures.

But, it might make more sense to use an explicit output setting. Maybe a function similar to devtoolModuleFilenameTemplate and devtoolFallbackModuleFilenameTemplate, but for module ids.

dinofx avatar Jun 23 '25 17:06 dinofx

same question

zhangmingyuan666 avatar Jul 08 '25 07:07 zhangmingyuan666

Even we can support beforeModuleIds hook, it will cause huge Rust | JS communication which may slow the build a lot

The default/current behavior for generating module ids in Rspack includes these unstable paths. Since the module identifier changes between builds, several other things seem affected. "deterministic" chunk ids are changing, even when the entire source content of the chunk is unchanged (other than the string containing its id at the beginning, and source map URL at the end).

@dinofx if you seem different behavior with webpack, it maybe Rspack's bug and we can fix it if you can provide minimal repro

hardfist avatar Jul 15 '25 02:07 hardfist

@hardfist here's an example repo: https://github.com/dinofx/rspack-unstable-identifiers/tree/main

Just run pnpm i and pnpm run build.

build-1 and build-2 are identical copies of the same package, with two copies of the same "dependency" located at build-1/packages/my-lib-v1 and build-2/packages/my-lib-v2.

Since the package my-lib is aliased to ./packages/my-lib-v1 in one package, but -v2 in the other, the module in that library has a different relative path, resulting in a different module identifier, resulting in a different chunk name:

Image

dinofx avatar Jul 22 '25 20:07 dinofx