From 090169cfbc6699486765b729d532b5b837210b12 Mon Sep 17 00:00:00 2001 From: Andreu Botella Date: Sun, 19 Mar 2023 23:32:54 +0100 Subject: feat(compile): Add support for web workers in standalone mode (#17657) This commit adds support for spawning Web Workers in self-contained binaries created with "deno compile" subcommand. As long as module requested in "new Worker" constructor is part of the eszip (by means of statically importing it beforehand, or using "--include" flag), then the worker can be spawned. --- cli/tests/testdata/compile/workers/basic.out | 5 +++++ cli/tests/testdata/compile/workers/basic.ts | 11 +++++++++++ cli/tests/testdata/compile/workers/not_in_module_map.ts | 11 +++++++++++ cli/tests/testdata/compile/workers/worker.ts | 14 ++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 cli/tests/testdata/compile/workers/basic.out create mode 100644 cli/tests/testdata/compile/workers/basic.ts create mode 100644 cli/tests/testdata/compile/workers/not_in_module_map.ts create mode 100644 cli/tests/testdata/compile/workers/worker.ts (limited to 'cli/tests/testdata/compile') diff --git a/cli/tests/testdata/compile/workers/basic.out b/cli/tests/testdata/compile/workers/basic.out new file mode 100644 index 000000000..9cf9aa18f --- /dev/null +++ b/cli/tests/testdata/compile/workers/basic.out @@ -0,0 +1,5 @@ +worker.js imported from main thread +Starting worker +Hello from worker! +Received 42 +Closing diff --git a/cli/tests/testdata/compile/workers/basic.ts b/cli/tests/testdata/compile/workers/basic.ts new file mode 100644 index 000000000..8edf58de9 --- /dev/null +++ b/cli/tests/testdata/compile/workers/basic.ts @@ -0,0 +1,11 @@ +import "./worker.ts"; + +console.log("Starting worker"); +const worker = new Worker( + new URL("./worker.ts", import.meta.url), + { type: "module" }, +); + +setTimeout(() => { + worker.postMessage(42); +}, 500); diff --git a/cli/tests/testdata/compile/workers/not_in_module_map.ts b/cli/tests/testdata/compile/workers/not_in_module_map.ts new file mode 100644 index 000000000..b43f8cb1f --- /dev/null +++ b/cli/tests/testdata/compile/workers/not_in_module_map.ts @@ -0,0 +1,11 @@ +// This time ./worker.ts is not in the module map, so the worker +// initialization will fail unless worker.js is passed as a side module. + +const worker = new Worker( + new URL("./worker.ts", import.meta.url), + { type: "module" }, +); + +setTimeout(() => { + worker.postMessage(42); +}, 500); diff --git a/cli/tests/testdata/compile/workers/worker.ts b/cli/tests/testdata/compile/workers/worker.ts new file mode 100644 index 000000000..a1c357ab1 --- /dev/null +++ b/cli/tests/testdata/compile/workers/worker.ts @@ -0,0 +1,14 @@ +/// +/// + +if (import.meta.main) { + console.log("Hello from worker!"); + + addEventListener("message", (evt) => { + console.log(`Received ${evt.data}`); + console.log("Closing"); + self.close(); + }); +} else { + console.log("worker.js imported from main thread"); +} -- cgit v1.2.3