fix(plugin-sdk): fall back from dist facade overrides to source surfaces

This commit is contained in:
Vincent Koc
2026-04-14 20:06:26 +01:00
parent df956f8162
commit 34f9211e5c
2 changed files with 55 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ import { afterEach, describe, expect, it } from "vitest";
import {
PUBLIC_SURFACE_SOURCE_EXTENSIONS,
normalizeBundledPluginArtifactSubpath,
resolveBundledPluginPublicSurfacePath,
resolveBundledPluginSourcePublicSurfacePath,
} from "./public-surface-runtime.js";
@@ -49,6 +50,25 @@ describe("bundled plugin public surface runtime", () => {
).toBe(modulePath);
});
it("falls back from package dist overrides to the source extension tree", () => {
const packageRoot = createTempDir();
const sourceModulePath = path.join(packageRoot, "extensions", "demo", "api.ts");
fs.mkdirSync(path.dirname(sourceModulePath), { recursive: true });
fs.writeFileSync(sourceModulePath, "export const marker = 'source';\n", "utf8");
const bundledPluginsDir = path.join(packageRoot, "dist", "extensions");
fs.mkdirSync(path.join(bundledPluginsDir, "demo"), { recursive: true });
expect(
resolveBundledPluginPublicSurfacePath({
rootDir: packageRoot,
bundledPluginsDir,
dirName: "demo",
artifactBasename: "api.js",
}),
).toBe(sourceModulePath);
});
it("allows plugin-local nested artifact paths", () => {
expect(normalizeBundledPluginArtifactSubpath("src/outbound-adapter.js")).toBe(
"src/outbound-adapter.js",

View File

@@ -58,6 +58,28 @@ export function resolveBundledPluginSourcePublicSurfacePath(params: {
return null;
}
function resolvePackageSourceFallbackForBundledDir(params: {
rootDir: string;
bundledPluginsDir: string;
dirName: string;
artifactBasename: string;
}): string | null {
const normalizedBundledDir = path.resolve(params.bundledPluginsDir);
const normalizedRootDir = path.resolve(params.rootDir);
const packageBundledDirs = [
path.join(normalizedRootDir, "dist", "extensions"),
path.join(normalizedRootDir, "dist-runtime", "extensions"),
];
if (!packageBundledDirs.includes(normalizedBundledDir)) {
return null;
}
return resolveBundledPluginSourcePublicSurfacePath({
sourceRoot: path.join(normalizedRootDir, "extensions"),
dirName: params.dirName,
artifactBasename: params.artifactBasename,
});
}
export function resolveBundledPluginPublicSurfacePath(params: {
rootDir: string;
dirName: string;
@@ -75,11 +97,19 @@ export function resolveBundledPluginPublicSurfacePath(params: {
if (fs.existsSync(explicitBuiltCandidate)) {
return explicitBuiltCandidate;
}
return resolveBundledPluginSourcePublicSurfacePath({
sourceRoot: explicitBundledPluginsDir,
dirName: params.dirName,
artifactBasename,
});
return (
resolveBundledPluginSourcePublicSurfacePath({
sourceRoot: explicitBundledPluginsDir,
dirName: params.dirName,
artifactBasename,
}) ??
resolvePackageSourceFallbackForBundledDir({
rootDir: params.rootDir,
bundledPluginsDir: explicitBundledPluginsDir,
dirName: params.dirName,
artifactBasename,
})
);
}
for (const candidate of [