mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 13:50:49 +00:00
fix(plugins): avoid duplicate native fallback loads
This commit is contained in:
@@ -406,7 +406,6 @@ describe("getCachedPluginModuleLoader", () => {
|
||||
// allowWindows must be passed so the native fast path works on Windows too.
|
||||
expect(nativeStub).toHaveBeenCalledWith("/repo/dist/extensions/demo/api.js", {
|
||||
allowWindows: true,
|
||||
fallbackOnMissingDependency: true,
|
||||
});
|
||||
expect(getPluginModuleLoaderStats()).toMatchObject({
|
||||
calls: 1,
|
||||
@@ -417,6 +416,48 @@ describe("getCachedPluginModuleLoader", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("does not source-transform fallback after native loading reaches a missing dependency", async () => {
|
||||
const fromSourceTransformer = vi.fn();
|
||||
const createJiti = vi.fn(() => fromSourceTransformer);
|
||||
vi.doMock("jiti", () => ({ createJiti }));
|
||||
const missingDependency = Object.assign(new Error("Cannot find module 'missing-dep'"), {
|
||||
code: "MODULE_NOT_FOUND",
|
||||
});
|
||||
const nativeStub = vi.fn(() => {
|
||||
throw missingDependency;
|
||||
});
|
||||
vi.doMock("./native-module-require.js", () => ({
|
||||
isJavaScriptModulePath: () => true,
|
||||
tryNativeRequireJavaScriptModule: nativeStub,
|
||||
}));
|
||||
const { getCachedPluginModuleLoader, getPluginModuleLoaderStats } = await importFreshModule<
|
||||
typeof import("./plugin-module-loader-cache.js")
|
||||
>(import.meta.url, "./plugin-module-loader-cache.js?scope=native-missing-dependency");
|
||||
|
||||
const cache = new Map();
|
||||
const loader = getCachedPluginModuleLoader({
|
||||
cache,
|
||||
modulePath: "/repo/dist/extensions/demo/api.js",
|
||||
importerUrl: "file:///repo/src/plugins/public-surface-loader.ts",
|
||||
loaderFilename: "file:///repo/src/plugins/public-surface-loader.ts",
|
||||
createLoader: asPluginModuleLoaderFactory(createJiti),
|
||||
});
|
||||
|
||||
expect(() => loader("/repo/dist/extensions/demo/api.js")).toThrow("missing-dep");
|
||||
expect(createJiti).not.toHaveBeenCalled();
|
||||
expect(fromSourceTransformer).not.toHaveBeenCalled();
|
||||
expect(nativeStub).toHaveBeenCalledWith("/repo/dist/extensions/demo/api.js", {
|
||||
allowWindows: true,
|
||||
});
|
||||
expect(getPluginModuleLoaderStats()).toMatchObject({
|
||||
calls: 1,
|
||||
nativeHits: 0,
|
||||
nativeMisses: 0,
|
||||
sourceTransformFallbacks: 0,
|
||||
sourceTransformForced: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to source transform when the native-require helper declines", async () => {
|
||||
const fromSourceTransformer = vi.fn(() => ({ fromSourceTransform: true }));
|
||||
const createJiti = vi.fn(() => fromSourceTransformer);
|
||||
|
||||
@@ -284,7 +284,6 @@ function createPluginModuleLoader(params: {
|
||||
}
|
||||
const native = tryNativeRequireJavaScriptModule(target, {
|
||||
allowWindows: true,
|
||||
fallbackOnMissingDependency: true,
|
||||
});
|
||||
if (native.ok) {
|
||||
pluginModuleLoaderStats.nativeHits += 1;
|
||||
|
||||
Reference in New Issue
Block a user