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

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>
This commit is contained in:
chrnx committed 2026-10-24 22:52:07 -05:00
1 parent dd3a74afcd
commit b7a852ac92
6 files changed
+77 -2

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
// Worker entry for test/edge/dist.ts: the npm ESM build as an app installs it, not `src/`.
import { createApp, Route, Get, Module } from '../../dist/index.mjs';
import { edgeHandler } from '../../dist/edge.mjs';
@Route('/rt')
class Rt {
@Get('/hello')
hello() {
return { ok: true };
}
}
@Module({ mountpoint: '/', controllers: [Rt] })
class AppModule {}
export default { fetch: edgeHandler(createApp({ modules: [AppModule] })) };
+46
View File
@@ -0,0 +1,46 @@
// #93: the published `dist/` ESM build must load on workerd. test/edge/run.ts bundles `src/`, so it
// never saw tsup's `createRequire` banner, which ran at module scope and threw there because
// `import.meta.url` is not a file URL. `npm run test:edge` builds first.
//
// Run with: npm run test:edge
import { strict as assert } from 'node:assert';
import { existsSync } from 'node:fs';
import { build } from 'esbuild';
import { Miniflare } from 'miniflare';
const BUNDLE = 'test/edge/_dist-worker.mjs';
async function main() {
if (!existsSync('dist/edge.mjs')) throw new Error('dist/edge.mjs is missing: run `npm run build` first');
// Same bundling as run.ts, which is what wrangler does with an installed package.
await build({
entryPoints: ['test/edge/dist-worker.ts'],
bundle: true,
format: 'esm',
outfile: BUNDLE,
platform: 'browser',
target: 'esnext',
external: ['node:*', 'http', 'https'],
alias: { stream: 'node:stream', events: 'node:events', zlib: 'node:zlib', crypto: 'node:crypto' },
});
const mf = new Miniflare({
modules: true,
scriptPath: BUNDLE,
compatibilityDate: '2024-11-01',
compatibilityFlags: ['nodejs_compat'],
});
try {
const res = await mf.dispatchFetch('http://localhost/rt/hello');
assert.equal(res.status, 200);
assert.deepEqual(await res.json(), { ok: true });
console.log('edge dist: OK (the npm ESM build loads and serves on workerd)');
} finally {
await mf.dispose();
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});