The Compiler is a core object in Rspack. A Compiler instance is created whenever you call Rspack's JavaScript API or CLI.
It provides methods like run and watch to start builds, while exposing various Compiler hooks that allow Rspack plugins to intervene at different stages of the build process.
Start a compilation, and calls back when the compilation is completed or aborted due to an error.
If you need to call the run method of the same compiler object multiple times, please note the following:
compiler.close() in the callback function of compiler.run and wait for it to finish. Only then can you proceed with the next compiler.run call. Running multiple compilation processes simultaneously can lead to unexpected results in the output files.modifiedFiles and removedFiles parameters. When caching is enabled and you're using a custom watcher to watch file changes, you need to pass these values to Rspack via the options parameter.Watching files and directories, start a compilation process after they change, and calls back every time the compilation is completed or aborted due to an error.
This API only supports one compilation at a time. Please call compiler.close in the compiler.watch callback and wait for it to finish before executing compiler.watch again. Concurrent compilations will damage the output files.
The Watching object provides the following methods:
watch:
(files: string[], dirs: string[], missing: string[]): voidinvalidate:
(callback: () => void): voidsuspend:
(): voidresume:
(): voidclose:
(callback: () => void): voidClose the current compiler, and handle low-priority tasks such as caching during this period.
Create a logger object that is not associated with any compilation, which is used to print global logs.
Create a cache object to share data in the build process.
Stop the read loop of the input file system, which internally contains a timer and may cause the process to still not be able to exit after calling compiler.close.
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.
Running the child compiler, which will doing a complete compiling and generate the assets.
Whether this compiler is a child compiler.
See compiler hooks for more details.
typeof rspackGet the exports of @rspack/core to obtain the associated internal objects. This is especially useful when you cannot directly reference @rspack/core or there are multiple Rspack instances.
A common example is accessing the sources object in a Rspack plugin:
typeof rspackEquivalent to compiler.rspack, this property is used for compatibility with webpack plugins.
If the Rspack plugin you are developing needs to be webpack compatible, you can use this property instead of compiler.rspack.
stringGet the name:
name.createChildCompiler.Current project root directory:
new Compiler, it is the value passed in.rspack({}), it is context configuration.CompilerGet the root of the child compiler tree.
RspackOptionsNormalizedGet the full options used by this compiler.
booleanWhether started through compiler.watch.
WatchingGet the watching object, see watch method for more details.
booleanWhether the compilation is currently being executed.
InputFileSystemGet the proxy object used for reading from the file system, which has optimizations such as caching inside to reduce duplicate reading of the same file.
OutputFileSystemGet the proxy object used for writing to the file system, fs by default.
WatchFileSystemGet the proxy object used for watching files or directories changes, which provides a watch method to start watching, and passes in the changed and removed items in the callback.
The MultiCompiler module allows Rspack to run multiple configurations in separate compilers. If the options parameter in the Rspack's JavaScript API is an array of options, Rspack applies separate compilers and calls the callback after all compilers have been executed.
It can also be created through new MultiCompiler:
MultiCompiler also provides some methods and attributes of the Compiler.
Specify the dependency relationship between the compilers, using compiler.name as the identifier, to ensure the execution order of the compilers.
Check whether the dependency relationship between the compilers is legal. If there is a cycle or a missing dependency, it will trigger the callback.
Execute the run method of each compiler according to the dependency relationship to start the compilation process.
Execute the watch method of each compiler according to the dependency relationship to start watching, and start a compilation process after the file changes.
Execute the close method of each compiler to close them, and handle low-priority tasks such as caching during this period.
Execute the purgeInputFileSystem of each compiler to stop the read loop of the file system
Create a logger object that is not associated with any compilation, which is used to print global logs.
Same with
compilers[0].getInfrastructureLogger()
Compiler[]Get all included compilers.
RspackOptionsNormalized[]Get all the full options used by the compilers.
InputFileSystemSet the proxy object used for reading from the file system for each compiler.
OutputFileSystemSet the proxy object used for writing from the file system for each compiler.
WatchFileSystemSet the proxy object used for watching files or directories changes for each compiler.
booleanWhether the compilation is currently being executed.