fix(build): let the npm ESM build load on workerd (#93) #173

Merged
chrnx merged 1 commits from fix/edge-esm-no-create-require into develop 2026-10-25 03:54:16 +00:00
Owner

tsup.config.ts gives every ESM output a banner that calls
createRequire(import.meta.url) at module scope, so the lazy
require('ws') / require('busboy') resolve. On workerd import.meta.url
is not a file URL: createRequire throws during module evaluation and
the worker fails to start. dist/edge.mjs, and the chunk it shares with
the other entry points, could therefore not be loaded from npm at all.
test/edge/run.ts bundles src/, which has no banner, so it never saw it.

The banner now wraps the call in a try and leaves require undefined
when it throws. esbuild's __require shim and src/node-require.ts
already handle an undefined require: the latter falls back to
process.getBuiltinModule, and its callers report a missing module in
their own terms. The static node:module import is kept: edge already
requires nodejs_compat, which provides it, and dropping it for
getBuiltinModule would lose Node 22.0-22.2, which lack that API.
Building edge separately was ruled out because it would duplicate
module state (MARK_EDGE) between @green-tea/core and its /edge entry.

test/edge/dist.ts bundles a worker that imports dist/index.mjs and
dist/edge.mjs, as wrangler does with the installed package, and serves
a request on Miniflare. It fails with the old banner and passes now.
npm run test:edge builds before running it, so CI's edge job covers it.

tsup.config.ts gives every ESM output a banner that calls createRequire(import.meta.url) at module scope, so the lazy require('ws') / require('busboy') resolve. On workerd import.meta.url is not a file URL: createRequire throws during module evaluation and the worker fails to start. dist/edge.mjs, and the chunk it shares with the other entry points, could therefore not be loaded from npm at all. test/edge/run.ts bundles src/, which has no banner, so it never saw it. The banner now wraps the call in a try and leaves `require` undefined when it throws. esbuild's __require shim and src/node-require.ts already handle an undefined require: the latter falls back to process.getBuiltinModule, and its callers report a missing module in their own terms. The static node:module import is kept: edge already requires nodejs_compat, which provides it, and dropping it for getBuiltinModule would lose Node 22.0-22.2, which lack that API. Building edge separately was ruled out because it would duplicate module state (MARK_EDGE) between @green-tea/core and its /edge entry. test/edge/dist.ts bundles a worker that imports dist/index.mjs and dist/edge.mjs, as wrangler does with the installed package, and serves a request on Miniflare. It fails with the old banner and passes now. npm run test:edge builds before running it, so CI's edge job covers it.
chrnx added 1 commit 2026-10-25 03:52:11 +00:00
fix(build): let the npm ESM build load on workerd (#93)
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 19s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 23s
CI / runtimes (deno) (pull_request) Successful in 33s
b7a852ac92
tsup.config.ts gives every ESM output a banner that calls
createRequire(import.meta.url) at module scope, so the lazy
require('ws') / require('busboy') resolve. On workerd import.meta.url
is not a file URL: createRequire throws during module evaluation and
the worker fails to start. dist/edge.mjs, and the chunk it shares with
the other entry points, could therefore not be loaded from npm at all.
test/edge/run.ts bundles src/, which has no banner, so it never saw it.

The banner now wraps the call in a try and leaves `require` undefined
when it throws. esbuild's __require shim and src/node-require.ts
already handle an undefined require: the latter falls back to
process.getBuiltinModule, and its callers report a missing module in
their own terms. The static node:module import is kept: edge already
requires nodejs_compat, which provides it, and dropping it for
getBuiltinModule would lose Node 22.0-22.2, which lack that API.
Building edge separately was ruled out because it would duplicate
module state (MARK_EDGE) between @green-tea/core and its /edge entry.

test/edge/dist.ts bundles a worker that imports dist/index.mjs and
dist/edge.mjs, as wrangler does with the installed package, and serves
a request on Miniflare. It fails with the old banner and passes now.
npm run test:edge builds before running it, so CI's edge job covers it.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx force-pushed fix/edge-esm-no-create-require from b8de325d21 to b7a852ac92 2026-10-25 03:52:11 +00:00 Compare
chrnx merged commit 74faa55f41 into develop 2026-10-25 03:54:16 +00:00
Sign in to join this conversation.