Skip to content

docs(cn): translated src/content/guides/web-workers.mdx #1075

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

Merged
merged 3 commits into from
May 1, 2021
Merged
Changes from 1 commit
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
14 changes: 8 additions & 6 deletions src/content/guides/web-workers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ title: Web Workers
sort: 21
contributors:
- chenxsan
translators:
- KimYangOfCat
---

As of webpack 5, you can use [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) without [`worker-loader`](https://github.com/webpack-contrib/worker-loader).
webpack 5 开始,你可以使用 [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) 代替 [`worker-loader`](https://github.com/webpack-contrib/worker-loader)

## Syntax
## 语法

```js
new Worker(new URL('./worker.js', import.meta.url));
```

The syntax was chosen to allow running code without bundler, it is also available in native ECMAScript modules in the browser.
选择这种语法是为了实现不使用 bundler 就可以运行代码,它也可以在浏览器中的原生 ECMAScript 模块中使用。

## Example
## 示例

**src/index.js**

Expand All @@ -42,12 +44,12 @@ self.onmessage = ({ data: { question } }) => {

## Node.js

Similar syntax is supported in Node.js (>= 12.17.0):
Node.js 也支持类似的语法(> = 12.17.0):

```js
import { Worker } from 'worker_threads';

new Worker(new URL('./worker.js', import.meta.url));
```

Note that this is only available in ESM. CommonJS is not supported by neither webpack nor Node.js.
请注意,这仅在 ESM 中可用。webpack Node.js 都不支持 CommonJS。