Skip to content

Commit 3c6254c

Browse files
authored
docs: update TypeScript config file guide (#11495)
1 parent 7fe7b69 commit 3c6254c

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

website/docs/en/config/index.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,17 @@ See [Extending Configurations](./extends.mdx) for details on how to extend confi
7272

7373
## TypeScript configuration file
7474

75-
When using `rspack.config.ts`, you need to use a runtime that supports TypeScript, or install additional dependencies to resolve TypeScript files. You can choose one of the following:
75+
When using `rspack.config.ts`, you can choose from one of the following approaches:
76+
77+
### Default behavior
78+
79+
Starting from v1.5.0, Rspack CLI has built-in support for TypeScript configuration, using SWC by default to transform TypeScript code into CommonJS format JavaScript code.
80+
81+
If you're using a Rspack CLI earlier than v1.5.0, it's recommended to upgrade to the latest version. You no longer need to load configuration files through `ts-node` or `esbuild-register`, and these dependencies can be removed.
7682

7783
### Native support
7884

79-
If your JavaScript runtime already natively supports TypeScript, you can use the built-in TS transformation to load the configuration file without needing to install additional dependencies.
85+
If your JavaScript runtime already natively supports TypeScript, you can use the built-in TS transformation to load the configuration file, ensuring that module resolution behavior is consistent with native behavior.
8086

8187
For example, Node.js already natively supports TypeScript, you can use the following command to use the Node.js native loader to load the configuration file:
8288

website/docs/zh/config/index.mdx

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,17 @@ Rspack 支持以下配置文件格式:
7272

7373
## TypeScript 配置文件
7474

75-
在使用 `rspack.config.ts` 时,你需要使用支持 TypeScript 的运行时,或者安装额外的依赖来解析 TypeScript 文件,你可以选择以下任意一种:
75+
在使用 `rspack.config.ts` 时,你可以选择以下任意一种方式:
76+
77+
### 默认行为
78+
79+
Rspack CLI 从 1.5.0 版本开始内置对 TypeScript 配置的支持,默认使用 SWC 来将 TypeScript 代码转换为 CommonJS 格式的 JavaScript 代码。
80+
81+
如果你使用的 Rspack CLI 版本小于 1.5.0,建议升级到最新版本,无需再通过 `ts-node``esbuild-register` 加载配置文件,这些依赖可以被移除。
7682

7783
### 原生支持
7884

79-
如果你使用的 JavaScript 运行时已经原生支持 TypeScript,你可以使用内置的 TS 转换来加载配置文件,而无需安装额外的依赖
85+
如果你使用的 JavaScript 运行时已经原生支持 TypeScript,你可以使用内置的 TS 转换来加载配置文件,确保模块解析行为与原生行为一致
8086

8187
例如,Node.js 已经原生支持 TypeScript,你可以使用以下命令来使用 Node.js 原生加载器来加载配置文件:
8288

@@ -92,34 +98,6 @@ Rspack 支持以下配置文件格式:
9298

9399
详见 [Node.js - Running TypeScript Natively](https://nodejs.org/en/learn/typescript/run-natively#running-typescript-natively)
94100

95-
### 使用 esbuild
96-
97-
对于低于 Node.js v22.7.0 的版本,你可以使用 `esbuild-register` 来加载配置文件。
98-
99-
安装 [esbuild](https://npmjs.com/package/esbuild)[esbuild-register](https://npmjs.com/package/esbuild-register) 即可,不需要任何配置。
100-
101-
<PackageManagerTabs command="add esbuild esbuild-register -D" />
102-
103-
### 使用 ts-node
104-
105-
你也可以使用 [ts-node](https://npmjs.com/package/ts-node) 来加载配置文件。
106-
107-
1. 安装 `ts-node`
108-
109-
<PackageManagerTabs command="add ts-node -D" />
110-
111-
2. 然后在 `tsconfig.json` 中配置 `ts-node` 使用 `CommonJS` 模块:
112-
113-
```json title="tsconfig.json"
114-
{
115-
"ts-node": {
116-
"compilerOptions": {
117-
"module": "CommonJS"
118-
}
119-
}
120-
}
121-
```
122-
123101
### 配置类型检查
124102

125103
使用 `defineConfig` 工具函数来启用智能补全。对于 JavaScript 配置文件,你可以使用 `// @ts-check` 注释来启用类型检查。

0 commit comments

Comments
 (0)