tools.bundlerChain

  • Type:
type BundlerChainFn = (
  chain: RspackChain,
  utils: ModifyBundlerChainUtils,
) => Promise<void> | void;
  • Default: undefined

rspack-chain is a utility library for configuring Rspack. It provides a chaining API, making the configuration of Rspack more flexible. By using rspack-chain, you can more easily modify and extend Rspack configurations without directly manipulating the complex configuration object.

You can use rspack-chain to modify the default Rspack config through tools.bundlerChain. Its value is a function that takes two arguments:

  • The first argument is a rspack-chain instance, which you can use to modify the Rspack config.
  • The second argument is an utils object, including env, isProd, CHAIN_ID, etc.

tools.bundlerChain will be executed earlier than tools.rspack, so it will be overridden by tools.rspack.

TIP

The built-in Rspack config in Rsbuild may change with iterations, and these changes won't be reflected in semver. Therefore, your custom config may become invalid when you upgrade Rsbuild.

Examples

Please refer to: RspackChain examples.

Utils

env

  • Type: 'development' | 'production' | 'test'

The env parameter can be used to determine whether the current environment is development, production or test. For example:

export default {
  tools: {
    bundlerChain: (chain, { env }) => {
      if (env === 'development') {
        chain.devtool('cheap-module-eval-source-map');
      }
    },
  },
};

isDev

  • Type: boolean

Used to determine whether the current build is a development build, such as:

export default {
  tools: {
    bundlerChain: (config, { isDev }) => {
      if (isDev) {
        config.devtool = 'eval-cheap-source-map';
      }
      return config;
    },
  },
};

isProd

  • Type: boolean

Used to determine whether the current build is a production build, such as:

export default {
  tools: {
    bundlerChain: (chain, { isProd }) => {
      if (isProd) {
        chain.devtool('source-map');
      }
    },
  },
};

target

  • Type: 'web' | 'node' | 'web-worker'

The target parameter can be used to determine the build target environment. For example:

export default {
  tools: {
    bundlerChain: (chain, { target }) => {
      if (target === 'node') {
        // ...
      }
    },
  },
};

isServer

  • Type: boolean

Determines whether the target environment is node, equivalent to target === 'node'.

export default {
  tools: {
    bundlerChain: (chain, { isServer }) => {
      if (isServer) {
        // ...
      }
    },
  },
};

isWebWorker

  • Type: boolean

Determines whether the target environment is web-worker, equivalent to target === 'web-worker'.

export default {
  tools: {
    bundlerChain: (chain, { isWebWorker }) => {
      if (isWebWorker) {
        // ...
      }
    },
  },
};

rspack

  • Type: Rspack

The Rspack instance. For example:

export default {
  tools: {
    bundlerChain: (chain, { rspack }) => {
      chain.plugin('extra-define').use(rspack.DefinePlugin, [
        {
          'process.env': {
            NODE_ENV: JSON.stringify(process.env.NODE_ENV),
          },
        },
      ]);
    },
  },
};

HtmlPlugin

  • Type: typeof import('html-rspack-plugin')

The default export of html-rspack-plugin.

export default {
  tools: {
    bundlerChain: (chain, { HtmlPlugin }) => {
      console.log(HtmlPlugin);
    },
  },
};

CHAIN_ID

Some common Chain IDs are predefined in the Rsbuild, and you can use these IDs to locate the built-in Rule or Plugin.

TIP

Please note that some of the rules or plugins listed below are not available by default. They will only be included in the Rspack or webpack configuration when you enable specific options or register certain plugins.

For example, the RULE.STYLUS rule exists only when the Stylus plugin is registered.

CHAIN_ID.RULE

IDDescription
RULE.JSRule for js and ts
RULE.SVGRule for svg
RULE.CSSRule for css
RULE.LESSRule for less
RULE.SASSRule for sass
RULE.YAMLRule for yaml
RULE.WASMRule for WASM
RULE.FONTRule for font
RULE.IMAGERule for image
RULE.MEDIARule for media
RULE.VUERule for vue (requires Vue plugin)
RULE.SVELTERule for svelte (requires Svelte plugin)
RULE.STYLUSRule for stylus (requires Stylus plugin)

CHAIN_ID.ONE_OF

ONE_OF.[ID] can match a certain type of rule in the rule array.

IDDescription
ONE_OF.SVG_URLRules for SVG, output as a separate file
ONE_OF.SVG_INLINERules for SVG, inlined into bundles as data URIs
ONE_OF.SVG_ASSETSRules for SVG, automatic choice between data URI and separate file

CHAIN_ID.USE

USE.[ID] can match a certain loader.

IDDescription
USE.SWCcorrespond to builtin:swc-loader
USE.STYLEcorrespond to style-loader
USE.POSTCSScorrespond to postcss-loader
USE.LESScorrespond to less-loader (requires Less plugin)
USE.SASScorrespond to sass-loader (requires Sass plugin)
USE.RESOLVE_URLcorrespond to resolve-url-loader (requires Sass plugin)
USE.VUEcorrespond to vue-loader (requires Vue plugin)
USE.SVGRcorrespond to svgr-loader (requires Svgr plugin)
USE.BABELcorrespond to babel-loader (requires Babel plugin)
USE.SVELTEcorrespond to svelte-loader (requires Svelte plugin)
USE.STYLUScorrespond to stylus-loader (requires Stylus plugin)

CHAIN_ID.PLUGIN

PLUGIN.[ID] can match a certain Rspack or webpack plugin.

IDDescription
PLUGIN.HTMLcorrespond to HtmlRspackPlugin, you need to concat the entry name when using: ${PLUGIN.HTML}-${entryName}
PLUGIN.SUBRESOURCE_INTEGRITYcorrespond to SubresourceIntegrityPlugin
PLUGIN.BUNDLE_ANALYZERcorrespond to BundleAnalyzerPlugin

CHAIN_ID.MINIMIZER

MINIMIZER.[ID] can match a certain minimizer.

IDDescription
MINIMIZER.JScorrespond to SwcJsMinimizerRspackPlugin
MINIMIZER.CSScorrespond to LightningCssMinimizerRspackPlugin