>=1.1.7
Set the alias for the module path, which is used to simplify the import path or redirect the module reference.
For TypeScript projects, you only need to configure compilerOptions.paths in the tsconfig.json
file. The Rsbuild will automatically recognize it, so there is no need to configure the resolve.alias
option separately. For more details, please refer to Path Aliases.
In versions prior to Rsbuild 1.1.7, you can use the source.alias
to set alias, but it will be removed in the next major version.
resolve.alias
can be an object, the key
is the module path in the source code to be replaced, and the value
is the target path to which the module will be mapped.
With the above configuration, if @common/Foo.tsx
is imported in the code, it will be mapped to the <project-root>/src/common/Foo.tsx
.
The resolve.alias
can be a function, it will accept the previous alias object, and you can modify it.
To remove the built-in @swc/helpers
alias, delete it in the function:
You can also return a new object as the final result in the function, which will replace the preset alias object.
Rsbuild's resolve.alias
is similar to Rspack's resolve.alias configuration, but there are some differences:
resolve.alias
is a relative path, Rsbuild will automatically convert it to an absolute path to ensure that the path is relative to the project root.When you build for multiple environments, you can set different alias for each environment:
For example, set different alias for web
and node
environments:
By default, resolve.alias
will automatically match sub-paths, for example, with the following configuration:
It will match as follows:
You can add the $
symbol to enable exact matching, which will not automatically match sub-paths.
It will match as follows:
You can use alias
to resolve an npm package to a specific directory.
For example, if multiple versions of the react
are installed in the project, you can alias react
to the version installed in the root node_modules
directory to avoid bundling multiple copies of the React code.
When using alias to handle npm packages, please be aware of whether different major versions of the package are being used in the project.
For example, if a module or npm dependency in your project uses the React 19 API, and you alias React to version 17, the module will not be able to reference the React 19 API, resulting in code exceptions.
resolve.alias
does not support creating aliases for loaders.
To create aliases for loaders, use Rspack's resolveLoader configuration.