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>
28 lines
1.5 KiB
TypeScript
28 lines
1.5 KiB
TypeScript
import { defineConfig } from 'tsup';
|
|
|
|
// Dual build: CJS (dist/index.js) + ESM (dist/index.mjs) + type declarations, from one entry.
|
|
// tsup injects a createRequire shim into the ESM output so the lazy require('ws') / require('busboy')
|
|
// (optional peer deps) keep working there. reflect-metadata and the peer deps stay external.
|
|
export default defineConfig({
|
|
entry: ['src/index.ts', 'src/deno.ts', 'src/bun.ts', 'src/edge.ts'],
|
|
format: ['cjs', 'esm'],
|
|
dts: true,
|
|
clean: true,
|
|
sourcemap: true,
|
|
target: 'node22',
|
|
// Give the ESM output a real `require` (via createRequire) so the lazy require('ws') / require('busboy')
|
|
// (optional peer deps) resolve instead of hitting esbuild's "Dynamic require not supported" stub.
|
|
// The call is guarded because it runs at module scope: on workerd `import.meta.url` is not a file
|
|
// URL and `createRequire` throws, which failed the whole worker at load (#93). `require` is then
|
|
// undefined, and src/node-require.ts falls back to `process.getBuiltinModule` or reports the miss
|
|
// in its callers' own terms. The static `node:module` import stays: edge already needs
|
|
// `nodejs_compat`, which provides it, and Node 22.0-22.2 have no `getBuiltinModule` to replace it.
|
|
esbuildOptions(options, context) {
|
|
if (context.format === 'esm') {
|
|
options.banner = {
|
|
js: "import { createRequire as _createRequire } from 'node:module'; const require = (() => { try { return _createRequire(import.meta.url); } catch { return undefined; } })();",
|
|
};
|
|
}
|
|
},
|
|
});
|