mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-10 05:02:58 +00:00
perf: resolve native esm plugin sdk imports
This commit is contained in:
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user