fix: shim createRequire(undefined) for Paseo CJS plugin bundle
This commit is contained in:
1 parent
cd8cb7d528
commit
b8f5b77931
3 files changed
+35
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
/// <reference types="node" />
|
/// <reference types="node" />
|
||||||
|
import "./server/sprites-cjs-shim";
|
||||||
import type { PluginServerContext } from "@getpaseo/plugin/server";
|
import type { PluginServerContext } from "@getpaseo/plugin/server";
|
||||||
import { createSpriteAgentTools } from "./server/agent-tools";
|
import { createSpriteAgentTools } from "./server/agent-tools";
|
||||||
import { createFlySpritesProvider } from "./server/provider";
|
import { createFlySpritesProvider } from "./server/provider";
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import module from "node:module";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
|
// Paseo bundles server plugins into CommonJS and evaluates them from memory,
|
||||||
|
// so `import.meta.url` inside `@fly/sprites/dist/client-signals.js` becomes
|
||||||
|
// `undefined` when calling `createRequire(import.meta.url)` for `../package.json`.
|
||||||
|
const SPRITES_SDK_VERSION = "0.2.4";
|
||||||
|
const moduleNs = module as typeof module & {
|
||||||
|
createRequire?: (filename: string | URL) => NodeRequire;
|
||||||
|
};
|
||||||
|
const originalCreateRequire = moduleNs.createRequire;
|
||||||
|
|
||||||
|
if (typeof originalCreateRequire === "function") {
|
||||||
|
moduleNs.createRequire = function patchedCreateRequire(
|
||||||
|
filename: string | URL,
|
||||||
|
): NodeRequire {
|
||||||
|
if (filename !== undefined) {
|
||||||
|
return originalCreateRequire.call(this, filename);
|
||||||
|
}
|
||||||
|
const fallbackReq = originalCreateRequire.call(
|
||||||
|
this,
|
||||||
|
process.execPath || fileURLToPath("file:///"),
|
||||||
|
);
|
||||||
|
const wrapped = ((id: string) => {
|
||||||
|
if (id === "../package.json") {
|
||||||
|
return { version: SPRITES_SDK_VERSION };
|
||||||
|
}
|
||||||
|
return fallbackReq(id);
|
||||||
|
}) as NodeRequire;
|
||||||
|
Object.assign(wrapped, fallbackReq);
|
||||||
|
return wrapped;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import "./sprites-cjs-shim";
|
||||||
import { randomBytes } from "node:crypto";
|
import { randomBytes } from "node:crypto";
|
||||||
import { execFile as nodeExecFile, spawn as nodeSpawn } from "node:child_process";
|
import { execFile as nodeExecFile, spawn as nodeSpawn } from "node:child_process";
|
||||||
import { stat } from "node:fs/promises";
|
import { stat } from "node:fs/promises";
|
||||||
|
|||||||
Reference in new issue
Block a user