perf: resolve native esm plugin sdk imports

This commit is contained in:
Peter Steinberger
2026-05-29 23:37:57 +01:00
parent d7354d61b2
commit 41a92ae445
6 changed files with 282 additions and 9 deletions

View File

@@ -1,6 +1,8 @@
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { afterEach, describe, expect, it } from "vitest";
import {
clearNativeRequireJavaScriptModuleCache,
@@ -78,6 +80,51 @@ describe("tryNativeRequireJavaScriptModule", () => {
).toEqual({ ok: false });
});
it("loads native ESM graphs with temporary SDK aliases", () => {
const dir = makeTempDir();
const sdkPath = path.join(dir, "sdk.js");
const modulePath = path.join(dir, "plugin.mjs");
const probePath = path.join(dir, "probe.mjs");
const nativeRequireModuleUrl = pathToFileURL(
path.join(process.cwd(), "src", "plugins", "native-module-require.ts"),
).href;
fs.writeFileSync(
sdkPath,
'export const defineChannelMessageAdapter = () => "adapter";\n',
"utf8",
);
fs.writeFileSync(
modulePath,
'import { defineChannelMessageAdapter } from "openclaw/plugin-sdk/channel-outbound";\nexport const marker = defineChannelMessageAdapter();\n',
"utf8",
);
fs.writeFileSync(
probePath,
[
`import { tryNativeRequireJavaScriptModule } from ${JSON.stringify(nativeRequireModuleUrl)};`,
`const result = tryNativeRequireJavaScriptModule(${JSON.stringify(modulePath)}, {`,
" allowWindows: true,",
` aliasMap: { "openclaw/plugin-sdk/channel-outbound": ${JSON.stringify(sdkPath)} },`,
"});",
"if (!result.ok) {",
' throw new Error("native require declined ESM graph");',
"}",
"console.log(result.moduleExport.marker);",
"",
].join("\n"),
"utf8",
);
const result = spawnSync(process.execPath, ["--import", "tsx", probePath], {
cwd: process.cwd(),
encoding: "utf8",
});
expect(result.stderr).toBe("");
expect(result.status).toBe(0);
expect(result.stdout.trim()).toBe("adapter");
});
it("declines missing dependency errors when the caller can use source transform fallback", () => {
const dir = makeTempDir();
const modulePath = path.join(dir, "plugin.cjs");