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
|
// This script is run when you change anything in src/js/*
import path from "path";
import { cap, writeIfNotChanged } from "./helpers";
import { createInternalModuleRegistry } from "./internal-module-registry-scanner";
const BASE = path.join(import.meta.dir, "../js");
const CMAKE_BUILD_ROOT = process.argv[2];
if (!CMAKE_BUILD_ROOT) {
console.error("Usage: bun bundle-modules-fast.ts <CMAKE_WORK_DIR>");
process.exit(1);
}
const CODEGEN_DIR = path.join(CMAKE_BUILD_ROOT, "codegen");
let start = performance.now();
function mark(log: string) {
const now = performance.now();
console.log(`${log} (${(now - start).toFixed(0)}ms)`);
start = now;
}
const {
//
moduleList,
nativeModuleIds,
} = createInternalModuleRegistry(BASE);
function idToEnumName(id: string) {
return id
.replace(/\.[mc]?[tj]s$/, "")
.replace(/[^a-zA-Z0-9]+/g, " ")
.split(" ")
.map(x => (["jsc", "ffi", "vm", "tls", "os", "ws", "fs", "dns"].includes(x) ? x.toUpperCase() : cap(x)))
.join("");
}
function idToPublicSpecifierOrEnumName(id: string) {
id = id.replace(/\.[mc]?[tj]s$/, "");
if (id.startsWith("node/")) {
return "node:" + id.slice(5).replaceAll(".", "/");
} else if (id.startsWith("bun/")) {
return "bun:" + id.slice(4).replaceAll(".", "/");
} else if (id.startsWith("internal/")) {
return "internal:" + id.slice(9).replaceAll(".", "/");
} else if (id.startsWith("thirdparty/")) {
return id.slice(11).replaceAll(".", "/");
}
return idToEnumName(id);
}
// This is a generated enum for zig code (exports.zig)
writeIfNotChanged(
path.join(CODEGEN_DIR, "ResolvedSourceTag.zig"),
`// zig fmt: off
pub const ResolvedSourceTag = enum(u32) {
// Predefined
javascript = 0,
package_json_type_module = 1,
wasm = 2,
object = 3,
file = 4,
esm = 5,
json_for_object_loader = 6,
// Built in modules are loaded through InternalModuleRegistry by numerical ID.
// In this enum are represented as \`(1 << 9) & id\`
${moduleList.map((id, n) => ` @"${idToPublicSpecifierOrEnumName(id)}" = ${(1 << 9) | n},`).join("\n")}
// Native modules run through a different system using ESM registry.
${Object.entries(nativeModuleIds)
.map(([id, n]) => ` @"${id}" = ${(1 << 10) | n},`)
.join("\n")}
};
`,
);
mark("Generate Code");
|