Skip to content

Commit 6f24f88

Browse files
authored
docs: document package.json imports resolution (#13686)
* docs: document package.json imports resolution * docs: fix
1 parent 5f6b5b2 commit 6f24f88

4 files changed

Lines changed: 61 additions & 5 deletions

File tree

website/docs/en/config/resolve.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,9 @@ e.g.
463463
}
464464
```
465465
466-
When this configuration is ["testImports", "imports"], the result of `import value from '#foo'` in current package is `src/test/foo.js`.
466+
When this configuration is `["testImports", "imports"]`, the result of `import value from '#foo'` in current package is `src/test/foo.js`.
467+
468+
> See [Module resolution - package.json `imports`](/guide/features/module-resolution#packagejson-imports) for more details.
467469
468470
## resolve.mainFields
469471

website/docs/en/guide/features/module-resolution.mdx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ description: "Module resolution is the process of converting a module identifier
44

55
# Module resolution
66

7-
Module resolution is the process of converting a module identifier to a module's file path. The Rust port of enhanced-resolve is used for module path resolution, which is an extension to the [node module resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together) with the same interface as [enhanced-resolve](https://github.com/webpack/enhanced-resolve), refer to [resolve configuration](/config/resolve) for more information about module resolution configuration.
7+
Module resolution is the process of converting a module identifier to a module's file path.
8+
9+
Rspack uses [rspack-resolver](https://github.com/rstackjs/rspack-resolver) for module path resolution, which is an extension to the [Node.js module resolution algorithm](https://nodejs.org/api/esm.html#resolution-algorithm-specification) with the same interface as [enhanced-resolve](https://github.com/webpack/enhanced-resolve).
10+
11+
Refer to [resolve configuration](/config/resolve) for more information about module resolution configuration.
812

913
## Rspack
1014

@@ -33,3 +37,25 @@ import 'lodash';
3337
```
3438

3539
Module paths are those that do not start with `'./'`, `'../'`, `'/'`. In this case, Rspack will resolve the absolute path of the module according to the module resolution rules. The [node module resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together) has a detailed description of the rules for resolve modules.
40+
41+
## package.json `imports`
42+
43+
Rspack's resolver supports the [imports](https://nodejs.org/api/packages.html#subpath-imports) field in `package.json` for resolving module identifiers that start with `#` within a package.
44+
45+
In the following example, both `#utils` and `#/main` are resolved from the current package's `imports` field:
46+
47+
```json title="package.json"
48+
{
49+
"imports": {
50+
"#utils": "./src/utils/index.js",
51+
"#/*": "./src/*"
52+
}
53+
}
54+
```
55+
56+
```js title="src/index.js"
57+
import utils from '#utils'; // resolves to ./src/utils/index.js
58+
import main from '#/main.ts'; // resolves to ./src/main.ts
59+
```
60+
61+
> Use the [`resolve.importsFields`](/config/resolve#resolveimportsfields) option to customize the field name used for `imports`.

website/docs/zh/config/resolve.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ export default {
446446
- **类型:** `string[]`
447447
- **默认值:** `['imports']`
448448
449-
自定义 package.json 中的 imports 字段,用于提供包的内部请求(以 `#` 开头的请求被视为内部请求)
449+
自定义 `package.json` 中的 `imports` 字段,用于提供包的内部请求(以 `#` 开头的请求被视为内部请求)
450450
451451
例如:
452452
@@ -463,7 +463,9 @@ export default {
463463
}
464464
```
465465
466-
则当配置为 ["testImports", "imports"] 时, 当前包内 `import value from '#foo'` 的结果为 `src/test/foo.js`
466+
则当配置为 `["testImports", "imports"]` 时, 当前包内 `import value from '#foo'` 的结果为 `src/test/foo.js`
467+
468+
> 查看 [模块解析 - package.json `imports`](/guide/features/module-resolution#packagejson-imports) 了解更多。
467469
468470
## resolve.mainFields
469471

website/docs/zh/guide/features/module-resolution.mdx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ description: '介绍 Rspack 的模块解析过程和基于 enhanced-resolve 的
44

55
# 模块解析
66

7-
模块解析指的是将模块标识符转换为模块的文件路径的过程。Rspack 使用 Rust 版本的 enhanced-resolve 来进行模块路径解析,它是对 [node 模块解析算法](https://nodejs.org/api/modules.html#modules_all_together)的一个扩展,其接口和 [enhanced-resolve](https://github.com/webpack/enhanced-resolve) 保持一致,请参考 [resolve 配置](/config/resolve) 了解更多关于模块解析的配置。
7+
模块解析指的是将模块标识符转换为模块的文件路径的过程。
8+
9+
Rspack 使用 [rspack-resolver](https://github.com/rstackjs/rspack-resolver) 来进行模块路径解析,它是对 [Node.js 模块解析算法](https://nodejs.org/api/esm.html#resolution-algorithm-specification) 的一个扩展,其接口和 [enhanced-resolve](https://github.com/webpack/enhanced-resolve) 保持一致。
10+
11+
参考 [resolve 配置](/config/resolve) 了解更多关于模块解析的配置。
812

913
## Rspack 中的解析规则
1014

@@ -33,3 +37,25 @@ import 'lodash';
3337
```
3438

3539
模块路径指那些没有以 `'./'``'../'``'/'` 开头的路径。在这种情况下,Rspack 会根据模块解析规则,来解析模块的绝对路径。[node 模块解析算法](https://nodejs.org/api/modules.html#modules_all_together)有关于解析模块规则的详细说明。
40+
41+
## package.json `imports`
42+
43+
Rspack 的解析器支持 `package.json`[imports](https://nodejs.org/api/packages.html#subpath-imports) 字段,用于解析包内以 `#` 开头的模块标识符。
44+
45+
比如下面的例子,`#utils``#/main` 都会从当前包的 `imports` 字段中解析:
46+
47+
```json title="package.json"
48+
{
49+
"imports": {
50+
"#utils": "./src/utils/index.js",
51+
"#/*": "./src/*"
52+
}
53+
}
54+
```
55+
56+
```js title="src/index.js"
57+
import utils from '#utils'; // 解析为 ./src/utils/index.js
58+
import main from '#/main.ts'; // 解析为 ./src/main.ts
59+
```
60+
61+
> 使用 [`resolve.importsFields`](/config/resolve#resolveimportsfields) 选项可以自定义 `imports` 字段的名称。

0 commit comments

Comments
 (0)