fix wasm url prod (#2145)

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2024-04-16 22:23:15 -07:00
committed by GitHub
parent ff482e5f9b
commit 4abb8fc267
3 changed files with 11 additions and 23 deletions

View File

@ -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,
}

View File

@ -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
}

View File

@ -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(