Skip to content

Commit 43cbda7

Browse files
chenjiahanCopilot
andauthored
docs: improve Web Workers documentation with more details (#11737)
* docs: improve Web Workers documentation with more details * fix * Update website/docs/zh/guide/features/web-workers.mdx Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent a3406c0 commit 43cbda7

File tree

5 files changed

+88
-28
lines changed

5 files changed

+88
-28
lines changed

website/docs/en/config/module.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,8 @@ export default {
581581
};
582582
```
583583
584+
> See [Web Workers](/guide/features/web-workers) for more details.
585+
584586
### module.parser.javascript.overrideStrict
585587
586588
<ApiMeta addedVersion="1.0.0-alpha.4" />

website/docs/en/guide/features/web-workers.mdx

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,64 @@ Rspack provides built-in support for [Web Workers](https://developer.mozilla.org
1010

1111
## Usage
1212

13+
Basic Worker creation syntax:
14+
1315
```js
1416
new Worker(new URL('./worker.js', import.meta.url));
1517
```
1618
19+
You can customize the Worker's chunk name through the `name` property (the property value must be statically analyzable). This name will replace the `[name]` placeholder in the generated chunk filename:
20+
1721
```js
1822
new Worker(new URL('./worker.js', import.meta.url), {
19-
name: 'my-worker', // <-- When the value of the name property can be statically analyzed, the worker's chunk name can be customized with this property to replace the [name] placeholder when the chunk file is generated
23+
name: 'my-worker',
2024
});
2125
```
2226
23-
In addition to `new Worker()`, the following syntax is also supported:
27+
:::info
28+
This syntax was chosen for its strong standards compliance. It's built on the standard ECMAScript module specification, which means it works seamlessly even without build tools. For example, it runs natively in modern browsers with ES module support.
29+
:::
30+
31+
### Additional syntax
32+
33+
In addition to `new Worker()`, Rspack also supports the following syntax by default:
34+
35+
- `new SharedWorker()`, see [SharedWorker](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker)
36+
37+
```js
38+
const sharedWorker = new SharedWorker(
39+
new URL('./shared-worker.js', import.meta.url),
40+
);
41+
sharedWorker.port.start();
42+
```
43+
44+
- `import { Worker } from "node:worker_threads"`: commonly used in Node.js environments, see [Worker threads](https://nodejs.org/api/worker_threads.html)
45+
46+
```js
47+
import { Worker } from 'node:worker_threads';
48+
49+
const worker = new Worker(new URL('./node-worker.js', import.meta.url));
50+
```
51+
52+
- `navigator.serviceWorker.register()`: used to register Service Workers, see [ServiceWorkerContainer](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register)
53+
54+
```js
55+
navigator.serviceWorker.register('./sw.js').then(registration => {
56+
console.log('Service worker registration succeeded:', registration);
57+
});
58+
```
2459
25-
- `new SharedWorker()`
26-
- `import { Worker } from "worker_threads"`: usually used in Node environments
27-
- `navigator.serviceWorker.register()`: used to register Service Workers
60+
To support additional custom syntax, you can configure it through [`module.parser.javascript.worker`](/config/module#moduleparserjavascriptworker).
2861
29-
Custom syntax can be provided via [`module.parser.javascript.worker`](/config/module#moduleparserjavascriptworker).
62+
### Examples
3063
31-
For examples:
64+
For usage examples, see:
3265
3366
- [examples/worker](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/worker)
3467
- [examples/monaco-editor-js](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/monaco-editor-js)
3568
- [examples/monaco-editor-ts-react](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/monaco-editor-ts-react)
3669
37-
:::info
38-
The syntax was chosen to allow running code without bundler, it is also available in native ECMAScript modules in the browser.
39-
:::
40-
41-
:::warning
70+
### Notes
4271
4372
1. Note that `new Worker` can also accept a string representation of a URL, but only passing in URLs is supported in Rspack.
4473
2. Rspack does not support the use of variables in `new Worker`. For example, the following code will not work:
@@ -52,8 +81,6 @@ The syntax was chosen to allow running code without bundler, it is also availabl
5281
5382
3. Not support `/* webpackEntryOptions: { filename: "workers/[name].js" } */` magic comments for now.
5483
55-
:::
56-
5784
## worker-loader
5885
5986
:::warning

website/docs/zh/config/module.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,8 @@ export default {
581581
};
582582
```
583583
584+
> 查看 [Web Workers](/guide/features/web-workers) 了解更多。
585+
584586
### module.parser.javascript.overrideStrict
585587
586588
<ApiMeta addedVersion="1.0.0-alpha.4" />

website/docs/zh/guide/features/web-workers.mdx

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,64 @@ Rspack 提供了对 [Web Workers](https://developer.mozilla.org/zh-CN/docs/Web/A
1010

1111
## 使用方式
1212

13+
基本的 Worker 创建语法:
14+
1315
```js
1416
new Worker(new URL('./worker.js', import.meta.url));
1517
```
1618
19+
通过 `name` 属性可以自定义 Worker 的 chunk 名称(需要属性值可被静态分析),此名称会替换生成的 chunk 文件名中的 `[name]` 占位符:
20+
1721
```js
1822
new Worker(new URL('./worker.js', import.meta.url), {
19-
name: 'my-worker', // <-- 在可静态分析 name 属性的值时,可通过该属性自定义 worker 的 chunk name,用于替换最终 chunk 文件生成时 [name] 占位符
23+
name: 'my-worker',
2024
});
2125
```
2226
23-
`new Worker()` 以外还支持以下语法:
27+
:::info
28+
选择该语法的原因在于其具备良好的标准兼容性,它基于标准的 ECMAScript 模块规范,即使在没有构建工具的环境中也能正常运行。例如,它可以直接在支持 ES modules 的现代浏览器中执行。
29+
:::
30+
31+
### 更多语法
32+
33+
`new Worker()` 以外,Rspack 还默认支持以下语法:
34+
35+
- `new SharedWorker()`,详见 [SharedWorker](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker)。
36+
37+
```js
38+
const sharedWorker = new SharedWorker(
39+
new URL('./shared-worker.js', import.meta.url),
40+
);
41+
sharedWorker.port.start();
42+
```
43+
44+
- `import { Worker } from "node:worker_threads"`:常用于 Node.js 环境,详见 [Worker threads](https://nodejs.org/api/worker_threads.html)。
45+
46+
```js
47+
import { Worker } from 'node:worker_threads';
48+
49+
const worker = new Worker(new URL('./node-worker.js', import.meta.url));
50+
```
51+
52+
- `navigator.serviceWorker.register()`:用于注册 Service Worker,详见 [ServiceWorkerContainer](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register)。
53+
54+
```js
55+
navigator.serviceWorker.register('./sw.js').then(registration => {
56+
console.log('Service worker registration succeeded:', registration);
57+
});
58+
```
2459
25-
- `new SharedWorker()`
26-
- `import { Worker } from "worker_thread"`:常用于 node 环境
27-
- `navigator.serviceWorker.register()`:用于注册 Service Worker
60+
如需支持更多自定义语法,可通过 [`module.parser.javascript.worker`](/config/module#moduleparserjavascriptworker) 配置实现。
2861
29-
可通过 [`module.parser.javascript.worker`](/config/module#moduleparserjavascriptworker) 提供自定义语法。
62+
### 示例
3063
3164
使用示例可参考:
3265
3366
- [examples/worker](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/worker)
3467
- [examples/monaco-editor-js](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/monaco-editor-js)
3568
- [examples/monaco-editor-ts-react](https://github.com/rspack-contrib/rstack-examples/tree/main/rspack/monaco-editor-ts-react)
3669
37-
:::info
38-
选择该语法是因为该语法在不使用打包工具时也能运行代码,它可以直接运行在支持 ES modules 的浏览器上运行,是符合标准的 ECMAScript 语法。
39-
:::
40-
41-
:::warning
70+
### 注意事项
4271
4372
1. 需要注意的是 `new Worker` 也可以接受一个 URL 的字符串表示,但在 Rspack 中只支持传入 URL。
4473
2. Rspack 不支持在 `new Worker` 中使用变量。例如,以下代码将不起作用:
@@ -50,9 +79,7 @@ new Worker(new URL('./worker.js', import.meta.url), {
5079
5180
这是因为 Rspack 无法静态分析该语法。在 Rspack 中使用 Worker 语法时,请务必注意此限制。
5281
53-
3. 目前不支持 `/* webpackEntryOptions: { filename: "workers/[name].js" } */` 魔法注释
54-
55-
:::
82+
3. 目前不支持 `/* webpackEntryOptions: { filename: "workers/[name].js" } */` 魔法注释。
5683
5784
## worker-loader
5885

website/docs/zh/misc/team/join-us.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Web Infra 团队正在寻求经验丰富的前端工程师,来共同打造高
5353
- [前端技术专家 - 构建工具与编译器方向](https://jobs.bytedance.com/experienced/position/7304543984570370341/detail)
5454
- [前端技术专家 - Lynx 前端框架方向](https://jobs.bytedance.com/experienced/position/7304543795609241907/detail)
5555

56+
> 以上职位工作地点均包括北京、上海、杭州。
57+
5658
## 📌 岗位要求
5759

5860
- 掌握基于 React 生态和 Node.js 生态的技术栈。

0 commit comments

Comments
 (0)