-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess_wasm_pkg.js
More file actions
42 lines (34 loc) · 1.32 KB
/
process_wasm_pkg.js
File metadata and controls
42 lines (34 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env bun
import { readFileSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";
const PKG_DIR = resolve("markdown-renderer/pkg");
const JS_FILE = resolve(PKG_DIR, "markdown_renderer.js");
console.log("Processing wasm-pack output to use external .wasm file...");
try {
// Read the generated JavaScript file
const content = readFileSync(JS_FILE, "utf-8");
// Modify the import to use ?url suffix for Vite
// This tells Vite to treat it as an asset URL instead of a module to bundle
const modifiedContent = content.replace(
"new URL('markdown_renderer_bg.wasm', import.meta.url)",
"new URL('./markdown_renderer_bg.wasm?url', import.meta.url).href",
);
if (content !== modifiedContent) {
writeFileSync(JS_FILE, modifiedContent, "utf-8");
console.log("✓ Modified wasm import to use ?url suffix");
} else {
console.log("⚠ No modifications needed or pattern not found");
}
// Verify no base64 inlining
const base64Pattern = /data:application\/wasm;base64,/;
if (base64Pattern.test(content)) {
console.error(
"✗ Found base64-encoded wasm! This shouldn't happen with wasm-pack --target web",
);
process.exit(1);
}
console.log("✓ Post-processing complete");
} catch (err) {
console.error("Error processing wasm package:", err);
process.exit(1);
}