refactor: share bundled loader Jiti config helpers

This commit is contained in:
Peter Steinberger
2026-04-07 14:12:46 +01:00
parent 2e0354e725
commit df993291b6
7 changed files with 155 additions and 61 deletions

View File

@@ -8,7 +8,8 @@ import { resolveBundledPluginPublicSurfacePath } from "./public-surface-runtime.
import {
buildPluginLoaderAliasMap,
buildPluginLoaderJitiOptions,
resolvePluginLoaderJitiTryNative,
isBundledPluginExtensionPath,
resolvePluginLoaderJitiConfig,
resolveLoaderPackageRoot,
} from "./sdk-alias.js";
@@ -70,18 +71,16 @@ function resolvePublicSurfaceLocation(params: {
}
function getJiti(modulePath: string) {
const tryNative = resolvePluginLoaderJitiTryNative(modulePath, {
const { tryNative, aliasMap, cacheKey } = resolvePluginLoaderJitiConfig({
modulePath,
argv1: process.argv[1],
moduleUrl: import.meta.url,
preferBuiltDist: true,
});
const sharedLoader = getSharedBundledPublicSurfaceJiti(modulePath, tryNative);
if (sharedLoader) {
return sharedLoader;
}
const aliasMap = buildPluginLoaderAliasMap(modulePath, process.argv[1], import.meta.url);
const cacheKey = JSON.stringify({
tryNative,
aliasMap: Object.entries(aliasMap).toSorted(([left], [right]) => left.localeCompare(right)),
});
const cached = jitiLoaders.get(cacheKey);
if (cached) {
return cached;
@@ -99,18 +98,13 @@ function getSharedBundledPublicSurfaceJiti(
tryNative: boolean,
): ReturnType<typeof createJiti> | null {
const bundledPluginsDir = resolveBundledPluginsDir();
const normalizedModulePath = path.resolve(modulePath);
const sharedRoots = [
bundledPluginsDir ? path.resolve(bundledPluginsDir) : null,
path.join(OPENCLAW_PACKAGE_ROOT, "extensions"),
path.join(OPENCLAW_PACKAGE_ROOT, "dist", "extensions"),
path.join(OPENCLAW_PACKAGE_ROOT, "dist-runtime", "extensions"),
].filter((root): root is string => typeof root === "string");
const isBundledPublicSurface = sharedRoots.some(
(root) =>
normalizedModulePath === root || normalizedModulePath.startsWith(`${root}${path.sep}`),
);
if (!isBundledPublicSurface) {
if (
!isBundledPluginExtensionPath({
modulePath,
openClawPackageRoot: OPENCLAW_PACKAGE_ROOT,
...(bundledPluginsDir ? { bundledPluginsDir } : {}),
})
) {
return null;
}
const cacheKey = tryNative ? "bundled:native" : "bundled:source";