@farmfe/plugin-worker 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -0
- package/options.d.ts +3 -1
- package/package.json +13 -11
- package/types/index.d.ts +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @farmfe/plugin-worker
|
|
2
|
+
|
|
3
|
+
A web worker script can be imported using `new Worker()` and `new SharedWorker()` (inspired by [vite](https://github.com/vitejs/vite)).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i -D @farmfe/plugin-worker
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Create a `farm.config.js` [configuration file](https://www.farmfe.org/docs/config/configuring-farm) and import the plugin:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { defineConfig } from '@farmfe/core';
|
|
17
|
+
import worker from '@farmfe/plugin-worker';
|
|
18
|
+
export default defineConfig({
|
|
19
|
+
plugins: [
|
|
20
|
+
worker(),
|
|
21
|
+
],
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 🚧 Import via Constructor
|
|
26
|
+
|
|
27
|
+
A Web Worker can be imported using [`new Worker()`](https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API/Using_web_workers) and [`new SharedWorker()`](https://developer.mozilla.org/zh-CN/docs/Web/API/SharedWorker). This syntax is closer to the standard compared to the worker suffix and is the recommended way to create workers.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
const worker = new Worker(new URL('./worker.js', import.meta.url));
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The Worker constructor accepts options that can be used to create a "module" worker:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const worker = new Worker(new URL('./worker.js', import.meta.url), {
|
|
37
|
+
type: 'module',
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Import with Query Suffix
|
|
42
|
+
|
|
43
|
+
You can directly import a web worker script by adding the `?worker` or `?sharedworker` query parameter to the import request. The default export will be a custom worker constructor:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import MyWorker from './worker?worker';
|
|
47
|
+
|
|
48
|
+
const worker = new MyWorker();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This worker script can also use ESM import statements instead of `importScripts()`. Note: During development, this relies on native browser support, but in production builds, it will be compiled away.
|
|
52
|
+
|
|
53
|
+
By default, the worker script will compile into a separate chunk in production builds. If you want to inline the worker as a base64 string, please add the `inline` query parameter:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import MyWorker from './worker?worker&inline'
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
If you want to read the worker as a URL, add the `url` query:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import MyWorker from './worker?worker&url'
|
|
63
|
+
```
|
package/options.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farmfe/plugin-worker",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"main": "scripts/index.js",
|
|
5
5
|
"types": "scripts/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -27,22 +27,24 @@
|
|
|
27
27
|
"types": "./scripts/index.d.ts",
|
|
28
28
|
"default": "./scripts/index.js"
|
|
29
29
|
},
|
|
30
|
-
"./package.json": "./package.json"
|
|
30
|
+
"./package.json": "./package.json",
|
|
31
|
+
"./types": "./types/index.d.ts"
|
|
31
32
|
},
|
|
32
33
|
"files": [
|
|
33
34
|
"scripts",
|
|
35
|
+
"types",
|
|
34
36
|
"options.d.ts"
|
|
35
37
|
],
|
|
36
38
|
"optionalDependencies": {
|
|
37
|
-
"farmfe-plugin-worker-darwin-arm64": "0.0.
|
|
38
|
-
"farmfe-plugin-worker-darwin-x64": "0.0.
|
|
39
|
-
"farmfe-plugin-worker-linux-arm64-gnu": "0.0.
|
|
40
|
-
"farmfe-plugin-worker-linux-arm64-musl": "0.0.
|
|
41
|
-
"farmfe-plugin-worker-linux-x64-gnu": "0.0.
|
|
42
|
-
"farmfe-plugin-worker-linux-x64-musl": "0.0.
|
|
43
|
-
"farmfe-plugin-worker-win32-arm64-msvc": "0.0.
|
|
44
|
-
"farmfe-plugin-worker-win32-ia32-msvc": "0.0.
|
|
45
|
-
"farmfe-plugin-worker-win32-x64-msvc": "0.0.
|
|
39
|
+
"farmfe-plugin-worker-darwin-arm64": "0.0.2",
|
|
40
|
+
"farmfe-plugin-worker-darwin-x64": "0.0.2",
|
|
41
|
+
"farmfe-plugin-worker-linux-arm64-gnu": "0.0.2",
|
|
42
|
+
"farmfe-plugin-worker-linux-arm64-musl": "0.0.2",
|
|
43
|
+
"farmfe-plugin-worker-linux-x64-gnu": "0.0.2",
|
|
44
|
+
"farmfe-plugin-worker-linux-x64-musl": "0.0.2",
|
|
45
|
+
"farmfe-plugin-worker-win32-arm64-msvc": "0.0.2",
|
|
46
|
+
"farmfe-plugin-worker-win32-ia32-msvc": "0.0.2",
|
|
47
|
+
"farmfe-plugin-worker-win32-x64-msvc": "0.0.2"
|
|
46
48
|
},
|
|
47
49
|
"scripts": {
|
|
48
50
|
"dev": "cargo watch -w src -s 'scripts/watch.sh'",
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
declare module '*?worker' {
|
|
2
|
+
const workerConstructor: {
|
|
3
|
+
new(options?: { name?: string }): Worker
|
|
4
|
+
}
|
|
5
|
+
export default workerConstructor
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
declare module '*?worker&inline' {
|
|
9
|
+
const workerConstructor: {
|
|
10
|
+
new(options?: { name?: string }): Worker
|
|
11
|
+
}
|
|
12
|
+
export default workerConstructor
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare module '*?worker&url' {
|
|
16
|
+
const src: string
|
|
17
|
+
export default src
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare module '*?sharedworker' {
|
|
21
|
+
const sharedWorkerConstructor: {
|
|
22
|
+
new(options?: { name?: string }): SharedWorker
|
|
23
|
+
}
|
|
24
|
+
export default sharedWorkerConstructor
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
declare module '*?sharedworker&inline' {
|
|
28
|
+
const sharedWorkerConstructor: {
|
|
29
|
+
new(options?: { name?: string }): SharedWorker
|
|
30
|
+
}
|
|
31
|
+
export default sharedWorkerConstructor
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare module '*?sharedworker&url' {
|
|
35
|
+
const src: string
|
|
36
|
+
export default src
|
|
37
|
+
}
|