mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 08:20:43 +00:00
fix(plugins): use module path for bundled jiti loads
This commit is contained in:
99
src/plugins/loader.jiti-filename.test.ts
Normal file
99
src/plugins/loader.jiti-filename.test.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { importFreshModule } from "../../test/helpers/import-fresh.ts";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
function makeTempDir() {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-plugin-loader-"));
|
||||
tempDirs.push(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
function writeBundledPluginFixture(id: string) {
|
||||
const pluginRoot = makeTempDir();
|
||||
fs.writeFileSync(
|
||||
path.join(pluginRoot, "openclaw.plugin.json"),
|
||||
JSON.stringify(
|
||||
{
|
||||
id,
|
||||
configSchema: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
properties: {},
|
||||
},
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
"utf-8",
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(pluginRoot, "index.cjs"),
|
||||
`module.exports = { id: ${JSON.stringify(id)}, register() {} };`,
|
||||
"utf-8",
|
||||
);
|
||||
return pluginRoot;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.resetModules();
|
||||
vi.doUnmock("./jiti-loader-cache.js");
|
||||
delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
describe("createPluginJitiLoader", () => {
|
||||
it("uses the bundled plugin module path as the jiti filename", async () => {
|
||||
const jitiLoaderCalls: Array<{ modulePath: string; jitiFilename?: string }> = [];
|
||||
vi.doMock("./jiti-loader-cache.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("./jiti-loader-cache.js")>();
|
||||
return {
|
||||
...actual,
|
||||
getCachedPluginJitiLoader: vi.fn((params) => {
|
||||
jitiLoaderCalls.push({
|
||||
modulePath: params.modulePath,
|
||||
jitiFilename: params.jitiFilename,
|
||||
});
|
||||
return vi.fn(() => ({
|
||||
default: {
|
||||
id: "demo",
|
||||
register() {},
|
||||
},
|
||||
}));
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
const { loadOpenClawPlugins } = await importFreshModule<typeof import("./loader.js")>(
|
||||
import.meta.url,
|
||||
"./loader.js?scope=jiti-filename",
|
||||
);
|
||||
|
||||
const pluginRoot = writeBundledPluginFixture("demo");
|
||||
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = pluginRoot;
|
||||
|
||||
loadOpenClawPlugins({
|
||||
cache: false,
|
||||
workspaceDir: pluginRoot,
|
||||
onlyPluginIds: ["demo"],
|
||||
config: {
|
||||
plugins: {
|
||||
entries: {
|
||||
demo: {
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const bundledPluginLoad = jitiLoaderCalls.find((call) => call.modulePath.endsWith("index.cjs"));
|
||||
expect(bundledPluginLoad).toBeDefined();
|
||||
expect(bundledPluginLoad?.jitiFilename).toBe(bundledPluginLoad?.modulePath);
|
||||
});
|
||||
});
|
||||
@@ -447,7 +447,7 @@ function createPluginJitiLoader(options: Pick<PluginLoadOptions, "pluginSdkResol
|
||||
cache: jitiLoaders,
|
||||
modulePath,
|
||||
importerUrl: import.meta.url,
|
||||
jitiFilename: import.meta.url,
|
||||
jitiFilename: modulePath,
|
||||
pluginSdkResolution: options.pluginSdkResolution,
|
||||
// Source .ts runtime shims import sibling ".js" specifiers that only exist
|
||||
// after build. Disable native loading for source entries so Jiti rewrites
|
||||
|
||||
Reference in New Issue
Block a user