Skip to content

Commit 17e80be

Browse files
authored
docs: explain TypeScript extension alias (#3952)
1 parent 56d745a commit 17e80be

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

website/docs/en/guide/basic/typescript.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,30 @@ export default {
5959
},
6060
};
6161
```
62+
63+
## Path Extensions
64+
65+
When importing another module in a TypeScript module, TypeScript allows using the `.js` file extension:
66+
67+
```ts title="src/index.ts"
68+
// The actual referenced module could be `./some-module.ts` or `./some-module.tsx`
69+
import { someFn } from './some-module.js';
70+
```
71+
72+
Rsbuild supports this feature through Rspack's [extensionAlias](https://rspack.dev/config/resolve#resolveextensionalias) configuration. In TypeScript projects, Rsbuild adds the following configuration by default:
73+
74+
```js
75+
const rspackConfig = {
76+
resolve: {
77+
extensionAlias: {
78+
'.js': ['.ts', '.tsx', '.js'],
79+
'.jsx': ['.tsx', '.jsx'],
80+
},
81+
},
82+
};
83+
```
84+
85+
This means:
86+
87+
- You can use the `.js` extension to import `.ts` or `.tsx` files.
88+
- You can use the `.jsx` extension to import `.tsx` files.

website/docs/zh/guide/basic/typescript.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,30 @@ export default {
5959
},
6060
};
6161
```
62+
63+
## 路径后缀
64+
65+
当在一个 TypeScript 模块中导入另一个模块时,TypeScript 允许使用 `.js` 文件扩展名:
66+
67+
```ts title="src/index.ts"
68+
// 实际引用的模块可能是 `./some-module.ts` 或 `./some-module.tsx`
69+
import { someFn } from './some-module.js';
70+
```
71+
72+
Rsbuild 通过 Rspack 的 [extensionAlias](https://rspack.dev/config/resolve#resolveextensionalias) 配置来支持该特性。在 TypeScript 项目中,Rsbuild 默认会添加以下配置:
73+
74+
```js
75+
const rspackConfig = {
76+
resolve: {
77+
extensionAlias: {
78+
'.js': ['.ts', '.tsx', '.js'],
79+
'.jsx': ['.tsx', '.jsx'],
80+
},
81+
},
82+
};
83+
```
84+
85+
这意味着:
86+
87+
- 允许使用 `.js` 文件扩展名导入 `.ts``.tsx` 文件。
88+
- 允许使用 `.jsx` 文件扩展名导入 `.tsx` 文件。

0 commit comments

Comments
 (0)