2021-02-18 11:23:24 +01:00
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
|
2021-10-06 16:47:48 +02:00
|
|
|
export function pathify(path: string): string {
|
2021-02-18 11:23:24 +01:00
|
|
|
if (path.startsWith('file://')) {
|
|
|
|
path = fileURLToPath(path)
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:47:48 +02:00
|
|
|
export function instantiateEmscriptenWasm<T extends EmscriptenWasm.Module>(
|
|
|
|
factory: EmscriptenWasm.ModuleFactory<T>,
|
|
|
|
path: string,
|
|
|
|
workerJS: string = ''
|
|
|
|
): Promise<T> {
|
2021-02-18 11:23:24 +01:00
|
|
|
return factory({
|
2021-10-06 16:47:48 +02:00
|
|
|
locateFile(requestPath) {
|
|
|
|
// The glue code generated by emscripten uses the original
|
|
|
|
// file names of the worker file and the wasm binary.
|
|
|
|
// These will have changed in the bundling process and
|
|
|
|
// we need to inject them here.
|
|
|
|
if (requestPath.endsWith('.wasm')) return pathify(path)
|
|
|
|
if (requestPath.endsWith('.worker.js')) return pathify(workerJS)
|
|
|
|
return requestPath
|
2021-02-18 11:23:24 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|