fix(jsr): resolve require from source, not from tsup's banner
JSR publishes `src/` and runs no bundler, so the `createRequire` banner tsup gives
the ESM build (tsup.config.ts) never exists there. Every lazy `require(...)` in the
source had nothing to resolve, and one of them was unguarded: `@Html('file')` and
template mode died at boot on Deno with `ReferenceError: require is not defined`.
The two guarded sites were worse than the crash, because they answered confidently
and wrongly. `static` reported *"needs a filesystem and is unavailable on this
runtime (edge)"* while running on Deno, which has one; multipart reported busboy as
not installed while it sat in node_modules. Both blamed the runtime for a packaging
problem, and both named a runtime the reader was not on.
Route all ten call sites through one `nodeRequire`, which prefers the ambient
`require` — so the CJS build and the banner-carrying ESM build behave exactly as
before — and otherwise rebuilds one from `process.getBuiltinModule('node:module')`.
Node 22.3+, Deno and Bun all expose that synchronously, which is the requirement:
every caller sits on a sync path a dynamic `import()` could not serve. It resolves
from `process.cwd()`, since a peer dependency like busboy belongs to the application,
not to a module served over https from a registry.
Where neither exists — workerd, where esbuild rewrites the call into its own throwing
stub — the behaviour is unchanged: the guarded sites still catch, and their "edge has
no filesystem" story is finally the true one.
Covered by a Deno suite that imports `src/` exactly as JSR serves it. The other Deno
tests already did that; none of them touched a path that loads a Node builtin lazily,
which is how a package that died at boot kept a green suite.
Closes #69.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
11 files changed
+145
-23
No files matched your search
@@ -0,0 +1,44 @@
|
||||
// src/node-require.ts — one `require` that survives being published as source.
|
||||
|
||||
type Require = <T>(id: string) => T;
|
||||
|
||||
interface NodeProcess {
|
||||
cwd?: () => string;
|
||||
getBuiltinModule?: (id: string) => unknown;
|
||||
}
|
||||
|
||||
let cached: Require | null | undefined;
|
||||
|
||||
function resolveRequire(): Require | undefined {
|
||||
// The CJS build has a native one, and tsup gives the ESM build one through a `createRequire`
|
||||
// banner (tsup.config.ts). Both npm paths stop here.
|
||||
if (typeof require === 'function') return require as Require;
|
||||
|
||||
// JSR publishes `src/` and runs no bundler, so on that path nothing ever defined `require`.
|
||||
// `process.getBuiltinModule` is the way back: Node 22.3+, Deno and Bun all expose it, and all
|
||||
// three do it *synchronously* — which is the whole requirement, since every caller here sits on
|
||||
// a sync path that a dynamic `import()` could not serve.
|
||||
const proc = (globalThis as { process?: NodeProcess }).process;
|
||||
const nodeModule = proc?.getBuiltinModule?.('node:module') as
|
||||
{ createRequire?: (from: string) => Require } | undefined;
|
||||
if (!nodeModule?.createRequire || !proc?.cwd) return undefined;
|
||||
|
||||
// Resolve from the application's directory rather than from this file: served from JSR this
|
||||
// module lives under an `https://` URL with no `node_modules` beneath it, while an optional peer
|
||||
// dependency such as `busboy` is installed in the app that asked for it.
|
||||
return nodeModule.createRequire(`${proc.cwd()}/`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a Node builtin or an installed package, synchronously.
|
||||
*
|
||||
* Throws on a runtime that can offer neither — workerd, where `node:fs` and `busboy` are genuinely
|
||||
* out of reach. Every caller already guards for that and reports it in its own terms; what they
|
||||
* must not do is report it when the module is in fact reachable, which is what a bare `require`
|
||||
* caused on JSR by failing with `ReferenceError` before any of them was consulted.
|
||||
*/
|
||||
export function nodeRequire<T>(id: string): T {
|
||||
if (cached === undefined) cached = resolveRequire() ?? null;
|
||||
if (cached === null) throw new Error(`cannot load '${id}': this runtime provides no CommonJS require`);
|
||||
return cached<T>(id);
|
||||
}
|
||||
Reference in new issue
Block a user