Customize the build process.
Array<RspackPluginInstance | RspackPluginFunction | RspackBuiltinPlugin>
[]
Written in JavaScript and compatible with some of the plugins in the webpack community.
Among them, the interface provided on Compiler can refer to Plugin API.
usage:
usage:
Integrated within Rspack.
Configuration can be written to be used via import
/require
:
Plugins can be written to be used via compiler.webpack.XxxPlugin
:
In webpack, these plugins are also exported as internal plugins
Add an entry chunk at compile time.
string
string
This plugin will replace variables in the code with other values or expressions at compile time.
Record<string, string | boolean | undefined>
When using the following configuration:
Input:
Output:
This plugin will automatically load modules instead of having to import or require them everywhere.
Record<string, string | string[]>
When using the following configuration:
Which will convert the following code:
To:
Adds a banner to the top or bottom of each generated chunk.
options
undefined
Name | Type | Default | Description |
---|---|---|---|
banner | string | undefined | Specifies the banner, it will be wrapped in a comment. |
entryOnly | boolean|undefined | undefined | If true, the banner will only be added to the entry chunks. |
footer | boolean|undefined | undefined | If true, banner will be placed at the end of the output. |
raw | boolean|undefined | undefined | If true, banner will not be wrapped in a comment. |
test | BannerRules|undefined | undefined | Include all modules that pass test assertion. |
include | BannerRules|undefined | undefined | Include all modules matching any of these conditions. |
exclude | BannerRules|undefined | undefined | Exclude all modules matching any of these conditions. |
Add a banner to the top or bottom of each chunk file.
This plugin is used to add the Hot Module Replacement feature. When added, it will add the runtime modules required for the hot change to the runtime chunk.
This plugin can be used to configure the progress bar.
{ prefix?: string }
{}
Name | Type | Default | Description |
---|---|---|---|
prefix | string | 'Rspack' | The text will be displayed before the progress bar. |
This plugin can be used to add external dependencies.
This plugin enables more fine grained control of source map generation. It is also enabled automatically by certain settings of the devtool
configuration option.
test
(string
RegExp
[string, RegExp]
): Include source maps for modules based on their extension (defaults to .js, .mjs, and .css).include
(string
RegExp
[string, RegExp]
): Include source maps for module paths that match the given value.exclude
(string
RegExp
[string, RegExp]
): Exclude modules that match the given value from source map generation.filename
(string
): Defines the output filename of the SourceMap (will be inlined if no value is provided).append
(string
function
): Appends the given value to the original asset. Usually the #sourceMappingURL comment. [url] is replaced with a URL to the source map file. Path parameters are supported: [chunk], [filename] and [contenthash]. Setting append to false disables the appending.moduleFilenameTemplate
(string
): See output.devtoolModuleFilenameTemplate
.fallbackModuleFilenameTemplate
(string
): See link above.namespace
(string
): See output.devtoolNamespace
.module = true
(boolean
): Indicates whether loaders should generate source maps.columns = true
(boolean
): Indicates whether column mappings should be used.noSources = false
(boolean
): Prevents the source file content from being included in the source map.publicPath
(string
): Emits absolute URLs with public path prefix, e.g. https://example.com/project/.fileContext
(string
): Makes the [file] argument relative to this directory.sourceRoot
(string
): Provide a custom value for the sourceRoot property in the SourceMap.The following examples demonstrate some common use cases for this plugin.
You can use the following code to replace the configuration option devtool: inline-source-map with an equivalent custom plugin configuration:
The following code would exclude source maps for any modules in the vendor.js bundle:
Set a URL for source maps. Useful for hosting them on a host that requires authorization.
And for cases when source maps are stored in the upper level directory:
With the following config:
Will produce the following URL:
This plugin is used to external the Node.js built-in modules during bundling, and is used by externalsPresets.node
under the hood.
This plugin is used to bundle out Node.js assets, often used with childCompiler.
Enable runtime module bundling for this chunkLoadingType, and is used by output.enabledChunkLoadingTypes under the hood.
Enable library format bundling for this libraryType, and is used by output.enabledLibraryTypes under the hood.
Enable runtime module bundling for this wasmLoadingType, and is used by output.enabledWasmLoadingTypes under the hood.
This plugin is used to external the Electron built-in modules during bundling, and is used by externalsPresets.electron
, externalsPresets.electronMain
, externalsPresets.electronRenderer
, and externalsPresets.electronPreload
under the hood.
This plugin implements Module Federation 1.5.
implementation
string
runtimePlugins
string[]
name
string
filename
string
runtime
string | false
library
LibraryOptions
shareScope
string
"default"
.remoteType
ExternalsType
"script"
, which loads via the <script />
tag.remotes
exposes
shared
Type:
Usage: Specify which dependencies should be shared dependencies. This allows multiple micro-frontends to share the same instance of a dependency library to avoid loading the same code repeatedly. It can be an object dictionary where the keys are the names of the shared modules and the values are configuration or version strings. It can also be an array where the array items are the shared package names or configurations.
The SharedConfig can include the following sub-options:
true
, the shared module will be loaded in the initial chunk instead of being dynamically loaded when used. This means that the shared module will be loaded together with the main entry point regardless of whether it has been used. This can eliminate the delay caused by dynamic loading, but it will increase the size of the initial package. Also, please note that when this configuration is enabled, all provided modules and fallback modules will always be downloaded.package.json
. Configuration is only necessary when the package name cannot be automatically determined based on the request."^1.2.3"
. Used to set the version range of shared modules. If the module version of the remote container does not meet this range, the module will not be loaded."default"
.requiredVersion
. If set to true
, the shared module must match the version specified in requiredVersion exactly, otherwise an error will be reported and the module will not be loaded. If set to false
, it can tolerate imprecise matching.package.json
will be used.This plugin corresponds to Module Federation 1.0, which is the ModuleFederationPlugin in Webpack.
The configuration is consistent with the ModuleFederationPlugin above, except for the two fields implementation
and runtimePlugins
.
These plugins are Rspack's unique internal plugin and follow the XxxRspackPlugin naming scheme.
This plugin can be used to create html files that are associated with Rspack assets.
options
{}
Name | Type | Default | Description |
---|---|---|---|
title | string|undefined | undefined | The title to use for the generated HTML document. |
filename | string | 'index.html' | The file to write the HTML to. Defaults to index.html . You can specify a subdirectory here too (eg: pages/index.html). |
template | string|undefined | undefined | The template file path. |
templateContent | string|undefined | undefined | The template file content, priority is greater than template. |
templateParameters | Record<string, string> | {} | Allows to overwrite the parameters used in the template. |
inject | 'head'|'body'|undefined | undefined | The script and link tag inject position in template . |
publicPath | string | '' | The publicPath used for script and link tags. |
scriptLoading | 'blocking'|'defer'|'module' | 'defer' | Modern browsers support non blocking javascript loading ('defer') to improve the page startup performance. Setting to 'module' adds attribute type='module'. This also implies 'defer', since modules are automatically deferred. |
chunks | string[]|undefined | undefined | Allows you to add only some chunks. |
excludedChunks | string[]|undefined | undefined | Allows you to skip some chunks. |
sri | 'sha256'|'sha384'|'sha512'|undefined | undefined | The sri hash algorithm, disabled by default. |
minify | boolean | false | Controls whether to minify the output. |
favicon | string|undefined | undefined | Adds the given favicon path to the output HTML. |
meta | Record<string, string|Record<string, string>> | {} | Allows to inject meta-tags. |
For complex HTML configuration requirements, you can use html-webpack-plugin (after v0.3.3) or @rspack/plugin-html (before v0.3.3).
This plugin can be used to compress JS assets. See optimization.minimizer.
options
For details of each options, please visit here.
This plugin can be used to compress CSS assets. See optimization.minimizer.
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.
Copies individual files or entire directories, which already exist, to the build directory.
options
undefined
Name | Type | Default | Description |
---|---|---|---|
from | string | undefined | The source path of the copy operation, which can be an absolute path, a relative path, or a glob search string. It can refer to a file or a directory. If a relative path is passed, it is relative to the context configuration. |
to | string | undefined | The destination of the copy operation, which can be an absolute path, a relative path, or a template string, such as '[name].[hash][ext]' . If not specified, it is equal to output path. |
context | string | undefined | This configuration determines how the "from" path is matched and the resulting structure after copying. |
toType | 'dir'|'file'|'template' | undefined | Specify the type of to , which can be a directory, a file, or a template name in rspack. If not specified, it will be automatically inferred. |
noErrorOnMissing | boolean | false | Ignore error if there are missing files or directories. |
force | boolean | false | Whether to overwrite the asset if it already exist. |
priority | number | 0 | When force is set to true , if a matching file is found, the one with higher priority will overwrite the one with lower priority. |
globOptions | object | undefined | The configuration for glob queries: caseSensitiveMatch determines whether the matching is case sensitive, and dot determines whether files starting with . are matched. ignore is an array of strings in glob format that can be used to ignore specific paths. |
For example:
The result of running with the above configuration would be: "dist/file.txt"
.
The result of running with the above configuration would be: files and directories inside directory
will be placed at output path.
The result of running with the above configuration would be that the directory
folder is moved to the newdirectory
folder within the output folder, for example dist/newdirectory/directory/foo
.