fix(plugin-sdk): align root alias scoped sdk map

This commit is contained in:
Vincent Koc
2026-04-14 17:39:12 +01:00
parent 604a5e07d0
commit a80ecb9937
2 changed files with 28 additions and 4 deletions

View File

@@ -7,6 +7,7 @@ let monolithicSdk = null;
let diagnosticEventsModule = null;
const jitiLoaders = new Map();
const pluginSdkSubpathsCache = new Map();
const pluginSdkPackageNames = ["openclaw/plugin-sdk", "@openclaw/plugin-sdk"];
const isDistRootAlias = __filename.includes(
`${path.sep}dist${path.sep}plugin-sdk${path.sep}root-alias.cjs`,
);
@@ -127,14 +128,16 @@ function buildPluginSdkAliasMap(useDist) {
const ext = useDist ? ".js" : ".ts";
const normalizeTarget = (target) =>
process.platform === "win32" ? target.replace(/\\/g, "/") : target;
const aliasMap = {
"openclaw/plugin-sdk": normalizeTarget(__filename),
};
const aliasMap = Object.fromEntries(
pluginSdkPackageNames.map((packageName) => [packageName, normalizeTarget(__filename)]),
);
for (const subpath of listPluginSdkExportedSubpaths()) {
const candidate = path.join(pluginSdkDir, `${subpath}${ext}`);
if (fs.existsSync(candidate)) {
aliasMap[`openclaw/plugin-sdk/${subpath}`] = normalizeTarget(candidate);
for (const packageName of pluginSdkPackageNames) {
aliasMap[`${packageName}/${subpath}`] = normalizeTarget(candidate);
}
}
}

View File

@@ -253,6 +253,27 @@ describe("plugin-sdk root alias", () => {
);
});
it("builds scoped and unscoped plugin-sdk aliases for jiti loads", () => {
const lazyModule = loadRootAliasWithStubs({
distExists: true,
monolithicExports: {
slowHelper: (): string => "loaded",
},
});
expect((lazyModule.moduleExports.slowHelper as () => string)()).toBe("loaded");
expect(lazyModule.createJitiOptions.at(-1)?.alias).toMatchObject({
"openclaw/plugin-sdk": rootAliasPath,
"@openclaw/plugin-sdk": rootAliasPath,
"openclaw/plugin-sdk/group-access": expect.stringContaining(
path.join("src", "plugin-sdk", "group-access.ts"),
),
"@openclaw/plugin-sdk/group-access": expect.stringContaining(
path.join("src", "plugin-sdk", "group-access.ts"),
),
});
});
it("prefers hashed dist diagnostic events chunks before falling back to src", () => {
const packageRoot = createPackageRoot();
const distAliasPath = createDistAliasPath();