Skip to content

Commit 2cf2103

Browse files
author
Loïc Mangeonjean
committed
feat: allow crossorigin workers
1 parent 667f381 commit 2cf2103

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/features/viewPanels.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import getMarkersServiceOverride from '@codingame/monaco-vscode-markers-service-
22
import getOutputServiceOverride from '@codingame/monaco-vscode-output-service-override'
33
import { registerServices } from '../services'
44
import { registerWorkerLoader } from '../worker'
5+
import { Worker } from '../tools/crossOriginWorker'
56
import '@codingame/monaco-vscode-references-view-default-extension'
67

78
registerWorkerLoader('outputLinkComputer', () => new Worker(new URL('@codingame/monaco-vscode-output-service-override/worker', import.meta.url)))

src/tools/crossOriginWorker.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Cross origin workers don't work
3+
* The workaround used by vscode is to start a worker on a blob url containing a short script calling 'importScripts'
4+
* importScripts accepts to load the code inside the blob worker
5+
*/
6+
class CrossOriginWorker extends Worker {
7+
constructor (url: string | URL, options: WorkerOptions = {}) {
8+
const fullUrl = new URL(url, window.location.href).href
9+
const js = options.type === 'module' ? `import '${fullUrl}';` : `importScripts('${fullUrl}');`
10+
const blob = new Blob([js], { type: 'application/javascript' })
11+
super(URL.createObjectURL(blob), options)
12+
}
13+
}
14+
15+
export { CrossOriginWorker as Worker }

src/worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Worker } from './tools/crossOriginWorker'
12
export type WorkerLoader = () => Worker
23

34
const workerLoaders: Partial<Record<string, WorkerLoader>> = {

0 commit comments

Comments
 (0)