Skip to content

Commit

Permalink
feat: allow crossorigin workers
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Jan 31, 2024
1 parent 667f381 commit 2cf2103
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/features/viewPanels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
15 changes: 15 additions & 0 deletions src/tools/crossOriginWorker.ts
Original file line number Diff line number Diff line change
@@ -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 }
1 change: 1 addition & 0 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Worker } from './tools/crossOriginWorker'
export type WorkerLoader = () => Worker

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

0 comments on commit 2cf2103

Please sign in to comment.