Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/cli/script/file-import-hook.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** Handle Bun-style file imports while running source through tsx. */
export function load(url, context, nextLoad) {
if (context.importAttributes?.type === "file") {
return {
format: "module",
shortCircuit: true,
source: `export default ${JSON.stringify(new URL(url).pathname)};`,
};
}
return nextLoad(url, context);
}
23 changes: 5 additions & 18 deletions packages/cli/script/require-shim.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,15 @@
* Or in package.json scripts via the `pnpm tsx` alias.
*/

import { createRequire, registerHooks } from "node:module";
import { createRequire, register } from "node:module";

if (typeof globalThis.require === "undefined") {
globalThis.require = createRequire(
new URL("../package.json", import.meta.url)
);
}

// Handle `with { type: "file" }` import attributes in Node.js dev mode.
// Bun supports this natively; esbuild's text-import-plugin handles it at
// build time. In tsx dev mode neither applies, so we register a synchronous
// hook that returns the file path as a string — matching Bun's behaviour.
// registerHooks() is available from Node 22.15+ (our minimum).
registerHooks({
load(url, context, nextLoad) {
if (context.importAttributes?.type === "file") {
return {
format: "module",
shortCircuit: true,
source: `export default ${JSON.stringify(new URL(url).pathname)};`,
};
}
return nextLoad(url, context);
},
});
// Use an asynchronous hook so existing CommonJS loaders may omit `source`.
// Node 24 rejects that valid asynchronous result when it passes through a
// synchronous hook.
register(new URL("./file-import-hook.mjs", import.meta.url));
Loading