Skip to content

Commit 122b922

Browse files
committed
fix(producer): shim CJS __dirname / __filename in ESM banner
Bundled CJS deps — notably the ffmpeg/Emscripten wasm glue, which does `scriptDirectory = __dirname + "/"` — were crashing at render time with `__dirname is not defined in ES module scope`. The existing banner shimmed `require` from `import.meta.url` but not `__dirname` / `__filename`. Adds both globals from `import.meta.url` alongside the createRequire shim. Verified by re-running a producer render after rebuild — ffmpeg pipeline completes cleanly.
1 parent 211e0ad commit 122b922

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

packages/producer/build.mjs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ mkdirSync("dist", { recursive: true });
1515

1616
const scriptDir = dirname(fileURLToPath(import.meta.url));
1717

18-
// The banner provides a real `require` function via createRequire so that
19-
// esbuild's CJS interop (__require) works correctly in ESM output.
20-
// Without this, bundled CJS deps (recast, yauzl, etc.) that call
21-
// require("fs") throw "Dynamic require of 'fs' is not supported".
18+
// Shim `require` + `__dirname` / `__filename` for bundled CJS deps that expect them in ESM scope.
2219
const cjsBanner = {
23-
js: "import { createRequire as __cjsRequire } from 'module'; const require = __cjsRequire(import.meta.url);",
20+
js: [
21+
"import { createRequire as __cjsRequire } from 'module';",
22+
"import { fileURLToPath as __fileURLToPath } from 'url';",
23+
"import { dirname as __pathDirname } from 'path';",
24+
"const require = __cjsRequire(import.meta.url);",
25+
"const __filename = __fileURLToPath(import.meta.url);",
26+
"const __dirname = __pathDirname(__filename);",
27+
].join(" "),
2428
};
2529

2630
const workspaceAliasPlugin = {

0 commit comments

Comments
 (0)