Rsbuild provides a wide range of configuration options with sensible defaults for most use cases. In most scenarios, you can use Rsbuild out of the box without any configuration.
When you need to customize build behavior, use these configuration options.
The configuration structure of Rsbuild looks like this:
export default {
plugins: [
// configure Rsbuild plugins
],
dev: {
// options for local development
},
html: {
// options for HTML generation
},
tools: {
// options for the low-level tools
},
output: {
// options for build outputs
},
resolve: {
// options for module resolution
},
source: {
// options for input source code
},
server: {
// options for the Rsbuild server,
// will take effect during local development and preview
},
security: {
// options for Web security
},
performance: {
// options for build performance and runtime performance
},
moduleFederation: {
// options for module federation
},
environments: {
// define different Rsbuild configurations for each environment
},
};
You can find detailed descriptions of all configs on the Configure Overview page.
When you use the CLI of Rsbuild, Rsbuild will automatically read the configuration file in the root directory of the current project and resolve it in the following order:
We recommend using the .mjs
or .ts
format for the configuration file and importing the defineConfig
utility function from @rsbuild/core
. It provides friendly TypeScript type hints and autocompletion, which can help you avoid errors in the configuration.
For example, in rsbuild.config.ts
, you can define the Rsbuild resolve.alias configuration:
import { defineConfig } from '@rsbuild/core';
export default defineConfig({
resolve: {
alias: {
'@common': './src/common',
},
},
});
If you are developing a non-TypeScript project, you can use the .mjs
format for the configuration file:
import { defineConfig } from '@rsbuild/core';
export default defineConfig({
resolve: {
alias: (opts) => {
opts['@common'] = './src/common';
},
},
});
Rsbuild CLI uses the --config
option to specify the config file, which can be set to a relative path or an absolute path.
For example, if you need to use the rsbuild.prod.config.mjs
file when running build
, you can add the following scripts to package.json
:
{
"scripts": {
"build": "rsbuild build --config rsbuild.prod.config.mjs"
}
}
You can also abbreviate the --config
option to -c
:
rsbuild build -c rsbuild.prod.config.mjs
Rsbuild provides three ways to load configuration files:
jiti
(Default): When you use a configuration file with the .ts
, .mts
, and .cts
extensions, Rsbuild will use jiti to load configuration files, providing interoperability between ESM and CommonJS. The behavior of module resolution differs slightly from the native behavior of Node.js.
native
: Use Node.js native loader to load the configuration file. This can ensure that the module resolution behavior is consistent with the native behavior of Node.js and has better performance. This requires that your JavaScript runtime already natively supports TypeScript.
For example, Node.js v22.6.0+ already natively supports TypeScript, you can use the following command to use the Node.js native loader to load the configuration file:
# Node.js >= v22.18.0
# No need to set --experimental-strip-types
npx rsbuild build --config-loader native
# Node.js v22.6.0 - v22.17.1
# Need to set --experimental-strip-types
NODE_OPTIONS="--experimental-strip-types" npx rsbuild build --config-loader native
auto
: Use Node.js's native loader to load configuration files first, fallback to using jiti if it fails.
When using Node.js's native loader, please note the following limitations:
When importing JSON files, you need to use import attributes:
import pkgJson from './package.json' with { type: 'json' }; // ✅ Correct
import pkgJson from './package.json'; // ❌ Incorrect
When importing TypeScript files, you need to include the .ts
extension:
import baseConfig from './rsbuild.base.config.ts'; // ✅ Correct
import baseConfig from './rsbuild.base.config'; // ❌ Incorrect
See Node.js - Running TypeScript Natively for more details.
In the configuration file, you can use Node.js environment variables such as process.env.NODE_ENV
to dynamically set different configurations:
import { defineConfig } from '@rsbuild/core';
export default defineConfig({
resolve: {
alias: {
'@request':
process.env.NODE_ENV === 'development'
? './src/request.dev.js'
: './src/request.prod.js',
},
},
});
Rsbuild supports the export of a function in the config file, where you can dynamically compute the config and return it to Rsbuild.
import { defineConfig } from '@rsbuild/core';
export default defineConfig(({ env, command, envMode }) => ({
resolve: {
alias: {
'@foo': env === 'development' ? './src/foo.dev.ts' : './src/foo.prod.ts',
},
},
}));
The exported config function must provide a return value. If you do not need to return any config, you can return an empty object.
The function accepts the following parameters:
string
process.env.NODE_ENV
The current running environment.
rsbuild dev
, the default value of env is development
.rsbuild build
or rsbuild preview
, the default value of env is production
.string
process.env.NODE_ENV
The current value of the CLI parameter --env-mode
.
For example, when running rsbuild build --env-mode test
, the value of envMode
is test
.
string
The current running CLI command, such as dev
, build
, preview
.
Rsbuild also supports the export of an async function in the config file, where you can perform some async operations:
import { defineConfig } from '@rsbuild/core';
export default defineConfig(async ({ env, command }) => {
const result = await someAsyncFunction();
return {
html: {
title: result,
},
};
});
You can use the mergeRsbuildConfig function exported by @rsbuild/core
to merge multiple configurations.
import { defineConfig, mergeRsbuildConfig } from '@rsbuild/core';
const config1 = defineConfig({
dev: { port: '3000' },
});
const config2 = defineConfig({
dev: { port: '3001' },
});
// { dev: { port: '3001' }
export default mergeRsbuildConfig(config1, config2);
You can enable Rsbuild's debug mode by adding the DEBUG=rsbuild
environment variable when executing a build.
DEBUG=rsbuild pnpm dev
In debug mode, Rsbuild will write the Rsbuild config to the dist directory, which is convenient for developers to view and debug.
config inspection completed, open the following files to view the content:
- Rsbuild config: /Project/demo/dist/.rsbuild/rsbuild.config.mjs
- Rspack config (web): /Project/demo/dist/.rsbuild/rspack.config.web.mjs
Open the generated /dist/.rsbuild/rsbuild.config.mjs
file to see the complete content of the Rsbuild config.
For a complete introduction to debug mode, see the Debug Mode chapter.