forked from denoland/wasmbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindgen.ts
More file actions
82 lines (76 loc) · 2.27 KB
/
bindgen.ts
File metadata and controls
82 lines (76 loc) · 2.27 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2018-2025 the Deno authors. MIT license.
import { createTempDirSync } from "@david/temp";
import { generate_bindgen } from "./wasmbuild.js";
import type { Path } from "@david/path";
export interface BindgenTextFileOutput {
name: string;
text: string;
}
export interface BindgenOutput {
jsBg: BindgenTextFileOutput;
ts: BindgenTextFileOutput;
snippets: Map<string, string[]>;
localModules: Map<string, string>;
start: string | undefined;
wasm: {
name: string;
bytes: number[];
};
}
export async function generateBindgen({ libName, filePath, ext }: {
libName: string;
filePath: Path;
ext: string;
}) {
// if wasmbuild is building itself, then we need to use the wasm-bindgen-cli
const hasEnvPerm = await Deno.permissions.query({ name: "env" });
if (hasEnvPerm && Deno.env.get("WASMBUILD_BINDGEN_UPGRADE") === "1") {
return generateForSelfBuild(filePath);
}
const originalWasmBytes = filePath.readBytesSync();
return await generate_bindgen(
libName,
ext,
originalWasmBytes,
) as BindgenOutput;
}
async function generateForSelfBuild(filePath: Path): Promise<BindgenOutput> {
// When upgrading wasm-bindgen within wasmbuild, we can't rely on
// using the .wasm file because it will be out of date and not build,
// so we revert to using the globally installed wasm-bindgen cli.
// See https://github.com/denoland/wasmbuild/issues/51 for more details
using tempDir = createTempDirSync();
// note: ensure you have run `cargo install -f wasm-bindgen-cli` to upgrade
// to the latest version
const p = new Deno.Command("wasm-bindgen", {
args: [
"--target",
"bundler",
"--out-dir",
tempDir.toString(),
filePath.toString(),
],
}).spawn();
const output = await p.status;
if (!output.success) {
throw new Error("Failed.");
}
const wasmBytes = tempDir.join("wasmbuild_bg.wasm").readBytesSync();
return {
jsBg: {
name: "wasmbuild_bg.js",
text: tempDir.join("wasmbuild_bg.js").readTextSync(),
},
ts: {
name: "wasmbuild.d.ts",
text: tempDir.join("wasmbuild.d.ts").readTextSync(),
},
localModules: new Map(),
snippets: new Map(),
start: undefined,
wasm: {
name: "wasmbuild_bg.wasm",
bytes: Array.from(wasmBytes),
},
};
}