chore(producer): shim __filename/__dirname in the CJS banner (#1553)

* chore(producer): shim __filename/__dirname in the CJS banner

Bundled CJS deps like wawoff2 call __dirname; without the shim they throw
"__dirname is not defined in ES module" at render time. Also ignore .zed/.

* chore(producer): use a template literal for the CJS banner (review nit)
This commit is contained in:
Miguel Ángel
2026-06-22 01:19:44 -04:00
committed by GitHub
parent 80f89c30d3
commit fa9e8202e7
2 changed files with 14 additions and 5 deletions
+3
View File
@@ -144,3 +144,6 @@ hyperframes-bench/
tmp/
# Studio-generated preview thumbnails
.thumbnails/
# Local editor settings
.zed/
+11 -5
View File
@@ -15,12 +15,18 @@ mkdirSync("dist", { recursive: true });
const scriptDir = dirname(fileURLToPath(import.meta.url));
// The banner provides a real `require` function via createRequire so that
// esbuild's CJS interop (__require) works correctly in ESM output.
// Without this, bundled CJS deps (recast, yauzl, etc.) that call
// require("fs") throw "Dynamic require of 'fs' is not supported".
// The banner provides a real `require` (via createRequire) plus the CJS-only
// `__filename`/`__dirname` globals so esbuild's CJS interop works in ESM output.
// Without `require`, bundled CJS deps (recast, yauzl, etc.) that call
// require("fs") throw "Dynamic require of 'fs' is not supported"; without the
// dirname shims, deps like wawoff2 throw "__dirname is not defined in ES module".
const cjsBanner = {
js: "import { createRequire as __cjsRequire } from 'module'; const require = __cjsRequire(import.meta.url);",
js: `import { createRequire as __cjsRequire } from 'module';
import { fileURLToPath as __cjsFileURLToPath } from 'url';
import { dirname as __cjsDirname } from 'path';
const require = __cjsRequire(import.meta.url);
const __filename = __cjsFileURLToPath(import.meta.url);
const __dirname = __cjsDirname(__filename);`,
};
const workspaceAliasPlugin = {