From 54996dd29643abe91a8037dfa6ee6bca2e81e259 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 13 Dec 2024 17:49:01 +0800 Subject: [PATCH 1/4] docs: improve `resolve.mainFields` --- website/docs/en/config/resolve.mdx | 55 +++++++++++++++++++++++------- website/docs/zh/config/resolve.mdx | 55 +++++++++++++++++++++++------- 2 files changed, 84 insertions(+), 26 deletions(-) diff --git a/website/docs/en/config/resolve.mdx b/website/docs/en/config/resolve.mdx index 5f2575ab6bc..1bc388038a1 100644 --- a/website/docs/en/config/resolve.mdx +++ b/website/docs/en/config/resolve.mdx @@ -181,16 +181,15 @@ Customize the `imports` field in package.json which are used to provide the inte e.g. -``` -// package.json +```json title="package.json" { "name": "lib", "imports": { "#foo": "./src/foo.js", "#common/*": "./src/common/*.js" - } + }, "testImports": { - "#foo": "./src/test/foo.js", + "#foo": "./src/test/foo.js" } } ``` @@ -200,21 +199,41 @@ When this configuration is ["testImports", "imports"], the result of `import val ## resolve.mainFields - **Type:** `string[]` -- **Default:** - - `target` is `["browser", "module", "main"]` when it is web - - `["module", "main"]` for others +- **Default:** Based on the [target](/config/target) option -Try to parse the fields in package.json, e.g. +When importing from an npm package, for example `import React from 'react'`, `resolve.mainFields` is used to determine which fields in `package.json` are resolved. -```json -// package.json +If `target` is `'web'`, `'webworker'`, or not specified, the default value is `["browser", "module", "main"]`. + +```js title="rspack.config.js" +module.exports = { + resolve: { + mainFields: ['browser', 'module', 'main'], + }, +}; +``` + +For any other `target` (including `'node'`), the default value is `["module", "main"]`. + +```js title="rspack.config.js" +module.exports = { + resolve: { + mainFields: ['module', 'main'], + }, +}; +``` + +For example, consider an arbitrary library called `foo` with a `package.json` that contains the following fields: + +```json title="package.json" { - "name": "lib", - "module": "es/index.js" + "name": "foo", + "browser": "./dist/browser.js", + "module": "./dist/module.js" } ``` -then `import value from 'lib'` results in `lib/es/index.js`. +When `import foo from 'foo'`, Rspack resolves to the module in the `browser` field, because the `browser` field has the highest priority in `mainFields` array. ## resolve.mainFiles @@ -223,6 +242,16 @@ then `import value from 'lib'` results in `lib/es/index.js`. The filename suffix when resolving directories, e.g. `require('. /dir/')` will try to resolve `'. /dir/index'`. +Can configure multiple filename suffixes: + +```js title="rspack.config.js" +module.exports = { + resolve: { + mainFiles: ['index', 'main'], + }, +}; +``` + ## resolve.exportsFields - **Type:** `string[]` diff --git a/website/docs/zh/config/resolve.mdx b/website/docs/zh/config/resolve.mdx index ceb1d76e882..faeeaba9101 100644 --- a/website/docs/zh/config/resolve.mdx +++ b/website/docs/zh/config/resolve.mdx @@ -175,21 +175,41 @@ module.exports = { ## resolve.mainFields - **类型:** `string[]` -- **默认值:** - - `target` 为 web 时为 `["browser", "module", "main"]` - - 其他则为 `["module", "main"]` +- **默认值:** 根据 [target](/config/target) 选项确定 -尝试解析 package.json 中的字段,例如: +当从 npm 包导入时,例如 `import React from 'react'`,`resolve.mainFields` 用于确定其 `package.json` 中的哪些字段会被解析。 -```json -// package.json +当 `target` 配置为 `'web'`, `'webworker'`, 或未指定时,默认值为 `["browser", "module", "main"]`。 + +```js title="rspack.config.js" +module.exports = { + resolve: { + mainFields: ['browser', 'module', 'main'], + }, +}; +``` + +对于任何其他 `target`(包括 `'node'`), 默认值为 `["module", "main"]`。 + +```js title="rspack.config.js" +module.exports = { + resolve: { + mainFields: ['module', 'main'], + }, +}; +``` + +例如,对于一个名为 `foo` 的库,它的 `package.json` 包含以下字段: + +```json title="package.json" { - "name": "lib", - "module": "es/index.js" + "name": "foo", + "browser": "./dist/browser.js", + "module": "./dist/module.js" } ``` -则 `import value from 'lib'` 的结果为 `lib/es/index.js`。 +当 `import foo from 'foo'` 时,Rspack 会解析到 `browser` 字段中的模块,因为 `browser` 字段在 `mainFields` 数组中优先级最高。 ## resolve.mainFiles @@ -198,6 +218,16 @@ module.exports = { 解析目录时的文件名后缀,例如 `require('./dir/')` 会尝试解析 `'./dir/index'`。 +可以配置多个文件名后缀: + +```js title="rspack.config.js" +module.exports = { + resolve: { + mainFiles: ['index', 'main'], + }, +}; +``` + ## resolve.exportsFields - **类型:** `string[]` @@ -229,16 +259,15 @@ module.exports = { 例如: -``` -// package.json +```json title="package.json" { "name": "lib", "imports": { "#foo": "./src/foo.js", "#common/*": "./src/common/*.js" - } + }, "testImports": { - "#foo": "./src/test/foo.js", + "#foo": "./src/test/foo.js" } } ``` From dfbb01ed94cbe16bc5e07c06204abaa3d700a316 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 13 Dec 2024 17:52:38 +0800 Subject: [PATCH 2/4] docs: fix order --- website/docs/zh/config/resolve.mdx | 48 +++++++++++++++--------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/website/docs/zh/config/resolve.mdx b/website/docs/zh/config/resolve.mdx index faeeaba9101..6d86ea77660 100644 --- a/website/docs/zh/config/resolve.mdx +++ b/website/docs/zh/config/resolve.mdx @@ -172,6 +172,30 @@ module.exports = { }; ``` +## resolve.importsFields + +- **类型:** `string[]` +- **默认值:** `['imports']` + +自定义 package.json 中的 imports 字段,用于提供包的内部请求(以 `#` 开头的请求被视为内部请求) + +例如: + +```json title="package.json" +{ + "name": "lib", + "imports": { + "#foo": "./src/foo.js", + "#common/*": "./src/common/*.js" + }, + "testImports": { + "#foo": "./src/test/foo.js" + } +} +``` + +则当配置为 ["testImports", "imports"] 时, 当前包内 `import value from '#foo'` 的结果为 `src/test/foo.js`。 + ## resolve.mainFields - **类型:** `string[]` @@ -250,30 +274,6 @@ module.exports = { 则当配置为 `["testExports", "exports"]` 时, `import value from 'lib'` 的结果为 `lib/test.js`。 -## resolve.importsFields - -- **类型:** `string[]` -- **默认值:** `['imports']` - -自定义 package.json 中的 imports 字段,用于提供包的内部请求(以 `#` 开头的请求被视为内部请求) - -例如: - -```json title="package.json" -{ - "name": "lib", - "imports": { - "#foo": "./src/foo.js", - "#common/*": "./src/common/*.js" - }, - "testImports": { - "#foo": "./src/test/foo.js" - } -} -``` - -则当配置为 ["testImports", "imports"] 时, 当前包内 `import value from '#foo'` 的结果为 `src/test/foo.js`。 - ## resolve.modules - **类型:** `string[]` From 49260a4f6e8cd6d77912bee62451d81f0126155e Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 13 Dec 2024 17:53:33 +0800 Subject: [PATCH 3/4] docs: fix order --- website/docs/en/config/resolve.mdx | 44 +++++++++++++++--------------- website/docs/zh/config/resolve.mdx | 44 +++++++++++++++--------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/website/docs/en/config/resolve.mdx b/website/docs/en/config/resolve.mdx index 1bc388038a1..e793e1ca29a 100644 --- a/website/docs/en/config/resolve.mdx +++ b/website/docs/en/config/resolve.mdx @@ -73,6 +73,28 @@ module.exports = { }; ``` +## resolve.exportsFields + +- **Type:** `string[]` +- **Default:** `["exports"]` + +Customize the `exports` field in package.json. e.g. + +```json +// lib/package.json +{ + "name": "lib", + "testExports": { + ".": "./test.js" + }, + "exports": { + ".": "./index.js" + } +} +``` + +When this configuration is `["testExports", "exports"]`, the result of `import value from 'lib'` is `lib/test.js`. + ## resolve.extensions - **Type:** `string[]` @@ -252,28 +274,6 @@ module.exports = { }; ``` -## resolve.exportsFields - -- **Type:** `string[]` -- **Default:** `["exports"]` - -Customize the `exports` field in package.json. e.g. - -```json -// lib/package.json -{ - "name": "lib", - "testExports": { - ".": "./test.js" - }, - "exports": { - ".": "./index.js" - } -} -``` - -When this configuration is `["testExports", "exports"]`, the result of `import value from 'lib'` is `lib/test.js`. - ## resolve.modules - **Type:** `string[]` diff --git a/website/docs/zh/config/resolve.mdx b/website/docs/zh/config/resolve.mdx index 6d86ea77660..1f19c114ad3 100644 --- a/website/docs/zh/config/resolve.mdx +++ b/website/docs/zh/config/resolve.mdx @@ -73,6 +73,28 @@ module.exports = { }; ``` +## resolve.exportsFields + +- **类型:** `string[]` +- **默认值:** `["exports"]` + +自定义 package.json 中的 exports 字段,例如: + +```json +// lib/package.json +{ + "name": "lib", + "testExports": { + ".": "./test.js" + }, + "exports": { + ".": "./index.js" + } +} +``` + +则当配置为 `["testExports", "exports"]` 时, `import value from 'lib'` 的结果为 `lib/test.js`。 + ## resolve.extensions - **类型:** `string[]` @@ -252,28 +274,6 @@ module.exports = { }; ``` -## resolve.exportsFields - -- **类型:** `string[]` -- **默认值:** `["exports"]` - -自定义 package.json 中的 exports 字段,例如: - -```json -// lib/package.json -{ - "name": "lib", - "testExports": { - ".": "./test.js" - }, - "exports": { - ".": "./index.js" - } -} -``` - -则当配置为 `["testExports", "exports"]` 时, `import value from 'lib'` 的结果为 `lib/test.js`。 - ## resolve.modules - **类型:** `string[]` From fa56638d00d3be015605a836a658a4654d5c68c9 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 13 Dec 2024 17:55:43 +0800 Subject: [PATCH 4/4] docs: fix --- website/docs/en/config/resolve.mdx | 3 +-- website/docs/zh/config/resolve.mdx | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/website/docs/en/config/resolve.mdx b/website/docs/en/config/resolve.mdx index e793e1ca29a..b5be4de429d 100644 --- a/website/docs/en/config/resolve.mdx +++ b/website/docs/en/config/resolve.mdx @@ -80,8 +80,7 @@ module.exports = { Customize the `exports` field in package.json. e.g. -```json -// lib/package.json +```json title="lib/package.json" { "name": "lib", "testExports": { diff --git a/website/docs/zh/config/resolve.mdx b/website/docs/zh/config/resolve.mdx index 1f19c114ad3..9cf266757b9 100644 --- a/website/docs/zh/config/resolve.mdx +++ b/website/docs/zh/config/resolve.mdx @@ -80,8 +80,7 @@ module.exports = { 自定义 package.json 中的 exports 字段,例如: -```json -// lib/package.json +```json title="lib/package.json" { "name": "lib", "testExports": {