---
title: "@farmfe/plugin-worker"
locale: "zh"
version: "current"
canonicalUrl: "https://farmfe.org/zh/docs/plugins/official-plugins/worker"
publicationStatus: "prerelease"
---

<a id="farmfeplugin-worker"></a>
# @farmfe/plugin-worker


A Rust Farm plugin for importing web worker scripts with ` new Worker() `, ` new SharedWorker() `, and worker query suffixes.


<a id="installation"></a>
## Installation


**npm**

```javascript
npm install -D @farmfe/plugin-worker
```


**yarn**

```javascript
yarn add -D @farmfe/plugin-worker
```


**pnpm**

```javascript
pnpm add -D @farmfe/plugin-worker
```



<a id="usage"></a>
## Usage


Register the plugin in ` farm.config.ts `:


```ts
import { defineConfig } from "@farmfe/core";
import worker from "@farmfe/plugin-worker";

export default defineConfig({
  plugins: [worker()],
});
```


<a id="import-via-constructor"></a>
## Import via constructor


Use the standard worker constructor syntax:


```ts
const worker = new Worker(new URL("./worker.js", import.meta.url));
```


Module workers are supported through the constructor options:


```ts
const worker = new Worker(new URL("./worker.js", import.meta.url), {
  type: "module",
});
```


<a id="import-with-query-suffix"></a>
## Import with query suffix


Use ` ?worker ` or ` ?sharedworker ` to import a custom worker constructor:


```ts
import MyWorker from "./worker?worker";

const worker = new MyWorker();
```


Add ` inline ` to inline the worker as a base64 string, or ` url ` to read it as a URL:


```ts
import InlineWorker from "./worker?worker&inline";
import WorkerUrl from "./worker?worker&url";
```


<a id="options"></a>
## Options


- ` compilerConfig?: Config ` — optional Farm compilation config used when compiling workers.
