fix(build): let the npm ESM build load on workerd (#93) #173
No files matched your search
@@ -23,6 +23,7 @@ coverage/
|
||||
test/edge/_worker.mjs
|
||||
test/edge/_parity-worker.mjs
|
||||
test/edge/_compression-worker.mjs
|
||||
test/edge/_dist-worker.mjs
|
||||
|
||||
# Bun writes this on `bun install`, and nothing here consumes it: `npm run test:bun` runs
|
||||
# `bun test` against the node_modules npm installed, which package-lock.json already pins.
|
||||
|
||||
@@ -347,6 +347,13 @@ npm treats versions as semver and semver forbids leading zeros.
|
||||
`@headers(['X-Trace'])` keeps the names as written in the bag it hands over, so the handler reads
|
||||
`X-Trace` back; `@headers()` with no argument still returns the lower-cased bag. Node, Deno, Bun
|
||||
and the edge share this path.
|
||||
- **The npm ESM build loads on Cloudflare Workers** (#93). tsup gives every ESM file a
|
||||
`createRequire(import.meta.url)` banner so the lazy `require('ws')`/`require('busboy')` work, and
|
||||
it ran at module scope. On workerd `import.meta.url` is not a file URL, so `createRequire` threw
|
||||
and the whole worker failed to load: `@green-tea/core/edge` from npm could not be used at all.
|
||||
The edge suite never noticed because it bundles `src/`. The call is now guarded; where it fails,
|
||||
`require` is left undefined and the existing fallback takes over. `npm run test:edge` now also
|
||||
builds and boots the published `dist/` on workerd. Node, Deno, Bun and JSR are unchanged.
|
||||
|
||||
- **`listen()` rejects when the port is taken** (#138). A bind failure such as `EADDRINUSE` or
|
||||
`EACCES` arrives as the server's `error` event, which `listen()` did not handle: the promise
|
||||
|
||||
+1
-1
@@ -90,7 +90,7 @@
|
||||
"test:dev": "vitest",
|
||||
"test:deno": "deno test --allow-net --allow-read --allow-run --allow-env --no-check test/deno/",
|
||||
"test:bun": "bun test test/bun/",
|
||||
"test:edge": "tsx test/edge/run.ts && tsx test/edge/compression.ts",
|
||||
"test:edge": "tsx test/edge/run.ts && tsx test/edge/compression.ts && npm run build && tsx test/edge/dist.ts",
|
||||
"test:all": "npm test && npm run test:deno && npm run test:bun && npm run test:edge && deno publish --dry-run --allow-dirty",
|
||||
"typecheck": "tsc --noEmit -p tsconfig.json",
|
||||
"typecheck:bench": "tsc --noEmit -p bench/tsconfig.json",
|
||||
|
||||
@@ -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] })) };
|
||||
@@ -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);
|
||||
});
|
||||
+6
-1
@@ -12,10 +12,15 @@ export default defineConfig({
|
||||
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 = _createRequire(import.meta.url);",
|
||||
js: "import { createRequire as _createRequire } from 'node:module'; const require = (() => { try { return _createRequire(import.meta.url); } catch { return undefined; } })();",
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user