perf: prefer bundled plugin dist entries

This commit is contained in:
Peter Steinberger
2026-05-29 02:47:12 +01:00
parent d33c2eefce
commit d6c76eb5bf
4 changed files with 252 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
listBundledChannelPluginMetadata,
resolveBundledChannelGeneratedPath,
resolveBundledChannelWorkspacePath,
} from "./bundled-channel-runtime.js";
@@ -39,4 +40,46 @@ describe("bundled channel runtime metadata", () => {
listBundledChannelPluginMetadata({ rootDir: tempRoot, scanDir: missingScanDir }),
).toStrictEqual([]);
});
it("prefers package-local dist entries over source checkout channel entries", () => {
const tempRoot = createTempRoot();
const pluginRoot = path.join(tempRoot, "extensions", "slack");
fs.mkdirSync(path.join(pluginRoot, "dist"), { recursive: true });
fs.writeFileSync(path.join(pluginRoot, "index.ts"), "export default {};\n", "utf8");
fs.writeFileSync(path.join(pluginRoot, "dist", "index.js"), "export default {};\n", "utf8");
expect(
resolveBundledChannelGeneratedPath(
tempRoot,
{
source: "./index.ts",
built: "index.js",
},
"slack",
path.join(tempRoot, "extensions"),
),
).toBe(path.join(pluginRoot, "dist", "index.js"));
});
it("prefers package-local dist entries for absolute installed registry sources", () => {
const tempRoot = createTempRoot();
const pluginRoot = path.join(tempRoot, "extensions", "slack");
const builtScanRoot = path.join(tempRoot, "dist", "extensions");
fs.mkdirSync(path.join(pluginRoot, "dist"), { recursive: true });
fs.mkdirSync(path.join(builtScanRoot, "slack"), { recursive: true });
fs.writeFileSync(path.join(pluginRoot, "index.ts"), "export default {};\n", "utf8");
fs.writeFileSync(path.join(pluginRoot, "dist", "index.js"), "export default {};\n", "utf8");
expect(
resolveBundledChannelGeneratedPath(
tempRoot,
{
source: path.join(pluginRoot, "index.ts"),
built: path.join(pluginRoot, "index.ts"),
},
"slack",
builtScanRoot,
),
).toBe(path.join(pluginRoot, "dist", "index.js"));
});
});

View File

@@ -229,15 +229,73 @@ function listBundledPluginEntryBaseDirs(params: {
pluginDirName?: string;
scanDir?: string;
}): string[] {
const scanPluginRoot = params.scanDir
? path.resolve(params.scanDir, params.pluginDirName ?? "")
: undefined;
const baseDirs = [
...(params.scanDir ? [path.resolve(params.scanDir, params.pluginDirName ?? "")] : []),
...(scanPluginRoot ? [path.resolve(scanPluginRoot, "dist")] : []),
...(scanPluginRoot ? [scanPluginRoot] : []),
path.resolve(params.rootDir, "dist", "extensions", params.pluginDirName ?? ""),
path.resolve(params.rootDir, "dist-runtime", "extensions", params.pluginDirName ?? ""),
path.resolve(params.rootDir, "extensions", params.pluginDirName ?? "", "dist"),
path.resolve(params.rootDir, "extensions", params.pluginDirName ?? ""),
];
return uniqueStrings(baseDirs);
}
function isPathInsideRoot(rootDir: string, targetPath: string): boolean {
const relative = path.relative(rootDir, targetPath);
return relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
}
function listBundledPluginEntryRoots(params: {
rootDir: string;
pluginDirName?: string;
scanDir?: string;
}): string[] {
const roots = [
...(params.scanDir ? [path.resolve(params.scanDir, params.pluginDirName ?? "")] : []),
path.resolve(params.rootDir, "extensions", params.pluginDirName ?? ""),
path.resolve(params.rootDir, "dist", "extensions", params.pluginDirName ?? ""),
path.resolve(params.rootDir, "dist-runtime", "extensions", params.pluginDirName ?? ""),
];
return uniqueStrings(roots);
}
function listBundledPluginEntrySearchPaths(
entry: BundledPluginPathPair,
params: {
rootDir: string;
pluginDirName?: string;
scanDir?: string;
},
): string[] {
const paths: string[] = [];
const roots = listBundledPluginEntryRoots(params);
for (const rawEntry of [entry.built, entry.source]) {
if (typeof rawEntry !== "string" || rawEntry.length === 0) {
continue;
}
if (!path.isAbsolute(rawEntry)) {
paths.push(rawEntry);
continue;
}
const normalizedEntry = path.normalize(rawEntry);
for (const root of roots) {
if (!isPathInsideRoot(root, normalizedEntry)) {
continue;
}
const relativeEntry = path.relative(root, normalizedEntry);
const builtEntry = rewriteBundledPluginEntryToBuiltPath(relativeEntry);
if (builtEntry) {
paths.push(builtEntry);
}
paths.push(relativeEntry);
}
}
return uniqueStrings(paths);
}
export function resolveBundledPluginGeneratedPath(
rootDir: string,
entry: BundledPluginPathPair | undefined,
@@ -247,9 +305,11 @@ export function resolveBundledPluginGeneratedPath(
if (!entry) {
return null;
}
const entryOrder = [entry.built, entry.source].filter(
(candidate): candidate is string => typeof candidate === "string" && candidate.length > 0,
);
const entryOrder = listBundledPluginEntrySearchPaths(entry, {
rootDir,
pluginDirName,
...(scanDir ? { scanDir } : {}),
});
const baseDirs = listBundledPluginEntryBaseDirs({
rootDir,
pluginDirName,