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 guide for worker-loader #3809

Merged
merged 3 commits into from
Oct 24, 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
27 changes: 27 additions & 0 deletions website/docs/en/guide/basic/web-workers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Web Workers are a type of JavaScript program that runs in the background, indepe

## Use Web Workers

### Import with Constructors

Web Workers are first-class citizens of Rspack, which means you don't need any loader to use web workers directly in Rspack or Rsbuild projects.

For example, create a file called `worker.js`:
Expand All @@ -31,6 +33,31 @@ worker.onmessage = (event) => {
worker.postMessage(10);
```

For more details, please refer to [Rspack - Web Workers](https://rspack.dev/guide/features/web-workers).

### Using worker-loader

> `worker-loader` is no longer maintained, it is recommended to use the `new Worker()` syntax.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we explain when users will need to use worker-loader?


If your project already uses `worker-loader`, or you want to use the `inline` and other features provided by `worker-loader`, you can use [worker-rspack-loader](https://github.com/rspack-contrib/worker-rspack-loader) as an alternative to `worker-loader` in the Rsbuild or Rspack project.

```ts
export default {
tools: {
rspack: {
module: {
rules: [
{
test: /\.worker\.js$/,
loader: 'worker-rspack-loader',
},
],
},
},
},
};
```

### Loading scripts from remote URLs (cross-origin)

By default, the worker script will be emitted as a separate chunk. This script supports uploading to CDN, but must obey the [same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy).
Expand Down
25 changes: 25 additions & 0 deletions website/docs/zh/guide/basic/web-workers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Web Workers 是一种 JavaScript API,它允许网页在后台线程中执行

## 使用 Web Workers

### 使用 Worker 构造器

Web Workers 是 Rspack 的一等公民,这意味着你不需要任何的 Loader 就可以直接在 Rspack / Rsbuild 项目中使用 Web Workers。详情可参考 [Rspack - Web Workers](https://rspack.dev/zh/guide/features/web-workers)。

例如,创建一个名为 worker.js 的文件:
Expand All @@ -31,6 +33,29 @@ worker.onmessage = (event) => {
worker.postMessage(10);
```

### 使用 worker-loader

> `worker-loader` 已不再维护,推荐使用 `new Worker()` 语法。

如果你的项目已经在使用 `worker-loader`,或者希望使用 `worker-loader` 内的 `inline` 等配置能力,在 Rsbuild / Rspack 项目中可以使用 `worker-loader` 的替代方案 [worker-rspack-loader](https://github.com/rspack-contrib/worker-rspack-loader)。

```ts
export default {
tools: {
rspack: {
module: {
rules: [
{
test: /\.worker\.js$/,
loader: 'worker-rspack-loader',
},
],
},
},
},
};
```

### 从远程 URL 加载脚本(跨域)

默认情况下,worker 脚本会输出成一个独立的 chunk。worker 脚本支持上传到 CDN,但在加载远程脚本时需要遵守[同源策略](https://developer.mozilla.org/zh-CN/docs/Web/Security/Same-origin_policy)。
Expand Down
Loading