Skip to content

Commit

Permalink
[Bug] v0.51.0 breaks for browser based imports (fix microsoft/monaco-…
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Sep 3, 2024
1 parent 43addb7 commit 634933e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/vs/base/browser/defaultWorkerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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' });
Expand Down

0 comments on commit 634933e

Please sign in to comment.