Skip to content

Commit d84e931

Browse files
authored
docs: polish module methods documentation (#8448)
1 parent 49a9974 commit d84e931

File tree

9 files changed

+51
-41
lines changed

9 files changed

+51
-41
lines changed

website/docs/en/api/cli.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Command Line Interface (CLI)
22

3-
@rspack/cli provides two common subcommands, serve and build, to simplify daily work. If you do not have @rspack/cli installed, please read the [Quick Start](/guide/start/quick-start) section first.
3+
[@rspack/cli](https://npmjs.com/package/@rspack/cli) provides two common subcommands, serve and build, to simplify daily work. If you do not have @rspack/cli installed, please read the [Quick Start](/guide/start/quick-start) section first.
44

55
:::warning Compatible with webpack-cli
66
@rspack/cli is not going to be compatible with webpack-cli, so there will be many differences between the two.

website/docs/en/api/runtime-api/module-methods.mdx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ import { ApiMeta } from '@components/ApiMeta';
55

66
# Module Methods
77

8-
This section covers all methods available in code compiled with Rspack. When using Rspack to bundle your application, you can pick from a variety of module syntax styles including ES6, CommonJS.
8+
This section covers all methods available in code compiled with Rspack. When using Rspack to bundle your application, you can pick from a variety of module syntax styles including [ES modules](https://nodejs.org/api/esm.html#modules-ecmascript-modules) and [CommonJS](https://nodejs.org/api/modules.html).
99

10-
While Rspack supports multiple module syntaxes, we recommend following a single syntax for consistency and to avoid odd behaviors/bugs.
10+
While Rspack supports multiple module syntaxes, we recommend following a single syntax for consistency and to avoid odd behaviors or bugs.
1111

12-
Actually Rspack would enforce the recommendation for `.mjs` files, `.cjs` files or `.js` files when their nearest parent `package.json` file contains a `"type"` field with a value of either `"module"` or `"commonjs"`. Please pay attention to these enforcements before you read on:
12+
Actually Rspack would enforce the recommendation for `.mjs` files, `.cjs` files or `.js` files when their nearest parent `package.json` file contains a ["type"](https://nodejs.org/api/packages.html#type) field with a value of either `"module"` or `"commonjs"`. Please pay attention to these enforcements before you read on:
1313

1414
- `.mjs` or `.js` with `"type": "module"` in package.json
1515
- No CommonJS allowed, for example, you can't use `require`, `module.exports` or `exports`
1616
- File extensions are required when importing, e.g, you should use import './src/App.mjs' instead of import './src/App' (you can disable this enforcement with Rule.resolve.fullySpecified)
1717
- `.cjs` or `.js` with "type": "commonjs" in package.json
1818
- Neither import nor export is available
1919

20-
## ES6 (Recommended)
20+
## ES modules (Recommended)
2121

22-
Rspack support ES6 module syntax natively, you can use static `import`, `export` and `import()` syntax.
22+
Rspack support ES modules syntax natively, you can use static `import`, `export` and `import()` syntax.
2323

2424
:::warning
2525
Keep in mind that you will still probably need SWC or Babel for other ES6+ features.
@@ -67,7 +67,9 @@ export default {
6767
function import(path: string): Promise;
6868
```
6969

70-
Dynamically load modules. Calls to `import()` are treated as split points, meaning the requested module and its children are split out into a separate chunk.
70+
Dynamically load modules, see [Dynamic import](/guide/optimization/code-splitting#dynamic-import) for more details.
71+
72+
Calls to `import()` are treated as split points, meaning the requested module and its children are split out into a separate chunk.
7173

7274
```js
7375
if (module.hot) {
@@ -78,14 +80,15 @@ if (module.hot) {
7880
```
7981

8082
:::warning
81-
This feature relies on `Promise` internally. If you use import() with older browsers, remember to shim `Promise` using a polyfill such as [es6-promise](https://github.com/stefanpenner/es6-promise) or [promise-polyfill](https://github.com/taylorhakes/promise-polyfill).
83+
This feature relies on `Promise` internally. If you use `import()` with legacy browsers, remember to shim `Promise` using a polyfill such as [core-js](https://github.com/zloirock/core-js), [es6-promise](https://github.com/stefanpenner/es6-promise) or [promise-polyfill](https://github.com/taylorhakes/promise-polyfill).
8284
:::
8385

8486
#### Dynamic expressions in import()
8587

8688
It is not possible to use a fully dynamic import statement, such as `import(foo)`. Because `foo` could potentially be any path to any file in your system or project.
8789

88-
The `import()` must contain at least some information about where the module is located. Bundling can be limited to a specific directory or set of files so that when you are using a dynamic expression - every module that could potentially be requested on an `import()` call is included.
90+
The `import()` must contain at least some information about where the module is located. Bundling can be limited to a specific directory or set of files so that when you are using a dynamic expression, every module that could potentially be requested on an `import()` call is included.
91+
8992
For example, `import(`./locale/$\{language}.json`)` will cause every `.json` file in the `./locale` directory to be bundled into the new chunk. At run time, when the variable `language` has been computed, any file like `english.json` or `german.json` will be available for consumption.
9093

9194
```js
@@ -100,7 +103,7 @@ import(`./locale/${language}.json`).then(module => {
100103

101104
<ApiMeta specific={['Rspack', 'Webpack']} />
102105

103-
Inline comments to make features work. By adding comments to the import, we can do things such as name our chunk or select different modes. For a full list of these magic comments see the code below followed by an explanation of what these comments do.
106+
Inline comments to make features work. By adding comments to the import, we can do things such as specify chunk name or select different loading modes. For a full list of these magic comments see the code below followed by an explanation of what these comments do.
104107

105108
```js
106109
// Single target
@@ -128,7 +131,7 @@ import(
128131

129132
- **Type:** `boolean`
130133

131-
Disables dynamic import parsing when set to true.{' '}
134+
Disables dynamic import parsing when set to true.
132135

133136
:::warning
134137
Note that setting webpackIgnore to true opts out of code splitting.
@@ -137,6 +140,7 @@ Note that setting webpackIgnore to true opts out of code splitting.
137140
##### webpackMode
138141

139142
- **Type:** `"eager" | "lazy" | "weak" | "lazy-once"`
143+
- **Default:** `'lazy'`
140144

141145
Different modes for resolving dynamic imports can be specified. The following options are supported:
142146

website/docs/en/api/runtime-api/module-variables.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { ApiMeta } from '@components/ApiMeta';
66

77
# Module Variables
88

9+
This section covers all variables available in code compiled with Rspack. Modules will be able to access specific data from the compilation process through `module` and other variables.
10+
911
## CommonJs
1012

1113
### module.loaded

website/docs/en/guide/optimization/code-splitting.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ Here we introduce a concept called Chunk, representing a resource that a browser
1212

1313
Rspack use the [import()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) syntax that conforms to the ECMAScript proposal for dynamic imports.
1414

15-
:::tip Inconsistent behaviors with webpack
16-
Rspack doesn't support `require.ensure`.
17-
:::
18-
1915
In `index.js`, we dynamically import two modules through `import()`, thereby separating into a new chunk.
2016

2117
```js title=index.js
@@ -35,6 +31,10 @@ console.log('bar.js');
3531

3632
Now we build this project, we get 3 chunks, `src_bar_js.js`, `src_foo_js.js` and `main.js`, if you see them, you will find `shared.js` exist in both `src_bar_js.js` and `src_foo_js.js`, we will remove duplicated modules in later chapters.
3733

34+
:::tip
35+
Refer to [Module Methods - Dynamic import()](/api/runtime-api/module-methods#dynamic-import) for the detailed dynamic import API, as well as how to use dynamic expressions and magic comments in dynamic import.
36+
:::
37+
3838
:::info
3939
Though `shared.js` exist in 2 chunks, but it is executed only once, you don't have to worry about issue of multiple instance.
4040
:::

website/docs/en/guide/tech/react.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Rspack provides two solutions to support React:
1212

1313
## Configure JSX/TSX
1414

15-
Rspack leverages SWC transformer for JSX/TSX.
15+
Rspack leverages SWC transformer for supporting JSX and TSX.
1616

17-
Add `builtin:swc-loader` loader to support `jsx` and `tsx`:
17+
You can add [builtin:swc-loader](/guide/features/builtin-swc-loader) loader to support `.jsx` and `.tsx` files:
1818

1919
```js title=rspack.config.js
2020
module.exports = {
@@ -55,7 +55,7 @@ module.exports = {
5555
};
5656
```
5757

58-
Refer to [Builtin swc-loader](guide/features/builtin-swc-loader) for detailed configurations.
58+
Refer to [builtin:swc-loader](/guide/features/builtin-swc-loader) for detailed configurations.
5959

6060
## Fast Refresh
6161

website/docs/zh/api/cli.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 命令行接口 (CLI)
22

3-
@rspack/cli 提供了 `serve``build` 两个常用的子命令来简化日常工作。如果你没有安装过 `@rspack/cli`,请先阅读[快速开始](/guide/start/quick-start)章节。
3+
[@rspack/cli](https://npmjs.com/package/@rspack/cli) 提供了 `serve``build` 两个常用的子命令来简化日常工作。如果你没有安装过 `@rspack/cli`,请先阅读[快速开始](/guide/start/quick-start)章节。
44

55
:::warning 和 webpack-cli 的兼容性
66
`@rspack/cli` 不计划和 `webpack-cli` 进行兼容,因此 `@rspack/cli``webpack-cli` 会有很多使用方式的差异。

website/docs/zh/api/runtime-api/module-methods.mdx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ import { ApiMeta } from '@components/ApiMeta';
55

66
# 模块方法
77

8-
当使用 Rspack 打包应用程序时,你可以选择多种模块语法风格,包括 ES6、CommonJS。
8+
当使用 Rspack 打包应用程序时,你可以选择多种模块语法风格,包括 [ES modules](https://nodejs.org/api/esm.html#modules-ecmascript-modules)[CommonJS](https://nodejs.org/api/modules.html)
99

1010
尽管 Rspack 支持多种模块语法,但我们还是建议尽量使用一致的语法,以此避免一些奇怪的行为和 bug。
1111

12-
事实上,当距离最近的 `package.json` 文件中 `"type"` 字段值为 `"module"``"commonjs"` 时,Rspack 会对 `.mjs` 文件、`.cjs` 文件或`.js` 文件进行对应的处理:
12+
事实上,当距离最近的 `package.json` 文件中 ["type"](https://nodejs.org/api/packages.html#type) 字段值为 `"module"``"commonjs"` 时,Rspack 会对 `.mjs` 文件、`.cjs` 文件或`.js` 文件进行对应的处理:
1313

14-
- 后缀为 `.mjs` 或者 `package.json``"type": "module"` 且后缀为 `.js` 时:
14+
- 文件后缀为 `.mjs`或者 `package.json``"type": "module"` 且后缀为 `.js` 时:
1515
- 不允许使用 CommonJS,如 `require``module.exports``exports`
1616
- 引入文件时需指定扩展名,例如,应使用 `import './src/App.mjs'`,而非 `import './src/App'`
17-
- 后缀为 `.cjs` 或者 `package.json``"type": "commonjs"` 且后缀为 `.js` 时:
17+
- 文件后缀为 `.cjs`或者 `package.json``"type": "commonjs"` 且后缀为 `.js` 时:
1818
- 不允许使用 ESM,如 `import``export`
1919

20-
## ES6 (推荐)
20+
## ES modules (推荐)
2121

22-
Rspack 原生支持 ES6 模块语法,可以使用静态的 `import``export``import()` 语法。
22+
Rspack 原生支持 ES modules 语法,可以使用静态的 `import``export``import()` 语法。
2323

2424
:::warning
25-
如果使用其他的 ES6+ 特性,仍然需要引入 SWC / Babel 进行转换。
25+
如果使用其他的 ES6+ 特性,你仍然需要引入 SWC Babel 进行转换。
2626
:::
2727

2828
### import
@@ -61,13 +61,15 @@ export default {
6161
};
6262
```
6363

64-
### 动态 import()
64+
### Dynamic import()
6565

6666
```ts
6767
function import(path: string): Promise;
6868
```
6969

70-
动态加载模块。对 `import()` 的调用被视为分割点,这意味着请求的模块及其子模块被拆分成单独的 chunk。
70+
动态加载模块,参考 [Dynamic import](guide/optimization/code-splitting#动态导入dynamic-import) 了解更多。
71+
72+
`import()` 的调用被视为分割点,这意味着请求的模块及其子模块被拆分成单独的 chunk。
7173

7274
```js
7375
if (module.hot) {
@@ -77,15 +79,16 @@ if (module.hot) {
7779
}
7880
```
7981

80-
:::warning 警告
81-
此功能内部依赖于 `Promise`。如果你在旧版浏览器中使用 `import()`,请记得使用类似于 [`es6-promise`](https://github.com/stefanpenner/es6-promise)[`promise-polyfill`](https://github.com/taylorhakes/promise-polyfill) 的 polyfill 来模拟 `Promise`
82+
:::warning
83+
此功能内部依赖于 `Promise`。如果你在旧版浏览器中使用 `import()`,请记得使用类似于 [core-js](https://github.com/zloirock/core-js)[es6-promise](https://github.com/stefanpenner/es6-promise)[promise-polyfill](https://github.com/taylorhakes/promise-polyfill) 的 polyfill 来模拟 `Promise`
8284
:::
8385

8486
#### import() 中的动态表达式
8587

86-
无法使用完全动态的导入语句,例如 `import(foo)`。因为 `foo` 可能是系统或项目中任何文件的任何路径。
88+
`import()` 无法使用完全动态的导入语句,例如 `import(foo)`。因为 `foo` 可能是系统或项目中任何文件的任何路径。
89+
90+
`import()` 必须至少包含一些关于模块所在位置的信息。可以将打包限制在特定目录或一组文件中,这样当你使用动态表达式时,在 `import()` 调用中可能被请求的每个模块都会被包括进来。
8791

88-
`import()` 必须至少包含一些关于模块所在位置的信息。可以将打包限制在特定目录或一组文件中,这样当你使用动态表达式时 - 在 `import()` 调用中可能被请求的每个模块都会被包括进来。
8992
例如,`import(./locale/${language}.json)` 会将 `./locale` 目录中的每个 `.json` 文件都被打包进新的代码块。在运行时,一旦变量 `language` 被计算出来,任何像 `english.json``german.json` 这样的文件都将可供使用。
9093

9194
```js
@@ -100,7 +103,7 @@ import(`./locale/${language}.json`).then(module => {
100103

101104
<ApiMeta specific={['Rspack', 'Webpack']} />
102105

103-
通过向 import 语句添加注释,我们可以执行诸如命名 chunk 或选择不同模式等操作。有关这些魔法注释的完整列表,请参见下面的代码,以及对这些注释功能的解释。
106+
通过向 import 语句添加注释,我们可以实现「指定 chunk 名称」或「设置加载模式」等操作。有关这些魔法注释的完整列表,请参见下面的代码,以及对这些注释功能的解释。
104107

105108
```js
106109
// 单个模块
@@ -136,6 +139,7 @@ import(
136139
##### webpackMode
137140

138141
- **类型:** `"eager" | "lazy" | "weak" | "lazy-once"`
142+
- **默认值:** `'lazy'`
139143

140144
可以指定以不同的模式解析动态导入。支持以下选项:
141145

website/docs/zh/guide/optimization/code-splitting.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ Rspack 支持代码分割特性,允许让你对代码进行分割,控制生
1212

1313
当涉及到动态代码拆分时, Rspack 使用符合 ECMAScript 提案的 [import()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) 语法来实现动态导入。
1414

15-
:::tip 与 webpack 的差异点
16-
Rspack 不支持 `require.ensure` 功能。
17-
:::
18-
1915
我们在 `index.js` 通过 `import()` 来动态导入 2 个模块,从而分离出一个新的 Chunk。
2016

2117
```js title=index.js
@@ -35,6 +31,10 @@ console.log('bar.js');
3531

3632
此时我们执行构建,会得到 3 个 Chunk ,`src_bar_js.js``src_foo_js.js` 以及 `main.js`,如果我们查看他们,会发现 `src_bar_js.js``src_foo_js.js` 中有重复的部分:`shared.js`,我们后面会介绍为何存在重复模块,以及如何去除重复模块。
3733

34+
:::tip
35+
参考 [模块方法 - Dynamic import()](/api/runtime-api/module-methods#dynamic-import) 了解详细的 dynamic import API,以及如何在 dynamic import 中使用动态表达式和 magic comments。
36+
:::
37+
3838
:::info
3939
虽然 `shared.js` 在 2 个 Chunk 中重复出现,但它只会被执行一次,不用担心重复模块会重复执行的问题。
4040
:::

website/docs/zh/guide/tech/react.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Rspack 提供两种方案来支持 React:
1212

1313
## 配置 JSX/TSX
1414

15-
Rspack 使用 SWC 转译器支持 JSX/TSX。
15+
Rspack 使用 SWC 转译器支持 JSXTSX。
1616

17-
添加 `builtin:swc-loader` 以支持 `jsx``tsx`:
17+
你可以添加 [builtin:swc-loader](/guide/features/builtin-swc-loader) 以支持 `.jsx``.tsx` 文件:
1818

1919
```js title=rspack.config.js
2020
module.exports = {
@@ -55,7 +55,7 @@ module.exports = {
5555
};
5656
```
5757

58-
关于配置项的更多信息请参考 [内置 swc-loader](guide/features/builtin-swc-loader)
58+
关于配置项的更多信息请参考 [builtin:swc-loader](/guide/features/builtin-swc-loader)
5959

6060
## Fast Refresh
6161

@@ -65,7 +65,7 @@ module.exports = {
6565

6666
[React Fast Refresh](https://reactnative.dev/docs/fast-refresh) 功能的开启主要分为两部分:代码注入和代码转换
6767

68-
- 代码注入会注入 `react-refresh` 包中的一些代码,以及一些自定义的运行时代码,都已集成在 [@rspack/plugin-react-refresh](https://github.com/rspack-contrib/rspack-plugin-react-refresh) 插件中,可通过该插件注入
68+
- 代码注入会注入 `react-refresh` 包中的一些代码,以及一些自定义的运行时代码,都已集成在 [@rspack/plugin-react-refresh](https://github.com/rspack-contrib/rspack-plugin-react-refresh) 插件中,可通过该插件注入
6969
- 代码转换可通过 loader 来完成,比如 `swc-loader`[jsc.transform.react.refresh](https://swc.rs/docs/configuration/compilation#jsctransformreactrefresh)`babel-loader`[react-refresh/babel 插件](https://github.com/facebook/react/tree/main/packages/react-refresh)
7070

7171
```js title=rspack.config.js

0 commit comments

Comments
 (0)