

























@@ -144,6 +144,8 @@ export type BundledEntryModuleLoadOptions = {
144144145145const nodeRequire = createRequire(import.meta.url);
146146const moduleLoaders: PluginModuleLoaderCache = new Map();
147+const entryBoundaryInfoCache = new Map<string, BundledEntryBoundaryInfo>();
148+const resolvedModulePaths = new Map<string, string>();
147149const loadedModuleExports = new Map<string, unknown>();
148150const disableBundledEntrySourceFallbackEnv = "OPENCLAW_DISABLE_BUNDLED_ENTRY_SOURCE_FALLBACK";
149151@@ -174,6 +176,38 @@ type BundledEntryModuleCandidate = {
174176boundaryRoot: string;
175177};
176178179+type BundledEntryBoundaryInfo = {
180+importerPath: string;
181+importerDir: string;
182+boundaryRoot: string;
183+packageRoot: string | null;
184+};
185+186+function resolveBundledEntryBoundaryInfo(importMetaUrl: string): BundledEntryBoundaryInfo {
187+const cacheKey = `${process.argv[1] ?? ""}\0${importMetaUrl}`;
188+const cached = entryBoundaryInfoCache.get(cacheKey);
189+if (cached) {
190+return cached;
191+}
192+const importerPath = fileURLToPath(importMetaUrl);
193+const importerDir = path.dirname(importerPath);
194+const boundaryRoot = path.dirname(importerPath);
195+const info = {
196+ importerPath,
197+ importerDir,
198+ boundaryRoot,
199+packageRoot:
200+resolveLoaderPackageRoot({
201+modulePath: importerPath,
202+moduleUrl: importMetaUrl,
203+cwd: importerDir,
204+argv1: process.argv[1],
205+}) ?? null,
206+};
207+entryBoundaryInfoCache.set(cacheKey, info);
208+return info;
209+}
210+177211function addBundledEntryCandidates(
178212candidates: BundledEntryModuleCandidate[],
179213basePath: string,
@@ -193,9 +227,8 @@ function resolveBundledEntryModuleCandidates(
193227importMetaUrl: string,
194228specifier: string,
195229): BundledEntryModuleCandidate[] {
196-const importerPath = fileURLToPath(importMetaUrl);
197-const importerDir = path.dirname(importerPath);
198-const boundaryRoot = resolveEntryBoundaryRoot(importMetaUrl);
230+const { importerPath, importerDir, boundaryRoot, packageRoot } =
231+resolveBundledEntryBoundaryInfo(importMetaUrl);
199232const candidates: BundledEntryModuleCandidate[] = [];
200233const primaryResolved = path.resolve(importerDir, specifier);
201234addBundledEntryCandidates(candidates, primaryResolved, boundaryRoot);
@@ -209,12 +242,6 @@ function resolveBundledEntryModuleCandidates(
209242);
210243}
211244212-const packageRoot = resolveLoaderPackageRoot({
213-modulePath: importerPath,
214-moduleUrl: importMetaUrl,
215-cwd: importerDir,
216-argv1: process.argv[1],
217-});
218245if (!packageRoot) {
219246return candidates;
220247}
@@ -282,7 +309,17 @@ function formatBundledEntryModuleOpenFailure(params: {
282309].join(" ");
283310}
284311312+function createBundledEntryModulePathCacheKey(importMetaUrl: string, specifier: string): string {
313+const sourceFallbackDisabled = isTruthyEnvFlag(process.env[disableBundledEntrySourceFallbackEnv]);
314+return `${sourceFallbackDisabled ? "1" : "0"}\0${importMetaUrl}\0${specifier}`;
315+}
316+285317function resolveBundledEntryModulePath(importMetaUrl: string, specifier: string): string {
318+const cacheKey = createBundledEntryModulePathCacheKey(importMetaUrl, specifier);
319+const cached = resolvedModulePaths.get(cacheKey);
320+if (cached) {
321+return cached;
322+}
286323const candidates = resolveBundledEntryModuleCandidates(importMetaUrl, specifier);
287324const fallbackCandidate = candidates[0] ?? {
288325path: path.resolve(path.dirname(fileURLToPath(importMetaUrl)), specifier),
@@ -304,6 +341,7 @@ function resolveBundledEntryModulePath(importMetaUrl: string, specifier: string)
304341});
305342if (opened.ok) {
306343fs.closeSync(opened.fd);
344+resolvedModulePaths.set(cacheKey, opened.path);
307345return opened.path;
308346}
309347firstFailure ??= { candidate, failure: opened };
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。