diff --git a/src/components/LspProvider.tsx b/src/components/LspProvider.tsx index 43bd0b4ed..d4af556fb 100644 --- a/src/components/LspProvider.tsx +++ b/src/components/LspProvider.tsx @@ -21,6 +21,7 @@ import { CopilotWorkerOptions, LspWorker, } from 'editor/plugins/lsp/types' +import { wasmUrl } from 'lang/wasm' const DEFAULT_FILE_NAME: string = 'main.kcl' @@ -99,6 +100,7 @@ export const LspProvider = ({ children }: { children: React.ReactNode }) => { const lspWorker = new Worker({ name: 'kcl' }) const initEvent: KclWorkerOptions = { + wasmUrl: wasmUrl(), token: token, baseUnit: defaultUnit.current, devMode: DEV, @@ -173,6 +175,7 @@ export const LspProvider = ({ children }: { children: React.ReactNode }) => { const lspWorker = new Worker({ name: 'copilot' }) const initEvent: CopilotWorkerOptions = { + wasmUrl: wasmUrl(), token: token, devMode: DEV, } diff --git a/src/editor/plugins/lsp/types.ts b/src/editor/plugins/lsp/types.ts index 6f13bd9a0..b6f786afa 100644 --- a/src/editor/plugins/lsp/types.ts +++ b/src/editor/plugins/lsp/types.ts @@ -5,12 +5,14 @@ export enum LspWorker { Copilot = 'copilot', } export interface KclWorkerOptions { + wasmUrl: string token: string baseUnit: UnitLength devMode: boolean } export interface CopilotWorkerOptions { + wasmUrl: string token: string devMode: boolean } diff --git a/src/editor/plugins/lsp/worker.ts b/src/editor/plugins/lsp/worker.ts index cf227fa85..9c6eedf83 100644 --- a/src/editor/plugins/lsp/worker.ts +++ b/src/editor/plugins/lsp/worker.ts @@ -18,29 +18,9 @@ import { EngineCommandManager } from 'lang/std/engineConnection' const intoServer: IntoServer = new IntoServer() const fromServer: FromServer = FromServer.create() -export const wasmUrl = () => { - const baseUrl = - typeof window === 'undefined' - ? 'http://localhost:3000' - : window.location.origin.includes('tauri://localhost') - ? 'tauri://localhost' // custom protocol for macOS - : window.location.origin.includes('tauri.localhost') - ? 'http://tauri.localhost' // fallback for Windows - : window.location.origin.includes('localhost') - ? 'http://localhost:3000' - : window.location.origin && window.location.origin !== 'null' - ? window.location.origin - : 'http://localhost:3000' - const fullUrl = baseUrl + '/wasm_lib_bg.wasm' - console.log(`Worker full URL for WASM: ${fullUrl}`) - - return fullUrl -} - // Initialise the wasm module. -const initialise = async () => { - const fullUrl = wasmUrl() - const input = await fetch(fullUrl) +const initialise = async (wasmUrl: string) => { + const input = await fetch(wasmUrl) const buffer = await input.arrayBuffer() return init(buffer) } @@ -80,7 +60,10 @@ onmessage = function (event) { switch (eventType) { case LspWorkerEventType.Init: - initialise() + let { wasmUrl }: KclWorkerOptions | CopilotWorkerOptions = eventData as + | KclWorkerOptions + | CopilotWorkerOptions + initialise(wasmUrl) .then((instantiatedModule) => { console.log('Worker: WASM module loaded', worker, instantiatedModule) const config = new ServerConfig(