























@@ -258,4 +258,64 @@ describe("getCachedPluginJitiLoader", () => {
258258expect(result.fromJiti).toBe(true);
259259expect(jitiLoader).toHaveBeenCalledWith("/repo/dist/extensions/demo/api.js");
260260});
261+262+it("skips the native-require fast path when tryNative is explicitly false", async () => {
263+const jitiLoader = vi.fn(() => ({ fromJiti: true }));
264+const createJiti = vi.fn(() => jitiLoader);
265+vi.doMock("jiti", () => ({ createJiti }));
266+const nativeStub = vi.fn(() => ({ ok: true, moduleExport: { fromNative: true } }));
267+vi.doMock("./native-module-require.js", () => ({
268+isJavaScriptModulePath: () => true,
269+tryNativeRequireJavaScriptModule: nativeStub,
270+}));
271+const { getCachedPluginJitiLoader } = await importFreshModule<
272+typeof import("./jiti-loader-cache.js")
273+>(import.meta.url, "./jiti-loader-cache.js?scope=native-require-opt-out");
274+275+const cache = new Map();
276+const loader = getCachedPluginJitiLoader({
277+ cache,
278+modulePath: "/repo/dist/extensions/demo/api.js",
279+importerUrl: "file:///repo/src/plugins/bundled-capability-runtime.ts",
280+jitiFilename: "file:///repo/src/plugins/bundled-capability-runtime.ts",
281+aliasMap: { "openclaw/plugin-sdk": "/repo/shim.js" },
282+tryNative: false,
283+});
284+285+const result = loader("/repo/dist/extensions/demo/api.js") as { fromJiti: boolean };
286+expect(result.fromJiti).toBe(true);
287+// With tryNative: false the wrapper must route every target through jiti
288+// so its alias rewrites still apply; native require must not be consulted.
289+expect(nativeStub).not.toHaveBeenCalled();
290+expect(jitiLoader).toHaveBeenCalledWith("/repo/dist/extensions/demo/api.js");
291+});
292+293+it("forwards extra loader arguments through to the jiti fallback", async () => {
294+const jitiLoader = vi.fn(() => ({ fromJiti: true }));
295+const createJiti = vi.fn(() => jitiLoader);
296+vi.doMock("jiti", () => ({ createJiti }));
297+vi.doMock("./native-module-require.js", () => ({
298+isJavaScriptModulePath: () => true,
299+tryNativeRequireJavaScriptModule: () => ({ ok: false }),
300+}));
301+const { getCachedPluginJitiLoader } = await importFreshModule<
302+typeof import("./jiti-loader-cache.js")
303+>(import.meta.url, "./jiti-loader-cache.js?scope=native-require-rest-args");
304+305+const cache = new Map();
306+const loader = getCachedPluginJitiLoader({
307+ cache,
308+modulePath: "/repo/dist/extensions/demo/api.js",
309+importerUrl: "file:///repo/src/plugins/public-surface-loader.ts",
310+jitiFilename: "file:///repo/src/plugins/public-surface-loader.ts",
311+});
312+313+const loose = loader as unknown as (t: string, ...a: unknown[]) => unknown;
314+loose("/repo/dist/extensions/demo/api.js", { hint: "x" }, 42);
315+expect(jitiLoader).toHaveBeenCalledWith(
316+"/repo/dist/extensions/demo/api.js",
317+{ hint: "x" },
318+42,
319+);
320+});
261321});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。