Filename placeholders
Rspack's filename-related options support placeholders. This lets you generate output filenames dynamically based on the current build context. It helps distinguish different assets and improves caching efficiency.
Different options support different types of placeholders. This page lists all placeholder types available in Rspack, and which filename options use which placeholder type.
Example
For example, the [contenthash] placeholder generates a hash based on the file content. If the content does not change, the filename remains stable. This is ideal for long-term caching.
Here is an example showing how [name] and [contenthash] can be used in output.filename:
This outputs:
Filename options
The following table summarizes common filename options and the placeholder types they support:
Placeholder types
Compilation placeholders
Compilation placeholders expose information about the entire build process.
Chunk placeholders
Chunk placeholders expose information about a specific chunk.
Module placeholders
Module placeholders expose information about a specific module.
File placeholders
File placeholders expose information derived from the file path.
Relationships:
[file]equals[path][base][base]equals[name][ext]
A complete path can be built using any of these forms:
[path][name][ext][query][fragment][path][base][query][fragment][file][query][fragment]
URL placeholders
URL placeholders expose information about the current request URL.
Hash Length
Hash placeholders ([hash], [contenthash], [chunkhash]) support length modifiers using [hash:n], where the default length is 16.
Example truncating [contenthash] to 8 characters:
You can also specify a digest (only base64 is supported) and length:
Alternatively, you can configure a global default using output.hashDigestLength.
During local development, avoid using hash-based filenames.
Entry chunks and chunks produced by optimization.splitChunks are loaded via <script> tags in HTML. If filenames contain hashes, the HTML file cannot update dynamically, which breaks HMR.
Escaping Placeholders
If you want to preserve the literal placeholder text instead of having Rspack interpolate it, escape the brackets with backslashes.
For example, to include [name] in the filename, rather than interpolating it as the chunk name, you can escape it with backslashes:
Using functions
Some options allow specifying a function instead of a string.
For example, output.filename option accepts a function, which will be called during compilation and receives a PathData object containing contextual information.
The function is invoked during the build process and receives an object that contains the current context data. You can use this information to construct any string you need, and the returned string will still go through the same placeholder substitution rules.

