Skip to content

Commit

Permalink
docs: add use case for extensionAlias (#7951)
Browse files Browse the repository at this point in the history
docs: add usecase for `extensionAlias`
  • Loading branch information
chenjiahan authored Sep 23, 2024
1 parent 53ba44f commit 74fec9c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
11 changes: 8 additions & 3 deletions website/docs/en/config/resolve.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ Parse modules in order, e.g. `require('. /index')`, will try to parse `'. /index

Define alias for the extension. e.g.

```js
// rspack.config.js
```js title="rspack.config.js"
module.exports = {
resolve: {
extensionAlias: {
Expand All @@ -98,7 +97,13 @@ module.exports = {
};
```

`require('./index.js')` will try to parse `'./index.ts'`, `./index.js`.
This is particularly useful for TypeScript projects, as TypeScript recommends using the `.js` extension to reference TypeScript files.

```ts title="index.ts"
import { foo } from './foo.js'; // actually refers to `foo.ts`
```

Rspack will try to resolve `'./foo.ts'` and `./foo.js'` sequentially when resolving `import './foo.js'`.

## resolve.fallback

Expand Down
13 changes: 9 additions & 4 deletions website/docs/zh/config/resolve.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ module.exports = {
- **类型:** `Record<string, string[] | string>`
- **默认值:** `{}`

定义拓展名的别名,例如
定义拓展名的别名,例如

```js
// rspack.config.js
```js title="rspack.config.js"
module.exports = {
resolve: {
extensionAlias: {
Expand All @@ -98,7 +97,13 @@ module.exports = {
};
```

`require('./index.js')` 时,会依次尝试解析 `'./index.ts'`, `./index.js`
这对于 TypeScript 项目来说非常有用,因为 TypeScript 推荐使用 `.js` 扩展名来引用 TypeScript 文件。

```ts title="index.ts"
import { foo } from './foo.js'; // 实际引用的是 foo.ts
```

Rspack 在解析 `import './foo.js'` 时,会依次尝试解析 `'./foo.ts'``./foo.js`

## resolve.fallback

Expand Down

0 comments on commit 74fec9c

Please sign in to comment.