Skip to content

Commit

Permalink
simplify worker loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfiszel committed Jan 17, 2025
1 parent ea2d155 commit 7f18d84
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 48 deletions.
96 changes: 51 additions & 45 deletions frontend/src/lib/monaco_workers/build_workers.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,61 @@
import { useWorkerFactory } from 'monaco-editor-wrapper/workerFactory'
import { initEnhancedMonacoEnvironment } from 'monaco-languageclient/vscode/services'

// import cssWorker from 'monaco-editor-wrapper/workers/module/css?worker&url'
// import htmlWorker from 'monaco-editor-wrapper/workers/module/html?worker&url'
// import jsonWorker from 'monaco-editor-wrapper/workers/module/json?worker&url'
// import editorWorker from 'monaco-editor-wrapper/workers/module/editor?worker&url'

export function buildWorkerDefinition() {
useWorkerFactory({
workerOverrides: {
ignoreMapping: true,
workerLoaders: {
editorWorkerService: () => {
return new Worker(
new URL('monaco-editor-wrapper/workers/module/editor', import.meta.url),
{
type: 'module'
}
)
},
javascript: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/ts', import.meta.url), {
type: 'module'
})
},
typescript: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/ts', import.meta.url), {
type: 'module'
})
},
json: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/json', import.meta.url), {
type: 'module'
})
},
html: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/html', import.meta.url), {
type: 'module'
})
},
css: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/css', import.meta.url), {
type: 'module'
})
},
graphql: () => {
console.log('Creating graphql worker')
return new Worker(new URL(`./graphql.worker.bundle.js`, import.meta.url), {
name: 'graphql'
})
}
const envEnhanced = initEnhancedMonacoEnvironment()

const getWorker = (moduleId: string, label: string) => {
console.log(`getWorker: moduleId: ${moduleId} label: ${label}`)

let selector = label
let workerLoaders = {
editorWorkerService: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/editor', import.meta.url), {
type: 'module'
})
},
javascript: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/ts', import.meta.url), {
type: 'module'
})
},
typescript: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/ts', import.meta.url), {
type: 'module'
})
},
json: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/json', import.meta.url), {
type: 'module'
})
},
html: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/html', import.meta.url), {
type: 'module'
})
},
css: () => {
return new Worker(new URL('monaco-editor-wrapper/workers/module/css', import.meta.url), {
type: 'module'
})
},
graphql: () => {
console.log('Creating graphql worker')
return new Worker(new URL(`./graphql.worker.bundle.js`, import.meta.url), {
name: 'graphql'
})
}
}
})
const workerFunc = workerLoaders[selector]
if (workerFunc !== undefined) {
return workerFunc()
} else {
throw new Error(`Unimplemented worker ${label} (${moduleId})`)
}
}
envEnhanced.getWorker = getWorker
}
15 changes: 12 additions & 3 deletions frontend/src/routes/test_dev_page/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// import EditableSchemaWrapper from '$lib/components/schema/EditableSchemaWrapper.svelte'
// import FlowBuilder from '$lib/components/FlowBuilder.svelte'
import ScriptBuilder from '$lib/components/ScriptBuilder.svelte'
// import ScriptBuilder from '$lib/components/ScriptBuilder.svelte'
import ScriptEditor from '$lib/components/ScriptEditor.svelte'
// import type { OpenFlow } from '$lib/gen'
// import { writable, type Writable } from 'svelte/store'
Expand Down Expand Up @@ -50,12 +51,20 @@
}}
/> -->

<ScriptBuilder
<!-- <ScriptBuilder
script={{
summary: 'foo',
path: 'u/admin/foo',
description: 'foo',
language: 'python3',
content: 'print("foo")'
}}
/>
/> -->

<!-- path: "/path/to/script",
code: "console.log('Hello, world!');",
lang: "bun",
tag: "", -->
<div class="h-screen">
<ScriptEditor path="/path/to/script" code="console.log('Hello, world!');" lang="bun" tag="" />
</div>
3 changes: 3 additions & 0 deletions frontend/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const config = {
},
dedupe: ['vscode', 'monaco-editor']
},
worker: {
format: 'es'
},
assetsInclude: ['**/*.wasm']
}

Expand Down

0 comments on commit 7f18d84

Please sign in to comment.