Skip to content

Code-first video & image, rendered natively on WebGPU.

Fix the browser engine failing to load: shim node:module

okanasl83e948dfb966
diff --git a/packages/gitframes/src/preview/bundle.ts b/packages/gitframes/src/preview/bundle.ts
index 9ddf6b41..6af1a0d8 100644
--- a/packages/gitframes/src/preview/bundle.ts
+++ b/packages/gitframes/src/preview/bundle.ts
@@ -47,6 +47,7 @@ const SHIMS: Record<string, string> = {
os: "os",
fs: "fs",
"fs/promises": "fs-promises",
+ module: "module",
};
/** Packages that only work in Node; the browser never needs them to draw. */
diff --git a/packages/gitframes/src/preview/preview.test.ts b/packages/gitframes/src/preview/preview.test.ts
index 3f3a5a1d..e8152fa8 100644
--- a/packages/gitframes/src/preview/preview.test.ts
+++ b/packages/gitframes/src/preview/preview.test.ts
@@ -2,9 +2,9 @@ import fs from "node:fs";
import http from "node:http";
import os from "node:os";
import path from "node:path";
-import { fileURLToPath } from "node:url";
+import { fileURLToPath, pathToFileURL } from "node:url";
import { afterEach, describe, expect, it } from "vitest";
-import { prebuiltEngineDir } from "./bundle.js";
+import { buildEngine, prebuiltEngineDir } from "./bundle.js";
import { type PreviewSession, startPreview } from "./index.js";
const entry = new URL("./fixtures/comp.ts", import.meta.url);
@@ -131,6 +131,28 @@ describe("startPreview", () => {
});
});
+describe("browser engine", () => {
+ it("loads every entry, with no Node built-ins left unshimmed", async () => {
+ const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gf-engine-"));
+ try {
+ await buildEngine(dir);
+ for (const name of [
+ "gitframes",
+ "effects",
+ "audio",
+ "signals",
+ "fonts",
+ "player",
+ ]) {
+ // Importing runs the module's top-level code, as a browser would.
+ await import(pathToFileURL(path.join(dir, `${name}.mjs`)).href);
+ }
+ } finally {
+ await fs.promises.rm(dir, { recursive: true, force: true });
+ }
+ }, 120_000);
+});
+
describe("prebuiltEngineDir", () => {
it("finds the engine at the dist root or beside dist/preview", async () => {
const root = await fs.promises.mkdtemp(
diff --git a/packages/gitframes/src/preview/shims/module.ts b/packages/gitframes/src/preview/shims/module.ts
new file mode 100644
index 00000000..532204b0
--- /dev/null
+++ b/packages/gitframes/src/preview/shims/module.ts
@@ -0,0 +1,14 @@
+// `node:module` in the browser. The preview server uses `createRequire` to
+// find optional packages (onnxruntime-web, the node renderer list); in the
+// browser those calls must stay harmless. `createRequire` returns a resolver
+// that throws only when it is actually asked to resolve something, which the
+// callers catch and treat as "not installed".
+export function createRequire(): (id: string) => never {
+ return (id: string) => {
+ throw new Error(`Cannot require "${id}" in the browser preview.`);
+ };
+}
+
+export function isBuiltin(): boolean {
+ return false;
+}