diff --git a/src/vs/base/worker/defaultWorkerFactory.ts b/src/vs/base/worker/defaultWorkerFactory.ts index 344588ad9efb7684b815a107a1cce04899714295..4fe3caa24038f5d7d95d3358c6a54bfec1dad81e 100644 --- a/src/vs/base/worker/defaultWorkerFactory.ts +++ b/src/vs/base/worker/defaultWorkerFactory.ts @@ -7,17 +7,22 @@ import { globals } from 'vs/base/common/platform'; import { logOnceWebWorkerWarning, IWorker, IWorkerCallback, IWorkerFactory } from 'vs/base/common/worker/simpleWorker'; -function getWorkerUrl(workerId: string, label: string): string { - // Option for hosts to overwrite the worker script url (used in the standalone editor) - if (globals.MonacoEnvironment && typeof globals.MonacoEnvironment.getWorkerUrl === 'function') { - return globals.MonacoEnvironment.getWorkerUrl(workerId, label); +function getWorker(workerId: string, label: string): Worker { + // Option for hosts to overwrite the worker script (used in the standalone editor) + if (globals.MonacoEnvironment) { + if (typeof globals.MonacoEnvironment.getWorker === 'function') { + return globals.MonacoEnvironment.getWorker(workerId, label); + } + if (typeof globals.MonacoEnvironment.getWorkerUrl === 'function') { + return new Worker(globals.MonacoEnvironment.getWorkerUrl(workerId, label)); + } } // ESM-comment-begin if (typeof require === 'function') { - return require.toUrl('./' + workerId) + '#' + label; + return new Worker(require.toUrl('./' + workerId) + '#' + label); } // ESM-comment-end - throw new Error(`You must define a function MonacoEnvironment.getWorkerUrl`); + throw new Error(`You must define a function MonacoEnvironment.getWorkerUrl or MonacoEnvironment.getWorker`); } /** @@ -31,7 +36,7 @@ class WebWorker implements IWorker { constructor(moduleId: string, id: number, label: string, onMessageCallback: IWorkerCallback, onErrorCallback: (err: any) => void) { this.id = id; - this.worker = new Worker(getWorkerUrl('workerMain.js', label)); + this.worker = getWorker('workerMain.js', label); this.postMessage(moduleId); this.worker.onmessage = function (ev: any) { onMessageCallback(ev.data);