This section describes some low-level tools configurations in Modern.js Builder.
Object | Function
You can modify the config of autoprefixer by tools.autoprefixer
.
When tools.autoprefixer
is configured as Object
type, it is merged with the default config through Object.assign. For example:
When tools.autoprefixer
is a Function, the default config is passed as the first parameter and can be directly modified or returned as the final result. For example:
Object | Function
undefined
With tools.babel
you can modify the options of babel-loader.
Please note the limitations of tools.babel
in the following usage scenarios:
tools.babel
option will significantly slow down the Rspack's build speed. This is because Rspack defaults to using SWC for compilation, and configuring Babel will cause the code to be compiled twice, resulting in additional compilation overhead.tools.babel
option will not take effect.When tools.babel
is of type Function
, the default Babel configuration will be passed as the first parameter. You can directly modify the configuration object or return an object as the final babel-loader
configuration.
The second parameter of the tools.babel
function provides some more convenient utility functions. Please continue reading the documentation below.
The above example is just for reference, usually you don't need to manually configure babel-plugin-import
, because the Builder already provides a more general source.transformImport
configuration.
When tools.babel
's type is Object
, the config will be shallow merged with default config by Object.assign
.
Note that Object.assign
is a shallow copy and will completely overwrite the built-in presets
or plugins
array, please use it with caution.
When tools.babel
is a Function, the tool functions available for the second parameter are as follows:
(plugins: BabelPlugin[]) => void
Add some Babel plugins. For example:
(presets: BabelPlugin[]) => void
Add Babel preset configuration. (No need to add presets in most cases)
(plugins: string | string[]) => void
To remove the Babel plugin, just pass in the name of the plugin to be removed, you can pass in a single string or an array of strings.
(presets: string | string[]) => void
To remove the Babel preset configuration, pass in the name of the preset to be removed, you can pass in a single string or an array of strings.
(options: PresetEnvOptions) => void
Modify the configuration of @babel/preset-env, the configuration you pass in will be shallowly merged with default config. For example:
(options: PresetReactOptions) => void
Modify the configuration of @babel/preset-react, the configuration you pass in will be shallowly merged with default config. For example:
Deprecated, please use source.include instead, both have the same functionality.
Deprecated, please use source.exclude instead, both have the same functionality.
After modifying the babel-loader
configuration through tools.babel
, you can view the final generated configuration in Builder debug mode.
First, enable debug mode by using the DEBUG=builder
parameter:
Then open the generated (webpack|rspack).config.web.js
file and search for the babel-loader
keyword to see the complete babel-loader
configuration.
Function | undefined
undefined
You can modify the webpack and Rspack configuration by configuring tools.bundlerChain
which is type of Function
. The function receives two parameters, the first is the original bundler chain object, and the second is an object containing some utils.
Bundler chain is a subset of webpack chain, which contains part of the webpack chain API that you can use to modify both webpack and Rspack configuration.
Configurations modified via bundler chain will work on both webpack and Rspack builds. Note that the bundler chain only supports modifying the configuration of the non-differentiated parts of webpack and Rspack. For example, modifying the devtool configuration option (webpack and Rspack have the same devtool property value type), or adding an Rspack-compatible webpack plugin.
tools.bundlerChain
is executed earlier than tools.webpackChain / tools.webpack / tools.rspack and thus will be overridden by changes in others.
For more information, please refer to Rsbuild#tools.bundlerChain
Object | Function
only support webpack
The config of mini-css-extract-plugin can be modified through tools.cssExtract
.
When this value is an Object, it is merged with the default config via Object.assign. For example:
When the value a Function, the default config is passed in as the first parameter. You can modify the config object directly, or return an object as the final config. For example:
For more config details, please refer to mini-css-extract-plugin.
Object | Function
undefined
The config of css-loader can be modified through tools.cssLoader
. The default config is as follows:
When using Rspack as the bundler, this configuration is only supported when set disableCssExtract is true.
To modify CSS Modules configuration, it is recommended to use the output.cssModules configuration.
When this value is an Object, it is merged with the default config via deep merge. For example:
When the value is a Function, the default config is passed in as the first parameter. You can modify the config object directly, or return an object as the final config. For example:
Object
{}
The config of DevServer can be modified through tools.devServer
.
Modern.js does not directly use webpack-dev-server or @rspack/dev-server, but implement DevServer based on webpack-dev-middleware.
Array
[]
Provides the ability to execute custom middleware after all other middleware internally within the server.
webpack-dev-server
uses Express as the server-side framework. Modern.js does not use any framework, and the req
and res
in the above middleware are all native Node objects. Therefore, the Express middleware used in webpack-dev-server
may not be directly usable in Modern.js.
If you want to migrate the Express middleware used in webpack-dev-server
, you can use the following method to pass the Express app as middleware:
Array
[]
Provides the ability to execute custom middleware prior to all other middleware internally within the server.
The config of HMR client, which are usually used to set the WebSocket URL of HMR.
boolean
true
Whether to enable gzip compression for served static assets.
If you want to disable the gzip compression, you can set compress
to false
:
The config of devMiddleware. Current options is the subset of webpack-dev-middleware.
Record<string, string>
undefined
Adds headers to all responses.
boolean | ConnectHistoryApiFallbackOptions
false
The index.html page will likely have to be served in place of any 404 responses. Enable devServer.historyApiFallback
by setting it to true
:
For more options and information, see the connect-history-api-fallback documentation.
boolean
true
Enable Hot Module Replacement feature.
boolean | { key: string; cert: string }
false
By default, DevServer will be served over HTTP. It can optionally be served over HTTPS by setting devServer.https
to true
, and will disable the HTTP server.
You can also manually pass in the certificate and corresponding private key required by the HTTPS server:
boolean
true
By default, the DevServer will reload/refresh the page when file changes are detected (devServer.hot
option must be disabled in order for liveReload to take effect). Disable devServer.liveReload
by setting it to false
.
undefined
Provides the ability to execute a custom function and apply custom middlewares.
The order among several different types of middleware is: devServer.before
=> unshift
=> internal middlewares => push
=> devServer.after
.
It is possible to use some server api to meet special scenario requirements:
Record<string, string> | Record<string, ProxyDetail>
undefined
Proxying some URLs.
A request to /api/users will now proxy the request to http://localhost:3000/api/users.
If you don't want /api to be passed along, we need to rewrite the path:
The DevServer Proxy makes use of the http-proxy-middleware package. Check out its documentation for more advanced usages.
The full type definition of DevServer Proxy is:
In addition to the http-proxy-middleware option, we also support the bypass and context configuration:
null
or undefined
to continue processing the request with proxy.false
to produce a 404 error for the request.boolean
true
Whether to watch files change in directories such as mock/
, server/
, api/
.
false | Object | Function
The configs of html-webpack-plugin or @rspack/plugin-html can be modified through tools.htmlPlugin
.
When tools.htmlPlugin
is Object
type, the value will be merged with the default config via Object.assign
.
When tools.htmlPlugin
is a Function:
The built-in html-webpack-plugin
plugins can be disabled by set tools.htmlPlugin
to false
.
By default, Builder will compresses JavaScript/CSS code inside HTML during the production build to improve the page performance. This ability is often helpful when using custom templates or inserting custom scripts.
However, when output.enableInlineScripts
or output.enableInlineStyles
is turned on, inline JavaScript/CSS code will be repeatedly compressed, which will have a certain impact on build performance. You can modify the default minify behavior by modifying the tools.htmlPlugin.minify
configuration.
Object | Function
You can modify the config of less-loader via tools.less
.
When tools.less
is configured as Object
type, it is merged with the default config through Object.assign in a shallow way. It should be noted that lessOptions
is merged through deepMerge in a deep way. For example:
When tools.less
is a Function, the default config is passed as the first parameter, which can be directly modified or returned as the final result. The second parameter provides some utility functions that can be called directly. For example:
In some scenarios, if you need to use a specific version of Less instead of the built-in Less v4 in Builder, you can install the desired Less version in your project and set it up using the implementation
option of the less-loader
.
(excludes: RegExp | RegExp[]) => void
Used to specify which files less-loader
does not compile, You can pass in one or more regular expressions to match the path of less files, for example:
Object | Function | undefined
When building for production, Builder will minimize the CSS code through css-minimizer-webpack-plugin. The config of css-minimizer-webpack-plugin can be modified via tools.minifyCss
.
When tools.minifyCss
is Object
type, it will be merged with the default config via Object.assign
.
For example, modify the preset
config of cssnano:
When tools.minifyCss
is Function
type, the default config is passed in as the first parameter, the config object can be modified directly, or a value can be returned as the final result.
Object | Function
Builder integrates PostCSS by default, you can configure postcss-loader through tools.postcss
.
When the value is a Function, the internal default config is passed as the first parameter, and the config object can be modified directly without returning, or an object can be returned as the final result; the second parameter is a set of tool functions for modifying the postcss-loader config.
For example, you need to add a PostCSS plugin on the basis of the original plugin, and push a new plugin to the postcssOptions.plugins array:
When you need to pass parameters to the PostCSS plugin, you can pass them in by function parameters:
tools.postcss
can return a config object and completely replace the default config:
When this value is an Object, it is merged with the default config via Object.assign
. Note that Object.assign
is a shallow copy and will completely overwrite the built-in presets
or plugins
array, please use it with caution.
(plugins: PostCSSPlugin | PostCSSPlugin[]) => void
For adding additional PostCSS plugins, You can pass in a single PostCSS plugin, or an array of PostCSS plugins.
Builder uses the PostCSS v8 version. When you use third-party PostCSS plugins, please pay attention to whether the PostCSS version is compatible. Some legacy plugins may not work in PostCSS v8.
true | Object | Function | undefined
false
Configure the Pug template engine.
Pug template engine is not enabled by default, you can enable it by setting tools.pug
to true
.
When enabled, you can use index.pug
as the template file in html.template
config.
When tools.terser
is Object
type, you can passing the Pug options:
For detailed options, please refer to Pug API Reference.
When tools.pug
is Function
type, the default configuration is passed in as the first parameter, the configuration object can be modified directly, or a value can be returned as the final result.
Object | Function
You can modify the config of sass-loader via tools.sass
.
When tools.sass
is Object
type, it is merged with the default config through Object.assign. It should be noted that sassOptions
is merged through deepMerge in a deep way.
For example:
When tools.sass
is a Function, the default config is passed as the first parameter, which can be directly modified or returned as the final result. The second parameter provides some utility functions that can be called directly. For Example:
In some scenarios, if you need to use a specific version of Sass instead of the built-in Dart Sass v1 in Builder, you can install the desired Sass version in your project and set it up using the implementation
option of the sass-loader
.
(excludes: RegExp | RegExp[]) => void
Used to specify which files sass-loader
does not compile, You can pass in one or more regular expressions to match the path of sass files, for example:
Object | Function
{}
The config of style-loader can be set through tools.styleLoader
.
It is worth noting that Builder does not enable style-loader
by default. You can use output.disableCssExtract
config to enable it.
When this value is an Object, it is merged with the default config via Object.assign. For example:
When the value is a Function, the default config is passed in as the first parameter. You can modify the config object directly, or return an object as the final config. For example:
Object | Function
tools.styledComponents
config is corresponding to babel-plugin-styled-components, or @swc/plugin-styled-components when using SWC plugin.
When the value is an Object, use the Object.assign function to merge with the default config. For example:
When the config is a Function, the first parameter is the default configuration, and the second parameter provides some utility functions that can be called directly:
The feature is enabled by default, and you can configure tools.styledComponents
to false
to disable this behavior, which can improve build performance:
Object | Function | undefined
only support webpack
When building for production, Builder will minimize the JavaScript code through terser-webpack-plugin. The config of terser-webpack-plugin can be modified via tools.terser
.
When tools.terser
is Object
type, it will be merged with the default config via Object.assign
.
For example, to exclude some files from minification:
When tools.terser
is Function
type, the default config is passed in as the first parameter, the config object can be modified directly, or a value can be returned as the final result.
If you need to disable code minification, you can use the output.disableMinimize configuration.
Object | Function | undefined
undefined
only support webpack
Using babel-loader or Rspack instead of ts-loader can significantly improve compilation speed and provide better extendability.
ts-loader cannot be used with certain features such as source.transformImport and tools.styledComponents provided by Babel & SWC.
ts-loader
is not enabled by default in the project. When tools.tsLoader
is not undefined, builder will use ts-loader instead of babel-loader to compile TypeScript code.
When this value is an Object, it is merged with the default configuration via Object.assign.
The default configuration is as follows:
You can override the default configuration via the tools.tsLoader
configuration option:
When this value is a Function, the default configuration is passed in as the first parameter, the configuration object can be modified directly, or an object can be returned as the final configuration.The second parameter is the util functions to modify the ts-loader
configuration. For example:
Deprecated, please use source.include instead, both have the same functionality.
Deprecated, please use source.exclude instead, both have the same functionality.
Object | Function
By default, the fork-ts-checker-webpack-plugin is enabled for type checking. You can use output.disableTsChecker
config to disable it.
When the value of tsChecker
is of type Object, it will be deeply merged with the default configuration.
When the value of tsChecker
is of type Function, the default configuration will be passed as the first argument. You can directly modify the configuration object or return an object as the final configuration.
Object | Function | undefined
undefined
only support webpack
tools.webpack
is used to configure webpack.
tools.bundlerChain
is also used to modify the webpack configuration, and the function is more powerful. It is recommended to usetools.bundlerChain
first.
tools.webpack
can be configured as an object to be deep merged with the built-in webpack configuration through webpack-merge.
For example, add resolve.alias
configuration:
tools.webpack
can be configured as a function. The first parameter of this function is the built-in webpack configuration object, you can modify this object, and then return it. For example:
The object returned by the tools.webpack
function is used directly as the final webpack configuration and is not merged with the built-in webpack configuration.
The second parameter of this function is an object, which contains some utility functions and properties, as follows:
'development' | 'production' | 'test'
The env
parameter can be used to determine whether the current environment is development, production or test. For example:
boolean
The isProd
parameter can be used to determine whether the current environment is production. For example:
'web' | 'node' | 'modern-web' | 'web-worker'
The target
parameter can be used to determine the current target. For example:
boolean
Determines whether the target environment is node
, equivalent to target === 'node'
.
boolean
Determines whether the target environment is web-worker
, equivalent to target === 'web-worker'
.
typeof import('webpack')
The webpack instance. For example:
typeof import('html-webpack-plugin')
The HtmlWebpackPlugin instance:
(rules: RuleSetRule | RuleSetRule[]) => void
Add additional webpack rules.
For example:
(plugins: WebpackPluginInstance | WebpackPluginInstance[]) => void
Add additional plugins to the head of the internal webpack plugins array, and the plugin will be executed first.
(plugins: WebpackPluginInstance | WebpackPluginInstance[]) => void
Add additional plugins at the end of the internal webpack plugins array, the plugin will be executed last.
(name: string) => void
Remove the internal webpack plugin, the parameter is the constructor.name
of the plugin.
For example, remove the internal fork-ts-checker-webpack-plugin:
(...configs: WebpackConfig[]) => WebpackConfig
Used to merge multiple webpack configs, same as webpack-merge.
(name: string) => string
Get the path to the builder built-in dependencies, same as webpackChain#getCompiledPath.
Function | undefined
undefined
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 intools.webpack
.
'development' | 'production' | 'test'
The env
parameter can be used to determine whether the current environment is development, production or test. For example:
boolean
The isProd
parameter can be used to determine whether the current environment is production. For example:
'web' | 'node' | 'modern-web' | 'web-worker'
The target
parameter can be used to determine the current environment. For example:
boolean
Determines whether the target environment is node
, equivalent to target === 'node'
.
boolean
Determines whether the target environment is web-worker
, equivalent to target === 'web-worker'
.
typeof import('webpack')
The webpack instance. For example:
typeof import('html-webpack-plugin')
The HtmlWebpackPlugin instance:
(name: string) => string
Get the path to the builder built-in dependencies, such as:
This method is usually used when you need to reuse the same dependency with the builder.
Builder built-in dependencies are subject to change with version iterations, e.g. generate large version break changes. Please avoid using this API if it is not necessary.
Some common Chain IDs are predefined in the Builder, and you can use these IDs to locate the built-in Rule or Plugin.
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.
ID | Description |
---|---|
RULE.JS |
Rule for js |
RULE.TS |
Rule for ts |
RULE.CSS |
Rule for css |
RULE.LESS |
Rule for less |
RULE.SASS |
Rule for sass |
RULE.STYLUS |
Rule for stylus (requires Stylus plugin) |
RULE.SVG |
Rule for svg |
RULE.PUG |
Rule for pug |
RULE.TOML |
Rule for toml |
RULE.YAML |
Rule for yaml |
RULE.WASM |
Rule for WASM |
RULE.NODE |
Rule for node |
RULE.FONT |
Rule for font |
RULE.IMAGE |
Rule for image |
RULE.MEDIA |
Rule for media |
ONE_OF.XXX
can match a certain type of rule in the rule array.
ID | Description |
---|---|
ONE_OF.SVG |
Rules for SVG, automatic choice between data URI and separate file |
ONE_OF.SVG_URL |
Rules for SVG, output as a separate file |
ONE_OF.SVG_INLINE |
Rules for SVG, inlined into bundles as data URIs |
ONE_OF.SVG_ASSETS |
Rules for SVG, automatic choice between data URI and separate file |
USE.XXX
can match a certain loader.
ID | Description |
---|---|
USE.TS |
correspond to ts-loader |
USE.CSS |
correspond to css-loader |
USE.LESS |
correspond to less-loader |
USE.SASS |
correspond to sass-loader |
USE.STYLUS |
correspond to stylus-loader |
USE.PUG |
correspond to pug-loader |
USE.VUE |
correspond to vue-loader |
USE.TOML |
correspond to toml-loader |
USE.YAML |
correspond to yaml-loader |
USE.NODE |
correspond to node-loader |
USE.URL |
correspond to url-loader |
USE.SVGR |
correspond to @svgr/webpack |
USE.BABEL |
correspond to babel-loader |
USE.STYLE |
correspond to style-loader |
USE.POSTCSS |
correspond to postcss-loader |
USE.CSS_MODULES_TS |
correspond to css-modules-typescript-loader |
USE.MINI_CSS_EXTRACT |
correspond to mini-css-extract-plugin.loader |
USE.RESOLVE_URL_LOADER_FOR_SASS |
correspond to resolve-url-loader |
PLUGIN.XXX
can match a certain webpack plugin.
ID | Description |
---|---|
PLUGIN.HMR |
correspond to HotModuleReplacementPlugin |
PLUGIN.COPY |
correspond to CopyWebpackPlugin |
PLUGIN.HTML |
correspond to HtmlWebpackPlugin , you need to splice the entry name when using: ${PLUGIN.HTML}-${entryName} |
PLUGIN.DEFINE |
correspond to DefinePlugin |
PLUGIN.IGNORE |
correspond to IgnorePlugin |
PLUGIN.BANNER |
correspond to BannerPlugin |
PLUGIN.PROGRESS |
correspond to Webpackbar |
PLUGIN.APP_ICON |
correspond to AppIconPlugin |
PLUGIN.MANIFEST |
correspond to WebpackManifestPlugin |
PLUGIN.TS_CHECKER |
correspond to ForkTsCheckerWebpackPlugin |
PLUGIN.INLINE_HTML |
correspond to InlineChunkHtmlPlugin |
PLUGIN.BUNDLE_ANALYZER |
correspond to WebpackBundleAnalyzer |
PLUGIN.MINI_CSS_EXTRACT |
correspond to MiniCssExtractPlugin |
PLUGIN.VUE_LOADER_PLUGIN |
correspond to VueLoaderPlugin |
PLUGIN.REACT_FAST_REFRESH |
correspond to ReactFastRefreshPlugin |
PLUGIN.NODE_POLYFILL_PROVIDE |
correspond to ProvidePlugin for node polyfills |
PLUGIN.SUBRESOURCE_INTEGRITY |
correspond to webpack-subresource-integrity |
PLUGIN.ASSETS_RETRY |
correspond to webpack static asset retry plugin in Builder |
PLUGIN.AUTO_SET_ROOT_SIZE |
correspond to automatically set root font size plugin in Builder |
MINIMIZER.XXX
can match a certain minimizer.
ID | Description |
---|---|
MINIMIZER.JS |
correspond to TerserWebpackPlugin |
MINIMIZER.CSS |
correspond to CssMinimizerWebpackPlugin |
MINIMIZER.ESBUILD |
correspond to ESBuildPlugin |
MINIMIZER.SWC |
correspond to SwcWebpackPlugin |
For usage examples, please refer to: WebpackChain usage examples.
Object | Function | undefined
undefined
only support Rspack
tools.rspack
is used to configure Rspack.
tools.rspack
can be configured as an object to be deep merged with the built-in Rspack configuration through webpack-merge.
For example, add resolve.alias
configuration:
tools.rspack
can be configured as a function. The first parameter of this function is the built-in Rspack configuration object, you can modify this object, and then return it. For example:
The object returned by the tools.rspack
function is used directly as the final Rspack configuration and is not merged with the built-in Rspack configuration.
The second parameter of this function is an object, which contains some utility functions and properties, as follows:
'development' | 'production' | 'test'
The env
parameter can be used to determine whether the current environment is development, production or test. For example:
boolean
The isProd
parameter can be used to determine whether the current environment is production. For example:
'web' | 'node' | 'modern-web' | 'web-worker'
The target
parameter can be used to determine the current target. For example:
boolean
Determines whether the target environment is node
, equivalent to target === 'node'
.
boolean
Determines whether the target environment is web-worker
, equivalent to target === 'web-worker'
.
typeof import('@rspack/core')
The Rspack instance. For example:
(rules: RuleSetRule | RuleSetRule[]) => void
Add additional Rspack rules.
For example:
(plugins: RspackPluginInstance | RspackPluginInstance[]) => void
Add additional plugins to the head of the internal Rspack plugins array, and the plugin will be executed first.
(plugins: RspackPluginInstance | RspackPluginInstance[]) => void
Add additional plugins at the end of the internal Rspack plugins array, the plugin will be executed last.
(name: string) => void
Remove the internal Rspack plugin, the parameter is the constructor.name
of the plugin.
For example, remove the internal webpack-bundle-analyzer:
(...configs: RspackConfig[]) => RspackConfig
Used to merge multiple Rspack configs, same as webpack-merge.
(name: string) => string
Get the path to the builder built-in dependencies, such as:
This method is usually used when you need to reuse the same dependency with the builder.
Builder built-in dependencies are subject to change with version iterations, e.g. generate large version break changes. Please avoid using this API if it is not necessary.