CC 4.0 License
The content of this section is derived from the content of the following links and is subject to the CC BY 4.0 license.
The following contents can be assumed to be the result of modifications and deletions based on the original contents if not specifically stated.
Compilation
The Compilation object is one of the core objects used in the Rspack build process. Whenever Rspack performs a build (including rebuilds in watch mode), a new Compilation instance is created, which contains all the information about the current build.
This page lists the available methods and properties of the Compilation object. You can also refer to Compilation Hooks to learn about the hooks provided by the Compilation object.
In Rspack, the real compilation object runs on the Rust side, and the JavaScript compilation object is only a proxy object used to communicate with the Rust compilation object.
Therefore, some complex data structures and methods will not be supported on the JavaScript compilation object, the data is read-only and the structure may differ from webpack.
Get compilation object
You can get the current compilation object via compiler.hooks.thisCompilation or compiler.hooks.compilation hooks.
Compilation methods
emitAsset
Emit a new asset, throw an error if the asset already exists.
The following code will add a new asset named asset-name.js and will not be compressed:
updateAsset
Update an existing asset, throw an error if the asset does not exist.
The following code replaces the content of main.js and not to minimize it:
renameAsset
Rename an existing asset.
The following code renames the asset named main.js to my-asset-name.js:
deleteAsset
Delete an existing asset.
The following code will delete the asset named no-need.js:
getAssets
Get the list of asset objects.
The following code will print the total size of all assets:
getAsset
Get the asset object with the specified name.
The following code will print the size of the asset named main.js:
getPath
Generate path string based on the Filename template, see Filename placeholders for the template rules.
The following code will replace the placeholder in the template to generate the path:
getPathWithInfo
Generate path string and asset info based on the Filename template, see Filename placeholders.
The following code will replace the placeholder in the template to generate the path and asset info:
getStats
Get the stats object of current compilation:
The following code prints the total number of modules:
createChildCompiler
Allows running another instance of Rspack inside of Rspack. However, as a child with different settings and configurations applied. It copies all hooks and plugins from the parent (or top-level compiler) and creates a child Compiler instance. Returns the created Compiler.
The following code will start a child compiler with child-entry.js as the entry, and output to dist/child:
addRuntimeModule
Add a custom runtime module to the compilation.
The following code will add a runtime module which define __webpack_require__.mock to the "main" chunk:
When implementing a custom runtime module class, the following methods/properties can be overridden to control the behavior of the runtime module:
- Pass the
nameandstageparameters in the constructor to specify the module name and the insertion stage. - Override the
generate()method to control the generated code of the module. - Override the
shouldIsolate()method to control whether the module is wrapped in an IIFE. - Override the
attach()method to modify the behavior when the module is added. - Modify its
fullHashordependentHashproperties to control whether the module can be cached.
rebuildModule
Triggers a re-build of the module.
The following code will rebuild the module ending with a.js:
getLogger
Get a log output utility object with the specified name, which can be used to print logs with unified format in the plugin.
The following code prints the asset to the debug log in processAssets:
getCache
Get a cache object with the specified name, which can be used for the plugin to share data during multiple compilations.
Compilation properties
options
Type: RspackOptionsNormalized
The normalized options used by this Compilation, see Configure Rspack for details.
compiler
Type: Compiler
Current compiler object.
hooks
The Compilation hooks.
hash/fullhash
Type: Readonly<string | null>
The hash of this compilation.
assets
Type: Record<string, Source>
The mapping from asset filenames and content sources.
chunkGroups
Type: ReadonlyArray<ChunkGroup>
The list of chunk group objects, with the structure as follows:
entrypoints
Type: ReadonlyMap<string, Entrypoint>
The mapping from name to entrypoint, which is a special chunk group and include a runtime chunk.
namedChunkGroups
Type: ReadonlyMap<string, Readonly<ChunkGroup>>
The mapping from names to chunk groups.
modules
Type: ReadonlySet<Module>
List of all modules, with the structure as follows:
builtModules
Type: ReadonlySet<Module>
List of built modules that were not be cached, with the structure as follows:
chunks
Type: ReadonlySet<Chunk>
List of all chunks, with the structure as follows:
namedChunks
Type: ReadonlyMap<string, Readonly<Chunk>>
The mapping from names to chunks.
fileDependencies
Type: CompilationDependencies
List of files this compilation depends on.
contextDependencies
Type: CompilationDependencies
List of directories this compilation depends on.
missingDependencies
Type: CompilationDependencies
List of not existing files this compilation depends on.
buildDependencies
Type: CompilationDependencies
List of build dependencies this compilation depends on.
errors
Type: RspackError[]
List of emitted errors during this compilation, with the structure as follows:
warnings
Type: RspackError[]
List of emitted warnings during this compilation.

