Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): trace ESM-only extension dependencies (#2291)

Casey Gowrie75bd9c8f2cb5
diff --git a/.changeset/tidy-dodos-import.md b/.changeset/tidy-dodos-import.md
new file mode 100644
index 000000000..92ebc3fcc
--- /dev/null
+++ b/.changeset/tidy-dodos-import.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Allow extension-owned external dependencies to use ESM-only packages that do not expose a CommonJS entry.
diff --git a/packages/eve/src/internal/nitro/host/extension-external-dependency-plugin.test.ts b/packages/eve/src/internal/nitro/host/extension-external-dependency-plugin.test.ts
index 821b7a4db..dccfd4065 100644
--- a/packages/eve/src/internal/nitro/host/extension-external-dependency-plugin.test.ts
+++ b/packages/eve/src/internal/nitro/host/extension-external-dependency-plugin.test.ts
@@ -30,4 +30,12 @@ describe("extension external dependency plugin", () => {
]),
).toEqual({ zod: expect.stringMatching(/zod[/\\].*index\.(?:c?js|mjs)$/) });
});
+
+ it("resolves ESM-only packages without a require export", () => {
+ expect(
+ resolveExtensionExternalDependencyPaths([
+ { externalDependencies: ["emulate"], sourceRoot: process.cwd() },
+ ]),
+ ).toEqual({ emulate: expect.stringMatching(/emulate[/\\]dist[/\\]api\.js$/) });
+ });
});
diff --git a/packages/eve/src/internal/nitro/host/extension-external-dependency-plugin.ts b/packages/eve/src/internal/nitro/host/extension-external-dependency-plugin.ts
index 55a82c19e..b5bda838d 100644
--- a/packages/eve/src/internal/nitro/host/extension-external-dependency-plugin.ts
+++ b/packages/eve/src/internal/nitro/host/extension-external-dependency-plugin.ts
@@ -1,6 +1,6 @@
-import { realpathSync } from "node:fs";
+import { existsSync, readFileSync, realpathSync } from "node:fs";
import { createRequire } from "node:module";
-import { join } from "node:path";
+import { dirname, join, resolve } from "node:path";
interface BundlerPluginShape {
readonly name: string;
@@ -43,7 +43,7 @@ export function resolveExtensionExternalDependencyPaths(
for (const [dependency, anchors] of collectDependencyAnchors(mounts)) {
for (const anchor of anchors) {
try {
- resolvedPaths[dependency] = createRequire(anchor).resolve(dependency);
+ resolvedPaths[dependency] = resolveDependencyEntry(dependency, anchor);
break;
} catch {}
}
@@ -56,6 +56,61 @@ export function resolveExtensionExternalDependencyPaths(
return resolvedPaths;
}
+function resolveDependencyEntry(dependency: string, anchor: string): string {
+ const require = createRequire(anchor);
+ try {
+ return require.resolve(dependency);
+ } catch (error) {
+ const packageJsonPath = require.resolve
+ .paths(dependency)
+ ?.map((searchPath) => join(searchPath, dependency, "package.json"))
+ .find(existsSync);
+ if (packageJsonPath === undefined) {
+ throw error;
+ }
+
+ const pkg = JSON.parse(readFileSync(packageJsonPath, "utf8")) as {
+ exports?: unknown;
+ main?: unknown;
+ module?: unknown;
+ };
+ const entry =
+ resolveImportExport(pkg.exports) ?? stringValue(pkg.module) ?? stringValue(pkg.main);
+ if (entry === undefined) {
+ throw error;
+ }
+ return resolve(dirname(packageJsonPath), entry);
+ }
+}
+
+function resolveImportExport(exports: unknown): string | undefined {
+ if (typeof exports === "string") {
+ return exports;
+ }
+ if (Array.isArray(exports)) {
+ return exports.map(resolveImportExport).find((entry) => entry !== undefined);
+ }
+ if (exports === null || typeof exports !== "object") {
+ return undefined;
+ }
+
+ const conditions = exports as Record<string, unknown>;
+ if ("." in conditions) {
+ return resolveImportExport(conditions["."]);
+ }
+ for (const condition of ["node", "import", "default"]) {
+ const entry = resolveImportExport(conditions[condition]);
+ if (entry !== undefined) {
+ return entry;
+ }
+ }
+ return undefined;
+}
+
+function stringValue(value: unknown): string | undefined {
+ return typeof value === "string" ? value : undefined;
+}
+
function collectDependencyAnchors(
mounts: readonly ExtensionExternalDependencyMount[],
): Map<string, string[]> {