tools.webpackChain

  • Type: Function | undefined
  • Default: undefined
  • Bundler: only support webpack

You can modify the webpack configuration by configuring tools.webpackChain which is type of Function. The function receives two parameters, the first is the original webpack chain object, and the second is an object containing some utils.

Compared with tools.webpack, webpack-chain not only supports chained calls, but also can locate built-in Rule or Plugin based on aliases, so as to achieve precise config modification. We recommend using tools.webpackChain instead of tools.webpack.

tools.webpackChain is executed earlier than tools.webpack and thus will be overridden by changes in tools.webpack.

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: {
    webpackChain: (chain, { env }) => {
      if (env === 'development') {
        chain.devtool('cheap-module-eval-source-map');
      }
    },
  },
};

isProd

  • Type: boolean

The isProd parameter can be used to determine whether the current environment is production. For example:

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

target

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

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

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

isServer

  • Type: boolean

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

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

isWebWorker

  • Type: boolean

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

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

webpack

  • Type: typeof import('webpack')

The webpack instance. For example:

export default {
  tools: {
    webpackChain: (chain, { webpack }) => {
      chain.plugin('my-progress').use(webpack.ProgressPlugin);
    },
  },
};

HtmlWebpackPlugin

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

The HtmlWebpackPlugin instance:

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

CHAIN_ID

Some common Chain IDs are predefined in the Modern.js, 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 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
RULE.TSRule for ts
RULE.CSSRule for css
RULE.LESSRule for less
RULE.SASSRule for sass
RULE.STYLUSRule for stylus(requires Stylus plugin)
RULE.SVGRule for svg
RULE.PUGRule for pug
RULE.TOMLRule for toml
RULE.YAMLRule for yaml
RULE.WASMRule for WASM
RULE.NODERule for node
RULE.FONTRule for font
RULE.IMAGERule for image
RULE.MEDIARule for media

CHAIN_ID.ONE_OF

ONE_OF.XXX can match a certain type of rule in the rule array.

IDDescription
ONE_OF.SVGRules for SVG, automatic choice between data URI and separate file
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.XXX can match a certain loader.

IDDescription
USE.TScorrespond to ts-loader
USE.CSScorrespond to css-loader
USE.LESScorrespond to less-loader
USE.SASScorrespond to sass-loader
USE.STYLUScorrespond to stylus-loader
USE.PUGcorrespond to pug-loader
USE.VUEcorrespond to vue-loader
USE.TOMLcorrespond to toml-loader
USE.YAMLcorrespond to yaml-loader
USE.NODEcorrespond to node-loader
USE.URLcorrespond to url-loader
USE.SVGRcorrespond to @svgr/webpack
USE.BABELcorrespond to babel-loader
USE.STYLEcorrespond to style-loader
USE.POSTCSScorrespond to postcss-loader
USE.CSS_MODULES_TScorrespond to css-modules-typescript-loader
USE.MINI_CSS_EXTRACTcorrespond to mini-css-extract-plugin.loader
USE.RESOLVE_URL_LOADER_FOR_SASScorrespond to resolve-url-loader

CHAIN_ID.PLUGIN

PLUGIN.XXX can match a certain webpack plugin.

IDDescription
PLUGIN.HMRcorrespond to HotModuleReplacementPlugin
PLUGIN.COPYcorrespond to CopyWebpackPlugin
PLUGIN.HTMLcorrespond to HtmlWebpackPlugin, you need to splice the entry name when using: ${PLUGIN.HTML}-${entryName}
PLUGIN.DEFINEcorrespond to DefinePlugin
PLUGIN.IGNOREcorrespond to IgnorePlugin
PLUGIN.BANNERcorrespond to BannerPlugin
PLUGIN.PROGRESScorrespond to Webpackbar
PLUGIN.APP_ICONcorrespond to AppIconPlugin
PLUGIN.MANIFESTcorrespond to WebpackManifestPlugin
PLUGIN.TS_CHECKERcorrespond to ForkTsCheckerWebpackPlugin
PLUGIN.INLINE_HTMLcorrespond to InlineChunkHtmlPlugin
PLUGIN.BUNDLE_ANALYZERcorrespond to WebpackBundleAnalyzer
PLUGIN.MINI_CSS_EXTRACTcorrespond to MiniCssExtractPlugin
PLUGIN.VUE_LOADER_PLUGINcorrespond to VueLoaderPlugin
PLUGIN.REACT_FAST_REFRESHcorrespond to ReactFastRefreshPlugin
PLUGIN.NODE_POLYFILL_PROVIDEcorrespond to ProvidePlugin for node polyfills
PLUGIN.SUBRESOURCE_INTEGRITYcorrespond to webpack-subresource-integrity
PLUGIN.ASSETS_RETRYcorrespond to webpack static asset retry plugin
PLUGIN.AUTO_SET_ROOT_SIZEcorrespond to automatically set root font size plugin

CHAIN_ID.MINIMIZER

MINIMIZER.XXX can match a certain minimizer.

IDDescription
MINIMIZER.JScorrespond to TerserWebpackPlugin
MINIMIZER.CSScorrespond to CssMinimizerWebpackPlugin
MINIMIZER.ESBUILDcorrespond to ESBuildPlugin
MINIMIZER.SWCcorrespond to SwcWebpackPlugin

Examples

For usage examples, please refer to: Rsbuild - bundlerChain examples.