Skip to content

Commit 523f324

Browse files
authored
docs: alias differences with Rspack (#4663)
1 parent 143574a commit 523f324

File tree

3 files changed

+74
-36
lines changed

3 files changed

+74
-36
lines changed

packages/core/src/types/config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1529,8 +1529,8 @@ export interface ResolveConfig {
15291529
*/
15301530
dedupe?: string[];
15311531
/**
1532-
* Create aliases to import or require certain modules,
1533-
* same as the [resolve.alias](https://rspack.dev/config/resolve) config of Rspack.
1532+
* Set the alias for the module path, which is used to simplify the import path or redirect the module reference.
1533+
* Similar to the [resolve.alias](https://rspack.dev/config/resolve) config of Rspack.
15341534
*/
15351535
alias?: ConfigChain<Alias>;
15361536
/**

website/docs/en/config/resolve/alias.mdx

+36-17
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ const defaultAlias = {
1616

1717
- **Version:** `>=1.1.7`
1818

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.
2020

2121
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).
2222

2323
:::tip
2424
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.
2525
:::
2626

27-
## Object type
27+
## Basic usage
2828

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.
3030

31-
```js
31+
```ts title="rsbuild.config.ts"
3232
export default {
3333
resolve: {
3434
alias: {
@@ -38,13 +38,13 @@ export default {
3838
};
3939
```
4040

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`.
4242

43-
## Function type
43+
## Function usage
4444

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.
4646

47-
```js
47+
```ts title="rsbuild.config.ts"
4848
export default {
4949
resolve: {
5050
alias: (alias) => {
@@ -56,7 +56,7 @@ export default {
5656

5757
To remove the built-in `@swc/helpers` alias, delete it in the function:
5858

59-
```js
59+
```ts title="rsbuild.config.ts"
6060
export default {
6161
resolve: {
6262
alias: (alias) => {
@@ -68,7 +68,7 @@ export default {
6868

6969
You can also return a new object as the final result in the function, which will replace the preset alias object.
7070

71-
```js
71+
```ts title="rsbuild.config.ts"
7272
export default {
7373
resolve: {
7474
alias: (alias) => {
@@ -80,13 +80,32 @@ export default {
8080
};
8181
```
8282

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+
export default {
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+
83102
## Set by environment
84103

85104
When you build for multiple [environments](/config/environments), you can set different alias for each environment:
86105

87106
For example, set different alias for `web` and `node` environments:
88107

89-
```js
108+
```ts title="rsbuild.config.ts"
90109
export default {
91110
environments: {
92111
web: {
@@ -117,7 +136,7 @@ export default {
117136

118137
By default, `resolve.alias` will automatically match sub-paths, for example, with the following configuration:
119138

120-
```js
139+
```ts title="rsbuild.config.ts"
121140
import path from 'node:path';
122141

123142
export default {
@@ -131,14 +150,14 @@ export default {
131150

132151
It will match as follows:
133152

134-
```js
153+
```ts
135154
import a from '@common'; // resolved to `./src/common`
136155
import b from '@common/util'; // resolved to `./src/common/util`
137156
```
138157

139158
You can add the `$` symbol to enable exact matching, which will not automatically match sub-paths.
140159

141-
```js
160+
```ts title="rsbuild.config.ts"
142161
import path from 'node:path';
143162

144163
export default {
@@ -152,7 +171,7 @@ export default {
152171

153172
It will match as follows:
154173

155-
```js
174+
```ts
156175
import a from '@common'; // resolved to `./src/common`
157176
import b from '@common/util'; // remains as `@common/util`
158177
```
@@ -163,7 +182,7 @@ You can use `alias` to resolve an npm package to a specific directory.
163182

164183
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.
165184

166-
```js
185+
```ts title="rsbuild.config.ts"
167186
import path from 'node:path';
168187

169188
export default {
@@ -185,7 +204,7 @@ For example, if a module or npm dependency in your project uses the React 19 API
185204

186205
To create aliases for loaders, use Rspack's [resolveLoader](https://rspack.dev/config/resolve-loader) configuration.
187206

188-
```ts
207+
```ts title="rsbuild.config.ts"
189208
export default {
190209
tools: {
191210
rspack: {

website/docs/zh/config/resolve/alias.mdx

+36-17
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ const defaultAlias = {
1616

1717
- **版本:** `>=1.1.7`
1818

19-
设置文件引用的别名,对应 Rspack 的 [resolve.alias](https://rspack.dev/zh/config/resolve#resolvealias) 配置
19+
设置模块路径的别名,用于简化导入路径或重定向模块引用
2020

2121
对于 TypeScript 项目,你只需要在 `tsconfig.json` 中配置 [compilerOptions.paths](https://typescriptlang.org/tsconfig#paths) 即可,Rsbuild 会自动识别它,不需要额外配置 `resolve.alias` 字段,详见 [「路径别名」](/guide/advanced/alias)
2222

2323
:::tip
2424
在 Rsbuild 1.1.7 之前的版本,你可以使用 `source.alias` 来设置 alias,但该字段将在下一个大版本中被移除。
2525
:::
2626

27-
## Object 类型
27+
## 基本用法
2828

29-
`alias` 的值可以定义为 Object 类型,其中的相对路径会自动被 Rsbuild 转换为绝对路径
29+
`resolve.alias` 的值可以传入一个对象,对象的 `key` 是源代码中需要被替换的模块路径,`value` 是模块实际映射到的目标路径
3030

31-
```js
31+
```ts title="rsbuild.config.ts"
3232
export default {
3333
resolve: {
3434
alias: {
@@ -38,13 +38,13 @@ export default {
3838
};
3939
```
4040

41-
以上配置完成后,如果你在代码中引用 `@common/Foo.tsx`, 则会映射到 `<project>/src/common/Foo.tsx` 路径上。
41+
添加以上配置后,当你在代码中引用 `@common/Foo.tsx` 时,会映射到 `<project-root>/src/common/Foo.tsx` 路径上。
4242

43-
## Function 类型
43+
## Function 用法
4444

45-
`alias` 的值定义为函数时,可以接受预设的 alias 对象,并对其进行修改。
45+
`resolve.alias` 的值定义为函数时,可以接受预设的 alias 对象,并对其进行修改。
4646

47-
```js
47+
```ts title="rsbuild.config.ts"
4848
export default {
4949
resolve: {
5050
alias: (alias) => {
@@ -56,7 +56,7 @@ export default {
5656

5757
如果你需要移除 Rsbuild 内置的 `@swc/helpers` 别名,可以在函数中删除它:
5858

59-
```js
59+
```ts title="rsbuild.config.ts"
6060
export default {
6161
resolve: {
6262
alias: (alias) => {
@@ -68,7 +68,7 @@ export default {
6868

6969
你也可以在函数中返回一个新对象作为最终结果,新对象会覆盖预设的 alias 对象。
7070

71-
```js
71+
```ts title="rsbuild.config.ts"
7272
export default {
7373
resolve: {
7474
alias: (alias) => {
@@ -80,13 +80,32 @@ export default {
8080
};
8181
```
8282

83+
## 与 Rspack 配置的差异
84+
85+
Rsbuild 的 `resolve.alias` 与 Rspack 的 [resolve.alias](https://rspack.dev/zh/config/resolve#resolvealias) 配置的用法基本一致,它们的差异是:
86+
87+
- 如果 `resolve.alias` 的值是一个相对路径,Rsbuild 会自动将其转换为绝对路径,以保证路径是相对于项目根目录的。
88+
89+
```ts title="rsbuild.config.ts"
90+
export default {
91+
resolve: {
92+
alias: {
93+
// 将被转换为 `<project-root>/src/common`
94+
'@common': './src/common',
95+
},
96+
},
97+
};
98+
```
99+
100+
- Rsbuild 额外支持了函数类型的用法。
101+
83102
## 基于 environment 设置
84103

85104
当你面向多个 [environments](/config/environments) 构建时,可以为每个 environment 设置不同的 alias:
86105

87106
比如为 `web``node` 环境设置不同的 alias:
88107

89-
```js
108+
```ts title="rsbuild.config.ts"
90109
export default {
91110
environments: {
92111
web: {
@@ -117,7 +136,7 @@ export default {
117136

118137
默认情况,`resolve.alias` 会自动匹配子路径,比如以下配置:
119138

120-
```js
139+
```ts title="rsbuild.config.ts"
121140
import path from 'node:path';
122141

123142
export default {
@@ -131,14 +150,14 @@ export default {
131150

132151
它的匹配结果如下:
133152

134-
```js
153+
```ts
135154
import a from '@common'; // 解析为 `./src/common`
136155
import b from '@common/util'; // 解析为 `./src/common/util`
137156
```
138157

139158
你可以添加 `$` 符号来开启精确匹配,开启后将不会自动匹配子路径。
140159

141-
```js
160+
```ts title="rsbuild.config.ts"
142161
import path from 'node:path';
143162

144163
export default {
@@ -152,7 +171,7 @@ export default {
152171

153172
它的匹配结果如下:
154173

155-
```js
174+
```ts
156175
import a from '@common'; // 解析为 `./src/common`
157176
import b from '@common/util'; // 保持 `@common/util` 不变
158177
```
@@ -163,7 +182,7 @@ import b from '@common/util'; // 保持 `@common/util` 不变
163182

164183
比如项目中安装了多份 `react`,你可以将 `react` 统一指向根目录的 `node_modules` 中安装的版本,避免出现打包多份 React 代码的问题。
165184

166-
```js
185+
```ts title="rsbuild.config.ts"
167186
import path from 'node:path';
168187

169188
export default {
@@ -183,7 +202,7 @@ export default {
183202

184203
`resolve.alias` 不支持为 loader 设置别名。如果你需要为 loader 设置别名,可以使用 Rspack 的 [resolveLoader](https://rspack.dev/zh/config/resolve-loader) 配置项。
185204

186-
```ts
205+
```ts title="rsbuild.config.ts"
187206
export default {
188207
tools: {
189208
rspack: {

0 commit comments

Comments
 (0)