fix(plugin-sdk): resolve hashed diagnostic events chunks

This commit is contained in:
Vincent Koc 2026-03-23 00:15:46 -07:00
parent 04c69ea3a0
commit b23e9c577d
2 changed files with 65 additions and 8 deletions

View File

@ -76,6 +76,20 @@ function getPackageRoot() {
return path.resolve(__dirname, "..", "..");
}
function findDistChunkByPrefix(prefix) {
const distRoot = path.join(getPackageRoot(), "dist");
try {
const entries = fs.readdirSync(distRoot, { withFileTypes: true });
const match = entries.find(
(entry) =>
entry.isFile() && entry.name.startsWith(`${prefix}-`) && entry.name.endsWith(".js"),
);
return match ? path.join(distRoot, match.name) : null;
} catch {
return null;
}
}
function listPluginSdkExportedSubpaths() {
const packageRoot = getPackageRoot();
if (pluginSdkSubpathsCache.has(packageRoot)) {
@ -157,7 +171,7 @@ function loadDiagnosticEventsModule() {
return diagnosticEventsModule;
}
const distCandidate = path.resolve(
const directDistCandidate = path.resolve(
__dirname,
"..",
"..",
@ -165,12 +179,17 @@ function loadDiagnosticEventsModule() {
"infra",
"diagnostic-events.js",
);
if (!shouldPreferSourceInTests && fs.existsSync(distCandidate)) {
try {
diagnosticEventsModule = getJiti(true)(distCandidate);
return diagnosticEventsModule;
} catch {
// Fall through to source path if dist is unavailable or stale.
if (!shouldPreferSourceInTests) {
const distCandidate =
(fs.existsSync(directDistCandidate) && directDistCandidate) ||
findDistChunkByPrefix("diagnostic-events");
if (distCandidate) {
try {
diagnosticEventsModule = getJiti(true)(distCandidate);
return diagnosticEventsModule;
} catch {
// Fall through to source path if dist is unavailable or stale.
}
}
}

View File

@ -22,6 +22,7 @@ type EmptySchema = {
function loadRootAliasWithStubs(options?: {
distExists?: boolean;
distEntries?: string[];
env?: Record<string, string | undefined>;
monolithicExports?: Record<string | symbol, unknown>;
aliasPath?: string;
@ -62,7 +63,18 @@ function loadRootAliasWithStubs(options?: {
"./plugin-sdk/group-access": { default: "./dist/plugin-sdk/group-access.js" },
},
}),
existsSync: () => options?.distExists ?? false,
existsSync: (targetPath: string) => {
if (targetPath.endsWith(path.join("dist", "infra", "diagnostic-events.js"))) {
return options?.distExists ?? false;
}
return options?.distExists ?? false;
},
readdirSync: () =>
(options?.distEntries ?? []).map((name) => ({
name,
isFile: () => true,
isDirectory: () => false,
})),
};
}
if (id === "jiti") {
@ -203,6 +215,32 @@ describe("plugin-sdk root alias", () => {
);
});
it("prefers hashed dist diagnostic events chunks before falling back to src", () => {
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,
distEntries: ["diagnostic-events-W3Hz61fI.js"],
monolithicExports: {
onDiagnosticEvent: () => () => undefined,
slowHelper: () => "loaded",
},
});
expect(
typeof (lazyModule.moduleExports.onDiagnosticEvent as (listener: () => void) => () => void)(
() => undefined,
),
).toBe("function");
expect(lazyModule.loadedSpecifiers).toContain(
path.join(packageRoot, "dist", "diagnostic-events-W3Hz61fI.js"),
);
expect(lazyModule.loadedSpecifiers).not.toContain(
path.join(packageRoot, "src", "infra", "diagnostic-events.ts"),
);
});
it("forwards delegateCompactionToRuntime through the compat-backed root alias", () => {
const delegateCompactionToRuntime = () => "delegated";
const lazyModule = loadRootAliasWithStubs({