From 634933e5d57b1e4d62e5df92b42c9024ba232707 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Tue, 3 Sep 2024 17:50:52 +0200 Subject: [PATCH] [Bug] v0.51.0 breaks for browser based imports (fix microsoft/monaco-editor#4654) --- src/vs/base/browser/defaultWorkerFactory.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/vs/base/browser/defaultWorkerFactory.ts b/src/vs/base/browser/defaultWorkerFactory.ts index 9cfd42992715f..6d20e7350c556 100644 --- a/src/vs/base/browser/defaultWorkerFactory.ts +++ b/src/vs/base/browser/defaultWorkerFactory.ts @@ -74,7 +74,8 @@ function getWorker(esmWorkerLocation: URI | undefined, label: string): Worker | } function getWorkerBootstrapUrl(label: string, workerScriptUrl: string, workerBaseUrl?: string): string { - if (/^((http:)|(https:)|(file:))/.test(workerScriptUrl) && workerScriptUrl.substring(0, globalThis.origin.length) !== globalThis.origin) { + const workerScriptUrlIsAbsolute = /^((http:)|(https:)|(file:)|(vscode-file:))/.test(workerScriptUrl); + if (workerScriptUrlIsAbsolute && workerScriptUrl.substring(0, globalThis.origin.length) !== globalThis.origin) { // this is the cross-origin case // i.e. the webpage is running at a different origin than where the scripts are loaded from } else { @@ -93,6 +94,12 @@ function getWorkerBootstrapUrl(label: string, workerScriptUrl: string, workerBas } } + if (!isESM && !workerScriptUrlIsAbsolute) { + // we have to convert relative script URLs to the origin because importScripts + // does not work unless the script URL is absolute + workerScriptUrl = new URL(workerScriptUrl, globalThis.origin).toString(); + } + const blob = new Blob([coalesce([ `/*${label}*/`, workerBaseUrl ? `globalThis.MonacoEnvironment = { baseUrl: '${workerBaseUrl}' };` : undefined, @@ -101,7 +108,7 @@ function getWorkerBootstrapUrl(label: string, workerScriptUrl: string, workerBas `globalThis._VSCODE_FILE_ROOT = '${globalThis._VSCODE_FILE_ROOT}';`, `const ttPolicy = globalThis.trustedTypes?.createPolicy('defaultWorkerFactory', { createScriptURL: value => value });`, `globalThis.workerttPolicy = ttPolicy;`, - isESM ? `await import(ttPolicy?.createScriptURL('${workerScriptUrl}') ?? '${workerScriptUrl}');` : `importScripts(ttPolicy?.createScriptURL('${workerScriptUrl}') ?? '${workerScriptUrl}');`, // + isESM ? `await import(ttPolicy?.createScriptURL('${workerScriptUrl}') ?? '${workerScriptUrl}');` : `importScripts(ttPolicy?.createScriptURL('${workerScriptUrl}') ?? '${workerScriptUrl}');`, isESM ? `globalThis.postMessage({ type: 'vscode-worker-ready' });` : undefined, // in ESM signal we are ready after the async import `/*${label}*/` ]).join('')], { type: 'application/javascript' });