fix(plugin-sdk): fall back to src root alias files

This commit is contained in:
Vincent Koc
2026-03-22 22:24:57 -07:00
parent 39409b6a6d
commit be3a2e2eb6
2 changed files with 31 additions and 3 deletions

View File

@@ -148,7 +148,7 @@ function loadMonolithicSdk() {
}
}
monolithicSdk = getJiti(false)(path.join(__dirname, "compat.ts"));
monolithicSdk = getJiti(false)(path.join(getPackageRoot(), "src", "plugin-sdk", "compat.ts"));
return monolithicSdk;
}
@@ -175,7 +175,7 @@ function loadDiagnosticEventsModule() {
}
diagnosticEventsModule = getJiti(false)(
path.join(__dirname, "..", "infra", "diagnostic-events.ts"),
path.join(getPackageRoot(), "src", "infra", "diagnostic-events.ts"),
);
return diagnosticEventsModule;
}

View File

@@ -24,6 +24,7 @@ function loadRootAliasWithStubs(options?: {
distExists?: boolean;
env?: Record<string, string | undefined>;
monolithicExports?: Record<string | symbol, unknown>;
aliasPath?: string;
}) {
let createJitiCalls = 0;
let jitiLoadCalls = 0;
@@ -48,6 +49,7 @@ function loadRootAliasWithStubs(options?: {
__dirname: string,
) => void;
const module = { exports: {} as Record<string, unknown> };
const aliasPath = options?.aliasPath ?? rootAliasPath;
const localRequire = ((id: string) => {
if (id === "node:path") {
return path;
@@ -78,7 +80,7 @@ function loadRootAliasWithStubs(options?: {
}
throw new Error(`unexpected require: ${id}`);
}) as NodeJS.Require;
wrapper(module.exports, localRequire, module, rootAliasPath, path.dirname(rootAliasPath));
wrapper(module.exports, localRequire, module, aliasPath, path.dirname(aliasPath));
return {
moduleExports: module.exports,
get createJitiCalls() {
@@ -175,6 +177,32 @@ describe("plugin-sdk root alias", () => {
expect(lazyModule.createJitiOptions.at(-1)?.tryNative).toBe(false);
});
it("falls back to src files even when the alias itself is loaded from dist", () => {
const packageRoot = path.dirname(path.dirname(rootAliasPath));
const distAliasPath = path.join(packageRoot, "dist", "plugin-sdk", "root-alias.cjs");
const lazyModule = loadRootAliasWithStubs({
aliasPath: distAliasPath,
distExists: false,
monolithicExports: {
onDiagnosticEvent: () => () => undefined,
slowHelper: () => "loaded",
},
});
expect((lazyModule.moduleExports.slowHelper as () => string)()).toBe("loaded");
expect(lazyModule.loadedSpecifiers).toContain(
path.join(packageRoot, "src", "plugin-sdk", "compat.ts"),
);
expect(
typeof (lazyModule.moduleExports.onDiagnosticEvent as (listener: () => void) => () => void)(
() => undefined,
),
).toBe("function");
expect(lazyModule.loadedSpecifiers).toContain(
path.join(packageRoot, "src", "infra", "diagnostic-events.ts"),
);
});
it("forwards delegateCompactionToRuntime through the compat-backed root alias", () => {
const delegateCompactionToRuntime = () => "delegated";
const lazyModule = loadRootAliasWithStubs({