Plugins provide a function similar to (options?: PluginOptions) => RsbuildPlugin as an entry point. It is recommended to name the plugin function pluginXXX and export it by name.
Rsbuild uses lifetime planning work internally, and plugins can also register hooks to take part in any stage of the workflow and implement their own features.
The full list of Rsbuild's lifetime hooks can be found in the API References.
The Rsbuild does not take over the hooks of the underlying Rspack, whose documents can be found here: Rspack Plugin API。
Custom plugins can usually get config from function parameters,
just define and use it at your pleasure.
But sometimes you may need to read and change the public config of the Rsbuild. To begin with, you should understand how the Rsbuild generates and uses its config:
Read, parse config and merge with default values.
Plugins modify the config by api.modifyRsbuildConfig(...).
Normalize the config and provide it to consume, then the config can no longer be modified.
Refer to this tiny example:
exportconst pluginUploadDist =(): RsbuildPlugin =>({ name:'plugin-upload-dist',setup(api){ api.modifyRsbuildConfig((config)=>{// try to disable minimize.// should deal with optional value by self. config.output ||={}; config.output.disableMinimize =true;// but also can be enable by other plugins...}); api.onBeforeBuild(()=>{// use the normalized config.const config = api.getNormalizedConfig();if(!config.output.disableMinimize){// let it crash when enable minimize.thrownewError('You must disable minimize to upload readable dist files.',);}}); api.onAfterBuild(()=>{const config = api.getNormalizedConfig();const distRoot = config.output.distPath.root;// TODO: upload all files in `distRoot`.});},});
There are 3 ways to use Rsbuild config:
register callback with api.modifyRsbuildConfig(config => {}) to modify config.
use api.getRsbuildConfig() to get Rsbuild config.
use api.getNormalizedConfig() to get finally normalized config.
When normalized, it will again merge the config object with the default values
and make sure the optional properties exist.
So for PluginUploadDist, part of its type looks like:
The return value type of getNormalizedConfig() is slightly different from that of RsbuildConfig and is narrowed compared to the types described elsewhere in the documentation.
You don't need to fill in the defaults when you use it.
Therefore, the best way to use configuration options is to
Modify the config with api.modifyRsbuildConfig(config => {})
Read api.getNormalizedConfig() as the actual config used by the plugin in the further lifetime.
Loaders can read and process different types of file modules, refer to concepts and loaders.
importtype{ RsbuildPlugin }from'@rsbuild/core';exportconst pluginTypeScriptExt =(): RsbuildPlugin =>({ name:'plugin-typescript-ext',setup(api){ api.modifyBundlerChain(async(chain)=>{// Set ts-loader to recognize more files as typescript modules chain.module.rule(CHAIN_ID.RULE.TS).test(/\.(ts|mts|cts|tsx|tss|tsm)$/);});},});