Skip to content

Commit b9d0473

Browse files
committed
refactor(plugins): match Jiti callable contract
1 parent 70dbbf3 commit b9d0473

2 files changed

Lines changed: 9 additions & 60 deletions

File tree

‎src/plugins/plugin-module-loader-cache.test.ts‎

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -780,33 +780,4 @@ describe("getCachedPluginModuleLoader", () => {
780780
"file:///C:/Users/alice/openclaw/extensions/feishu/api.ts",
781781
);
782782
});
783-
784-
it("forwards extra loader arguments through to the source-transform fallback", async () => {
785-
const fromSourceTransformer = vi.fn(() => ({ fromSourceTransform: true }));
786-
const createJiti = vi.fn(() => fromSourceTransformer);
787-
vi.doMock("./native-module-require.js", () => ({
788-
isJavaScriptModulePath: () => true,
789-
tryNativeRequireJavaScriptModule: () => ({ ok: false }),
790-
}));
791-
const { getCachedPluginModuleLoader } = await importFreshModule<
792-
typeof import("./plugin-module-loader-cache.js")
793-
>(import.meta.url, "./plugin-module-loader-cache.js?scope=native-require-rest-args");
794-
795-
const cache = new Map();
796-
const loader = getCachedPluginModuleLoader({
797-
cache,
798-
modulePath: "/repo/dist/extensions/demo/api.js",
799-
importerUrl: "file:///repo/src/plugins/public-surface-loader.ts",
800-
loaderFilename: "file:///repo/src/plugins/public-surface-loader.ts",
801-
createLoader: asPluginModuleLoaderFactory(createJiti),
802-
});
803-
804-
const loose = loader as unknown as (t: string, ...a: unknown[]) => unknown;
805-
loose("/repo/dist/extensions/demo/api.js", { hint: "x" }, 42);
806-
expect(fromSourceTransformer).toHaveBeenCalledWith(
807-
"/repo/dist/extensions/demo/api.js",
808-
{ hint: "x" },
809-
42,
810-
);
811-
});
812783
});

‎src/plugins/plugin-module-loader-cache.ts‎

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from "./sdk-alias.js";
1616

1717
/** Jiti-based module loader used for plugin source/runtime imports. */
18-
type PluginModuleLoader = ReturnType<typeof createJiti>;
18+
type PluginModuleLoader = (target: string) => unknown;
1919
export type PluginModuleLoaderFactory = typeof createJiti;
2020
export type PluginModuleLoaderCache = Pick<
2121
PluginLruCache<PluginModuleLoader>,
@@ -205,18 +205,7 @@ function createLazySourceTransformLoader(params: {
205205
tryNative: false,
206206
},
207207
);
208-
loadWithSourceTransform = new Proxy(jitiLoader, {
209-
apply(target, thisArg, argArray) {
210-
const [first, ...rest] = argArray as [unknown, ...unknown[]];
211-
if (typeof first === "string") {
212-
return Reflect.apply(target, thisArg, [
213-
toSourceTransformImportPath(first),
214-
...rest,
215-
] as never) as never;
216-
}
217-
return Reflect.apply(target, thisArg, argArray as never) as never;
218-
},
219-
});
208+
loadWithSourceTransform = (target) => jitiLoader(toSourceTransformImportPath(target));
220209
return loadWithSourceTransform;
221210
};
222211
}
@@ -234,10 +223,7 @@ function createPluginModuleLoader(params: {
234223
...params,
235224
});
236225
const loadedTargetExports = new Map<string, unknown>();
237-
const loadCachedTarget = (target: string, rest: unknown[], load: () => unknown): unknown => {
238-
if (rest.length > 0) {
239-
return load();
240-
}
226+
const loadCachedTarget = (target: string, load: () => unknown): unknown => {
241227
if (loadedTargetExports.has(target)) {
242228
return loadedTargetExports.get(target);
243229
}
@@ -250,17 +236,13 @@ function createPluginModuleLoader(params: {
250236
// jiti's alias rewriting to surface a narrow SDK slice), route every
251237
// target through jiti so those alias rewrites still apply.
252238
if (!params.tryNative) {
253-
return ((target: string, ...rest: unknown[]) => {
254-
return loadCachedTarget(target, rest, () => {
239+
return (target) =>
240+
loadCachedTarget(target, () => {
255241
pluginModuleLoaderStats.calls += 1;
256242
pluginModuleLoaderStats.sourceTransformForced += 1;
257243
recordSourceTransformTarget(target);
258-
return (getLoadWithSourceTransform() as (t: string, ...a: unknown[]) => unknown)(
259-
target,
260-
...rest,
261-
);
244+
return getLoadWithSourceTransform()(target);
262245
});
263-
}) as PluginModuleLoader;
264246
}
265247
// Otherwise prefer native require() for already-compiled JS artifacts
266248
// (the bundled plugin public surfaces shipped in dist/). jiti's transform
@@ -269,8 +251,8 @@ function createPluginModuleLoader(params: {
269251
// for TS / TSX sources and for the small set of require(esm) /
270252
// async-module fallbacks `tryNativeRequireJavaScriptModule` declines to
271253
// handle.
272-
return ((target: string, ...rest: unknown[]) => {
273-
return loadCachedTarget(target, rest, () => {
254+
return (target) =>
255+
loadCachedTarget(target, () => {
274256
pluginModuleLoaderStats.calls += 1;
275257
const native = tryNativeRequireJavaScriptModule(target, {
276258
allowWindows: true,
@@ -285,12 +267,8 @@ function createPluginModuleLoader(params: {
285267
pluginModuleLoaderStats.nativeMisses += 1;
286268
pluginModuleLoaderStats.sourceTransformFallbacks += 1;
287269
recordSourceTransformTarget(target);
288-
return (getLoadWithSourceTransform() as (t: string, ...a: unknown[]) => unknown)(
289-
target,
290-
...rest,
291-
);
270+
return getLoadWithSourceTransform()(target);
292271
});
293-
}) as PluginModuleLoader;
294272
}
295273

296274
export function getCachedPluginModuleLoader(

0 commit comments

Comments
 (0)