From bd83f8a844d85cbe5eda5db114e0210c841494c6 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 2 May 2026 17:05:23 +0100 Subject: [PATCH] perf(plugins): prefer built bundled extensions --- src/plugins/bundled-dir.test.ts | 8 ++++---- src/plugins/bundled-dir.ts | 16 +++++++++------- src/plugins/source-checkout-runtime.test.ts | 8 +++++--- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/plugins/bundled-dir.test.ts b/src/plugins/bundled-dir.test.ts index 371deb0d965..401a5bfd917 100644 --- a/src/plugins/bundled-dir.test.ts +++ b/src/plugins/bundled-dir.test.ts @@ -199,7 +199,7 @@ describe("resolveBundledPluginsDir", () => { }, ], [ - "prefers source extensions in a pnpm git checkout outside vitest", + "prefers built dist/extensions in a pnpm git checkout outside vitest", { prefix: "openclaw-bundled-dir-git-built-", hasExtensions: true, @@ -210,7 +210,7 @@ describe("resolveBundledPluginsDir", () => { hasPnpmWorkspace: true, }, { - expectedRelativeDir: "extensions", + expectedRelativeDir: path.join("dist", "extensions"), }, ], [ @@ -227,7 +227,7 @@ describe("resolveBundledPluginsDir", () => { }, ], [ - "still prefers source extensions during tsx-driven pnpm source execution", + "prefers built dist/extensions during tsx-driven pnpm source execution", { prefix: "openclaw-bundled-dir-tsx-built-", hasExtensions: true, @@ -238,7 +238,7 @@ describe("resolveBundledPluginsDir", () => { hasPnpmWorkspace: true, }, { - expectedRelativeDir: "extensions", + expectedRelativeDir: path.join("dist", "extensions"), execArgv: ["--import", "tsx"], }, ], diff --git a/src/plugins/bundled-dir.ts b/src/plugins/bundled-dir.ts index d084a040e02..3c6475f2ad0 100644 --- a/src/plugins/bundled-dir.ts +++ b/src/plugins/bundled-dir.ts @@ -167,13 +167,9 @@ function resolveBundledDirFromPackageRoot(packageRoot: string): string | undefin const builtExtensionsDir = path.join(packageRoot, "dist", "extensions"); const sourceCheckout = isSourceCheckoutRoot(packageRoot); const hasUsableSourceTree = sourceCheckout && hasUsableBundledPluginTree(sourceExtensionsDir); - // In pnpm source checkouts, extensions/* is a workspace package tree with its - // own package.json dependencies. Prefer it so git checkouts remain editable - // and dependency-complete without moving optional plugin deps back into root. - if (hasUsableSourceTree) { - return sourceExtensionsDir; - } - + // In pnpm source checkouts, prefer the built bundled plugin runtime when it + // exists so dist gateway runs avoid loading TS plugin entrypoints through jiti. + // Keep the source tree as the fallback for fresh checkouts before build. const runtimeExtensionsDir = path.join(packageRoot, "dist-runtime", "extensions"); const hasUsableRuntimeTree = sourceCheckout ? hasUsableBundledPluginTree(runtimeExtensionsDir) @@ -181,6 +177,12 @@ function resolveBundledDirFromPackageRoot(packageRoot: string): string | undefin const hasUsableBuiltTree = sourceCheckout ? hasUsableBundledPluginTree(builtExtensionsDir) : fs.existsSync(builtExtensionsDir); + if (sourceCheckout && hasUsableBuiltTree) { + return builtExtensionsDir; + } + if (sourceCheckout && hasUsableRuntimeTree) { + return runtimeExtensionsDir; + } if (hasUsableRuntimeTree && hasUsableBuiltTree) { return runtimeExtensionsDir; } diff --git a/src/plugins/source-checkout-runtime.test.ts b/src/plugins/source-checkout-runtime.test.ts index 6f839915e2c..ee1882143dc 100644 --- a/src/plugins/source-checkout-runtime.test.ts +++ b/src/plugins/source-checkout-runtime.test.ts @@ -3,7 +3,7 @@ import { describe, expect, it } from "vitest"; import { loadOpenClawPlugins } from "./loader.js"; describe("source checkout bundled plugin runtime", () => { - it("loads enabled bundled plugins from the pnpm workspace source tree", () => { + it("loads enabled bundled plugins from built dist when available", () => { const registry = loadOpenClawPlugins({ cache: false, onlyPluginIds: ["twitch"], @@ -21,7 +21,9 @@ describe("source checkout bundled plugin runtime", () => { status: "loaded", origin: "bundled", }); - expect(twitch?.source).toContain(`${path.sep}extensions${path.sep}twitch${path.sep}index.ts`); - expect(twitch?.rootDir).toContain(`${path.sep}extensions${path.sep}twitch`); + expect(twitch?.source).toContain( + `${path.sep}dist${path.sep}extensions${path.sep}twitch${path.sep}index.js`, + ); + expect(twitch?.rootDir).toContain(`${path.sep}dist${path.sep}extensions${path.sep}twitch`); }); });