Skip to content

Commit df75bb8

Browse files
authored
Merge pull request #798 from CodinGame/fix-standalone-workers
Make standalone workers support worker options
2 parents 8660fb4 + b697593 commit df75bb8

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: =?UTF-8?q?Lo=C3=AFc=20Mangeonjean?= <loic@coderpad.io>
3+
Date: Thu, 7 May 2026 15:35:50 +0200
4+
Subject: [PATCH] feat: support configuring worker options
5+
6+
and also use cross-origin compatible worker
7+
---
8+
src/common/workers.ts | 18 ++++++++++++++++--
9+
1 file changed, 16 insertions(+), 2 deletions(-)
10+
11+
diff --git a/src/common/workers.ts b/src/common/workers.ts
12+
index 94bbc257..8394dd36 100644
13+
--- a/src/common/workers.ts
14+
+++ b/src/common/workers.ts
15+
@@ -52,12 +52,25 @@ if (
16+
});
17+
}
18+
19+
+function getWorkerBootstrapUrl(workerScriptUrl: string, workerOptions: WorkerOptions): string {
20+
+ const blob = new Blob([[
21+
+ `${workerOptions.type === 'module' ? 'await import' : 'importScripts'}(${JSON.stringify(workerScriptUrl)});`,
22+
+
23+
+ `globalThis.postMessage({ type: 'vscode-worker-ready' });`
24+
+ ].join('')], { type: 'application/javascript' });
25+
+ return URL.createObjectURL(blob);
26+
+}
27+
+
28+
function getWorker(descriptor: { label: string; moduleId: string; createWorker?: () => Worker }): Worker | Promise<Worker> {
29+
const label = descriptor.label;
30+
// Option for hosts to overwrite the worker script (used in the standalone editor)
31+
interface IMonacoEnvironment {
32+
getWorker?(moduleId: string, label: string): Worker | Promise<Worker>;
33+
getWorkerUrl?(moduleId: string, label: string): string;
34+
+ /**
35+
+ * Return the options for web worker scripts.
36+
+ */
37+
+ getWorkerOptions?(moduleId: string, label: string): WorkerOptions | undefined;
38+
}
39+
const monacoEnvironment: IMonacoEnvironment | undefined = (globalThis as any).MonacoEnvironment;
40+
if (monacoEnvironment) {
41+
@@ -66,9 +79,10 @@ function getWorker(descriptor: { label: string; moduleId: string; createWorker?:
42+
}
43+
if (typeof monacoEnvironment.getWorkerUrl === 'function') {
44+
const workerUrl = monacoEnvironment.getWorkerUrl('workerMain.js', label);
45+
+ const workerOptions: WorkerOptions = { name: label, type: 'module', ...monacoEnvironment.getWorkerOptions?.("workerMain.js", label) };
46+
return new Worker(
47+
- ttPolicy ? (ttPolicy.createScriptURL(workerUrl) as unknown as string) : workerUrl,
48+
- { name: label, type: 'module' }
49+
+ getWorkerBootstrapUrl(ttPolicy ? (ttPolicy.createScriptURL(workerUrl) as unknown as string) : workerUrl, workerOptions),
50+
+ workerOptions
51+
);
52+
}
53+
}

0 commit comments

Comments
 (0)