Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add use case for extensionAlias #7951

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading