@vitest/web-worker 0.17.1 → 0.19.0
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/dist/index.js +1 -1
- package/dist/pure.js +141 -3
- package/package.json +3 -3
- package/dist/pure-ad049329.js +0 -141
package/dist/index.js
CHANGED
package/dist/pure.js
CHANGED
@@ -1,3 +1,141 @@
|
|
1
|
-
|
2
|
-
import '
|
3
|
-
|
1
|
+
import { VitestRunner } from 'vitest/node';
|
2
|
+
import { toFilePath } from 'vite-node/utils';
|
3
|
+
|
4
|
+
function getWorkerState() {
|
5
|
+
return globalThis.__vitest_worker__;
|
6
|
+
}
|
7
|
+
class Bridge {
|
8
|
+
constructor() {
|
9
|
+
this.callbacks = {};
|
10
|
+
}
|
11
|
+
on(event, fn) {
|
12
|
+
var _a;
|
13
|
+
(_a = this.callbacks)[event] ?? (_a[event] = []);
|
14
|
+
this.callbacks[event].push(fn);
|
15
|
+
}
|
16
|
+
off(event, fn) {
|
17
|
+
if (this.callbacks[event])
|
18
|
+
this.callbacks[event] = this.callbacks[event].filter((f) => f !== fn);
|
19
|
+
}
|
20
|
+
removeEvents(event) {
|
21
|
+
this.callbacks[event] = [];
|
22
|
+
}
|
23
|
+
clear() {
|
24
|
+
this.callbacks = {};
|
25
|
+
}
|
26
|
+
emit(event, ...data) {
|
27
|
+
return (this.callbacks[event] || []).map((fn) => fn(...data));
|
28
|
+
}
|
29
|
+
}
|
30
|
+
class InlineWorkerRunner extends VitestRunner {
|
31
|
+
constructor(options, context) {
|
32
|
+
super(options);
|
33
|
+
this.context = context;
|
34
|
+
}
|
35
|
+
prepareContext(context) {
|
36
|
+
const ctx = super.prepareContext(context);
|
37
|
+
this.context.self.importScripts = () => {
|
38
|
+
};
|
39
|
+
return Object.assign(ctx, this.context, {
|
40
|
+
importScripts: () => {
|
41
|
+
}
|
42
|
+
});
|
43
|
+
}
|
44
|
+
}
|
45
|
+
function defineWebWorker() {
|
46
|
+
if ("Worker" in globalThis)
|
47
|
+
return;
|
48
|
+
const { config, rpc, mockMap, moduleCache } = getWorkerState();
|
49
|
+
const options = {
|
50
|
+
fetchModule(id) {
|
51
|
+
return rpc.fetch(id);
|
52
|
+
},
|
53
|
+
resolveId(id, importer) {
|
54
|
+
return rpc.resolveId(id, importer);
|
55
|
+
},
|
56
|
+
moduleCache,
|
57
|
+
mockMap,
|
58
|
+
interopDefault: config.deps.interopDefault ?? true,
|
59
|
+
root: config.root,
|
60
|
+
base: config.base
|
61
|
+
};
|
62
|
+
globalThis.Worker = class Worker {
|
63
|
+
constructor(url) {
|
64
|
+
this.inside = new Bridge();
|
65
|
+
this.outside = new Bridge();
|
66
|
+
this.messageQueue = [];
|
67
|
+
this.onmessage = null;
|
68
|
+
this.onmessageerror = null;
|
69
|
+
this.onerror = null;
|
70
|
+
const invalidates = [];
|
71
|
+
const context = {
|
72
|
+
onmessage: null,
|
73
|
+
dispatchEvent: (event) => {
|
74
|
+
this.inside.emit(event.type, event);
|
75
|
+
return true;
|
76
|
+
},
|
77
|
+
addEventListener: this.inside.on.bind(this.inside),
|
78
|
+
removeEventListener: this.inside.off.bind(this.inside),
|
79
|
+
postMessage: (data) => {
|
80
|
+
this.outside.emit("message", { data });
|
81
|
+
},
|
82
|
+
get self() {
|
83
|
+
return context;
|
84
|
+
},
|
85
|
+
get global() {
|
86
|
+
return context;
|
87
|
+
},
|
88
|
+
invalidates
|
89
|
+
};
|
90
|
+
this.inside.on("message", (e) => {
|
91
|
+
var _a;
|
92
|
+
(_a = context.onmessage) == null ? void 0 : _a.call(context, e);
|
93
|
+
});
|
94
|
+
this.outside.on("message", (e) => {
|
95
|
+
var _a;
|
96
|
+
(_a = this.onmessage) == null ? void 0 : _a.call(this, e);
|
97
|
+
});
|
98
|
+
const runner = new InlineWorkerRunner(options, context);
|
99
|
+
const id = (url instanceof URL ? url.toString() : url).replace(/^file:\/+/, "/");
|
100
|
+
const fsPath = toFilePath(id, config.root);
|
101
|
+
invalidates.push(fsPath);
|
102
|
+
runner.executeFile(fsPath).then(() => {
|
103
|
+
invalidates.forEach((fsPath2) => {
|
104
|
+
moduleCache.delete(fsPath2);
|
105
|
+
moduleCache.delete(`${fsPath2}__mock`);
|
106
|
+
});
|
107
|
+
const q = this.messageQueue;
|
108
|
+
this.messageQueue = null;
|
109
|
+
if (q)
|
110
|
+
q.forEach(this.postMessage, this);
|
111
|
+
}).catch((e) => {
|
112
|
+
var _a;
|
113
|
+
this.outside.emit("error", e);
|
114
|
+
(_a = this.onerror) == null ? void 0 : _a.call(this, e);
|
115
|
+
console.error(e);
|
116
|
+
});
|
117
|
+
}
|
118
|
+
dispatchEvent(event) {
|
119
|
+
this.outside.emit(event.type, event);
|
120
|
+
return true;
|
121
|
+
}
|
122
|
+
addEventListener(event, fn) {
|
123
|
+
this.outside.on(event, fn);
|
124
|
+
}
|
125
|
+
removeEventListener(event, fn) {
|
126
|
+
this.outside.off(event, fn);
|
127
|
+
}
|
128
|
+
postMessage(data) {
|
129
|
+
if (this.messageQueue != null)
|
130
|
+
this.messageQueue.push(data);
|
131
|
+
else
|
132
|
+
this.inside.emit("message", { data });
|
133
|
+
}
|
134
|
+
terminate() {
|
135
|
+
this.outside.clear();
|
136
|
+
this.inside.clear();
|
137
|
+
}
|
138
|
+
};
|
139
|
+
}
|
140
|
+
|
141
|
+
export { defineWebWorker };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitest/web-worker",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.19.0",
|
5
5
|
"description": "Web Worker support for testing in Vitest",
|
6
6
|
"exports": {
|
7
7
|
".": {
|
@@ -26,10 +26,10 @@
|
|
26
26
|
"vitest": "*"
|
27
27
|
},
|
28
28
|
"dependencies": {
|
29
|
-
"vite-node": "0.
|
29
|
+
"vite-node": "0.19.0"
|
30
30
|
},
|
31
31
|
"devDependencies": {
|
32
|
-
"rollup": "^2.
|
32
|
+
"rollup": "^2.77.0"
|
33
33
|
},
|
34
34
|
"scripts": {
|
35
35
|
"build": "rimraf dist && rollup -c",
|
package/dist/pure-ad049329.js
DELETED
@@ -1,141 +0,0 @@
|
|
1
|
-
import { VitestRunner } from 'vitest/node';
|
2
|
-
import { toFilePath } from 'vite-node/utils';
|
3
|
-
|
4
|
-
function getWorkerState() {
|
5
|
-
return globalThis.__vitest_worker__;
|
6
|
-
}
|
7
|
-
class Bridge {
|
8
|
-
constructor() {
|
9
|
-
this.callbacks = {};
|
10
|
-
}
|
11
|
-
on(event, fn) {
|
12
|
-
var _a;
|
13
|
-
(_a = this.callbacks)[event] ?? (_a[event] = []);
|
14
|
-
this.callbacks[event].push(fn);
|
15
|
-
}
|
16
|
-
off(event, fn) {
|
17
|
-
if (this.callbacks[event])
|
18
|
-
this.callbacks[event] = this.callbacks[event].filter((f) => f !== fn);
|
19
|
-
}
|
20
|
-
removeEvents(event) {
|
21
|
-
this.callbacks[event] = [];
|
22
|
-
}
|
23
|
-
clear() {
|
24
|
-
this.callbacks = {};
|
25
|
-
}
|
26
|
-
emit(event, ...data) {
|
27
|
-
return (this.callbacks[event] || []).map((fn) => fn(...data));
|
28
|
-
}
|
29
|
-
}
|
30
|
-
class InlineWorkerRunner extends VitestRunner {
|
31
|
-
constructor(options, context) {
|
32
|
-
super(options);
|
33
|
-
this.context = context;
|
34
|
-
}
|
35
|
-
prepareContext(context) {
|
36
|
-
const ctx = super.prepareContext(context);
|
37
|
-
this.context.self.importScripts = () => {
|
38
|
-
};
|
39
|
-
return Object.assign(ctx, this.context, {
|
40
|
-
importScripts: () => {
|
41
|
-
}
|
42
|
-
});
|
43
|
-
}
|
44
|
-
}
|
45
|
-
function defineWebWorker() {
|
46
|
-
if ("Worker" in globalThis)
|
47
|
-
return;
|
48
|
-
const { config, rpc, mockMap, moduleCache } = getWorkerState();
|
49
|
-
const options = {
|
50
|
-
fetchModule(id) {
|
51
|
-
return rpc.fetch(id);
|
52
|
-
},
|
53
|
-
resolveId(id, importer) {
|
54
|
-
return rpc.resolveId(id, importer);
|
55
|
-
},
|
56
|
-
moduleCache,
|
57
|
-
mockMap,
|
58
|
-
interopDefault: config.deps.interopDefault ?? true,
|
59
|
-
root: config.root,
|
60
|
-
base: config.base
|
61
|
-
};
|
62
|
-
globalThis.Worker = class Worker {
|
63
|
-
constructor(url) {
|
64
|
-
this.inside = new Bridge();
|
65
|
-
this.outside = new Bridge();
|
66
|
-
this.messageQueue = [];
|
67
|
-
this.onmessage = null;
|
68
|
-
this.onmessageerror = null;
|
69
|
-
this.onerror = null;
|
70
|
-
const invalidates = [];
|
71
|
-
const context = {
|
72
|
-
onmessage: null,
|
73
|
-
dispatchEvent: (event) => {
|
74
|
-
this.inside.emit(event.type, event);
|
75
|
-
return true;
|
76
|
-
},
|
77
|
-
addEventListener: this.inside.on.bind(this.inside),
|
78
|
-
removeEventListener: this.inside.off.bind(this.inside),
|
79
|
-
postMessage: (data) => {
|
80
|
-
this.outside.emit("message", { data });
|
81
|
-
},
|
82
|
-
get self() {
|
83
|
-
return context;
|
84
|
-
},
|
85
|
-
get global() {
|
86
|
-
return context;
|
87
|
-
},
|
88
|
-
invalidates
|
89
|
-
};
|
90
|
-
this.inside.on("message", (e) => {
|
91
|
-
var _a;
|
92
|
-
(_a = context.onmessage) == null ? void 0 : _a.call(context, e);
|
93
|
-
});
|
94
|
-
this.outside.on("message", (e) => {
|
95
|
-
var _a;
|
96
|
-
(_a = this.onmessage) == null ? void 0 : _a.call(this, e);
|
97
|
-
});
|
98
|
-
const runner = new InlineWorkerRunner(options, context);
|
99
|
-
const id = (url instanceof URL ? url.toString() : url).replace(/^file:\/+/, "/");
|
100
|
-
const fsPath = toFilePath(id, config.root);
|
101
|
-
invalidates.push(fsPath);
|
102
|
-
runner.executeFile(fsPath).then(() => {
|
103
|
-
invalidates.forEach((fsPath2) => {
|
104
|
-
moduleCache.delete(fsPath2);
|
105
|
-
moduleCache.delete(`${fsPath2}__mock`);
|
106
|
-
});
|
107
|
-
const q = this.messageQueue;
|
108
|
-
this.messageQueue = null;
|
109
|
-
if (q)
|
110
|
-
q.forEach(this.postMessage, this);
|
111
|
-
}).catch((e) => {
|
112
|
-
var _a;
|
113
|
-
this.outside.emit("error", e);
|
114
|
-
(_a = this.onerror) == null ? void 0 : _a.call(this, e);
|
115
|
-
console.error(e);
|
116
|
-
});
|
117
|
-
}
|
118
|
-
dispatchEvent(event) {
|
119
|
-
this.outside.emit(event.type, event);
|
120
|
-
return true;
|
121
|
-
}
|
122
|
-
addEventListener(event, fn) {
|
123
|
-
this.outside.on(event, fn);
|
124
|
-
}
|
125
|
-
removeEventListener(event, fn) {
|
126
|
-
this.outside.off(event, fn);
|
127
|
-
}
|
128
|
-
postMessage(data) {
|
129
|
-
if (this.messageQueue != null)
|
130
|
-
this.messageQueue.push(data);
|
131
|
-
else
|
132
|
-
this.inside.emit("message", { data });
|
133
|
-
}
|
134
|
-
terminate() {
|
135
|
-
this.outside.clear();
|
136
|
-
this.inside.clear();
|
137
|
-
}
|
138
|
-
};
|
139
|
-
}
|
140
|
-
|
141
|
-
export { defineWebWorker as d };
|