From 2cf210371c432a4400ad4a8eff069f8309148f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mangeonjean?= Date: Tue, 30 Jan 2024 14:44:14 +0100 Subject: [PATCH] feat: allow crossorigin workers --- src/features/viewPanels.ts | 1 + src/tools/crossOriginWorker.ts | 15 +++++++++++++++ src/worker.ts | 1 + 3 files changed, 17 insertions(+) create mode 100644 src/tools/crossOriginWorker.ts diff --git a/src/features/viewPanels.ts b/src/features/viewPanels.ts index 206195a..34187d7 100644 --- a/src/features/viewPanels.ts +++ b/src/features/viewPanels.ts @@ -2,6 +2,7 @@ import getMarkersServiceOverride from '@codingame/monaco-vscode-markers-service- import getOutputServiceOverride from '@codingame/monaco-vscode-output-service-override' import { registerServices } from '../services' import { registerWorkerLoader } from '../worker' +import { Worker } from '../tools/crossOriginWorker' import '@codingame/monaco-vscode-references-view-default-extension' registerWorkerLoader('outputLinkComputer', () => new Worker(new URL('@codingame/monaco-vscode-output-service-override/worker', import.meta.url))) diff --git a/src/tools/crossOriginWorker.ts b/src/tools/crossOriginWorker.ts new file mode 100644 index 0000000..74cef34 --- /dev/null +++ b/src/tools/crossOriginWorker.ts @@ -0,0 +1,15 @@ +/** + * Cross origin workers don't work + * The workaround used by vscode is to start a worker on a blob url containing a short script calling 'importScripts' + * importScripts accepts to load the code inside the blob worker + */ +class CrossOriginWorker extends Worker { + constructor (url: string | URL, options: WorkerOptions = {}) { + const fullUrl = new URL(url, window.location.href).href + const js = options.type === 'module' ? `import '${fullUrl}';` : `importScripts('${fullUrl}');` + const blob = new Blob([js], { type: 'application/javascript' }) + super(URL.createObjectURL(blob), options) + } +} + +export { CrossOriginWorker as Worker } diff --git a/src/worker.ts b/src/worker.ts index 5f5bc57..5c0d289 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -1,3 +1,4 @@ +import { Worker } from './tools/crossOriginWorker' export type WorkerLoader = () => Worker const workerLoaders: Partial> = {