You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: website/docs/en/config/resolve/alias.mdx
+36-17
Original file line number
Diff line number
Diff line change
@@ -16,19 +16,19 @@ const defaultAlias = {
16
16
17
17
-**Version:**`>=1.1.7`
18
18
19
-
Create aliases to import or require certain modules, same as the [resolve.alias](https://rspack.dev/config/resolve#resolvealias) config of Rspack.
19
+
Set the alias for the module path, which is used to simplify the import path or redirect the module reference.
20
20
21
21
For TypeScript projects, you only need to configure [compilerOptions.paths](https://typescriptlang.org/tsconfig#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](/guide/advanced/alias).
22
22
23
23
:::tip
24
24
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.
25
25
:::
26
26
27
-
## Object type
27
+
## Basic usage
28
28
29
-
The `alias` can be an Object, and the relative path will be automatically converted to absolute path.
29
+
`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.
30
30
31
-
```js
31
+
```ts title="rsbuild.config.ts"
32
32
exportdefault {
33
33
resolve: {
34
34
alias: {
@@ -38,13 +38,13 @@ export default {
38
38
};
39
39
```
40
40
41
-
With above configuration, if `@common/Foo.tsx` is import in the code, it will be mapped to the `<project>/src/common/Foo.tsx` path.
41
+
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`.
42
42
43
-
## Function type
43
+
## Function usage
44
44
45
-
The `alias` can be a function, it will accept the previous alias object, and you can modify it.
45
+
The `resolve.alias` can be a function, it will accept the previous alias object, and you can modify it.
46
46
47
-
```js
47
+
```ts title="rsbuild.config.ts"
48
48
exportdefault {
49
49
resolve: {
50
50
alias: (alias) => {
@@ -56,7 +56,7 @@ export default {
56
56
57
57
To remove the built-in `@swc/helpers` alias, delete it in the function:
58
58
59
-
```js
59
+
```ts title="rsbuild.config.ts"
60
60
exportdefault {
61
61
resolve: {
62
62
alias: (alias) => {
@@ -68,7 +68,7 @@ export default {
68
68
69
69
You can also return a new object as the final result in the function, which will replace the preset alias object.
70
70
71
-
```js
71
+
```ts title="rsbuild.config.ts"
72
72
exportdefault {
73
73
resolve: {
74
74
alias: (alias) => {
@@ -80,13 +80,32 @@ export default {
80
80
};
81
81
```
82
82
83
+
## Differences with Rspack
84
+
85
+
Rsbuild's `resolve.alias` is similar to Rspack's [resolve.alias](https://rspack.dev/config/resolve#resolvealias) configuration, but there are some differences:
86
+
87
+
- If the value of `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.
88
+
89
+
```ts title="rsbuild.config.ts"
90
+
exportdefault {
91
+
resolve: {
92
+
alias: {
93
+
// Will be converted to `<project-root>/src/common`
94
+
'@common': './src/common',
95
+
},
96
+
},
97
+
};
98
+
```
99
+
100
+
- Rsbuild additionally supports the function type.
101
+
83
102
## Set by environment
84
103
85
104
When you build for multiple [environments](/config/environments), you can set different alias for each environment:
86
105
87
106
For example, set different alias for `web` and `node` environments:
88
107
89
-
```js
108
+
```ts title="rsbuild.config.ts"
90
109
exportdefault {
91
110
environments: {
92
111
web: {
@@ -117,7 +136,7 @@ export default {
117
136
118
137
By default, `resolve.alias` will automatically match sub-paths, for example, with the following configuration:
119
138
120
-
```js
139
+
```ts title="rsbuild.config.ts"
121
140
importpathfrom'node:path';
122
141
123
142
exportdefault {
@@ -131,14 +150,14 @@ export default {
131
150
132
151
It will match as follows:
133
152
134
-
```js
153
+
```ts
135
154
importafrom'@common'; // resolved to `./src/common`
136
155
importbfrom'@common/util'; // resolved to `./src/common/util`
137
156
```
138
157
139
158
You can add the `$` symbol to enable exact matching, which will not automatically match sub-paths.
140
159
141
-
```js
160
+
```ts title="rsbuild.config.ts"
142
161
importpathfrom'node:path';
143
162
144
163
exportdefault {
@@ -152,7 +171,7 @@ export default {
152
171
153
172
It will match as follows:
154
173
155
-
```js
174
+
```ts
156
175
importafrom'@common'; // resolved to `./src/common`
157
176
importbfrom'@common/util'; // remains as `@common/util`
158
177
```
@@ -163,7 +182,7 @@ You can use `alias` to resolve an npm package to a specific directory.
163
182
164
183
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.
165
184
166
-
```js
185
+
```ts title="rsbuild.config.ts"
167
186
importpathfrom'node:path';
168
187
169
188
exportdefault {
@@ -185,7 +204,7 @@ For example, if a module or npm dependency in your project uses the React 19 API
185
204
186
205
To create aliases for loaders, use Rspack's [resolveLoader](https://rspack.dev/config/resolve-loader) configuration.
0 commit comments