Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

loadBundle.ts

33 lines1.4 KB

1// @effect-diagnostics nodeBuiltinImport:off - runs from bin.ts before any Effect runtime.
2// Desktop packaging uses this to catch missing lazy chunks that --version skips.
3import * as NodeFS from "node:fs";
4import * as NodePath from "node:path";
5import * as NodeURL from "node:url";
6​
7const CHUNK_IMPORT = /(?:\bfrom\s*|\bimport\s*\(\s*|\bimport\s+)["']\.\/([^"'/]+\.mjs)["']/g;
8​
9export async function loadBundle(entryUrl: string): Promise<number> {
10 if (!entryUrl.endsWith(".mjs")) {
11 throw new Error("t3 __load-bundle checks the built bundle; run it from dist/bin.mjs.");
12 }
13 const entryPath = NodeURL.fileURLToPath(entryUrl);
14 const directory = NodePath.dirname(entryPath);
15 const entry = NodePath.basename(entryPath);
16 const files = [entry];
17 const seen = new Set(files);
18 for (const file of files) {
19 // The entry is still in its top-level await, so importing it again would wait on itself.
20 if (file !== entry) await import(NodeURL.pathToFileURL(NodePath.join(directory, file)).href);
21 const source = NodeFS.readFileSync(NodePath.join(directory, file), "utf8");
22 for (const [, chunk] of source.matchAll(CHUNK_IMPORT)) {
23 if (chunk && !seen.has(chunk)) {
24 seen.add(chunk);
25 files.push(chunk);
26 }
27 }
28 }
29 // Cursor's disk-backed SDK loads on first call, outside the chunk graph.
30 const { loadCursorSdk } = await import("@t3tools/provider-cursor/server/sdk");
31 loadCursorSdk();
32 return files.length - 1;