Skip to content
Snippets Groups Projects
Commit f5b6428f authored by Mateusz Tyszczak's avatar Mateusz Tyszczak :scroll: Committed by Bartek Wrona
Browse files

Allow bundling WASM binary in webpack to create single-file bundle

parent aaa9bc0b
No related branches found
No related tags found
No related merge requests found
...@@ -11,16 +11,25 @@ declare global { ...@@ -11,16 +11,25 @@ declare global {
} }
const ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined'; const ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined';
const getModuleExt = () => ({ const getModuleExt = () => {
locateFile: (path, scriptDirectory) => { // Warning: important change is moving conditional ternary expression outside of URL constructor call, what confused parcel analyzer.
if (path === "wax.common.wasm") { // Seems it must have simple variables & literals present to correctly translate code.
/// Warning: important change is moving conditional ternary expression outside of URL constructor call, what confused parcel analyzer. const wasmFilePath = (ENVIRONMENT_IS_WORKER ? new URL("./build_wasm/wax.common.wasm", self.location.href) : new URL("./build_wasm/wax.common.wasm", import.meta.url)).href;
/// Seems it must have simple variables & literals present to correctly translate code. // Fallback for client-bundled inlined WASM, e.g. when using webpack
return (ENVIRONMENT_IS_WORKER ? new URL("./build_wasm/wax.common.wasm", self.location.href) : new URL("./build_wasm/wax.common.wasm", import.meta.url)).href; let wasmBinary: Buffer | undefined;
} if (wasmFilePath.startsWith("data:application/wasm;base64,"))
return scriptDirectory + path; wasmBinary = Buffer.from(wasmFilePath.slice(29), "base64");
}
}) return {
locateFile(path: string, scriptDirectory: string): string {
if (path === "wax.common.wasm") {
return wasmFilePath;
}
return scriptDirectory + path;
},
wasmBinary
};
};
/** /**
* Creates a Wax Hive chain instance * Creates a Wax Hive chain instance
......
...@@ -10,12 +10,17 @@ const moduleArgs = (async () => { ...@@ -10,12 +10,17 @@ const moduleArgs = (async () => {
if ((import.meta as any).client || (!("client" in import.meta) && typeof (import.meta as any).env === "object" && "SSR" in (import.meta as any).env)) { if ((import.meta as any).client || (!("client" in import.meta) && typeof (import.meta as any).env === "object" && "SSR" in (import.meta as any).env)) {
const resolvedUrl = (await import('./build_wasm/wax.common.wasm' + '?url')).default; const resolvedUrl = (await import('./build_wasm/wax.common.wasm' + '?url')).default;
let wasmBinary: Buffer | undefined;
if (resolvedUrl.startsWith("data:application/wasm;base64,"))
wasmBinary = Buffer.from(resolvedUrl.slice(29), "base64");
return { return {
locateFile: (path, scriptDirectory) => { locateFile(path: string, scriptDirectory: string): string {
if (path === "wax.common.wasm") if (path === "wax.common.wasm")
return resolvedUrl; return resolvedUrl;
return scriptDirectory + path; return scriptDirectory + path;
} },
wasmBinary
}; };
} else { } else {
return {}; return {};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment