

























@@ -17,6 +17,14 @@ export type LoaderModuleResolveParams = {
1717pluginSdkResolution?: PluginSdkResolutionPreference;
1818};
191920+export type PluginRuntimeModuleResolution = {
21+modulePath?: string;
22+packageRoot: string | null;
23+candidates: string[];
24+resolvedPath: string | null;
25+error?: string;
26+};
27+2028type PluginSdkPackageJson = {
2129exports?: Record<string, unknown>;
2230bin?: string | Record<string, unknown>;
@@ -148,6 +156,66 @@ export function resolveLoaderPackageRoot(
148156});
149157}
150158159+function createPluginRuntimeModuleCandidateMap(packageRoot: string) {
160+return {
161+src: path.join(packageRoot, "src", "plugins", "runtime", "index.ts"),
162+dist: path.join(packageRoot, "dist", "plugins", "runtime", "index.js"),
163+} as const;
164+}
165+166+function appendPluginRuntimeModuleCandidates(
167+candidates: string[],
168+packageRoot: string,
169+orderedKinds: readonly PluginSdkAliasCandidateKind[],
170+): void {
171+const candidateMap = createPluginRuntimeModuleCandidateMap(packageRoot);
172+for (const kind of orderedKinds) {
173+candidates.push(candidateMap[kind]);
174+}
175+}
176+177+function dedupeResolvedPaths(paths: readonly string[]): string[] {
178+const seen = new Set<string>();
179+const deduped: string[] = [];
180+for (const candidate of paths) {
181+const resolved = path.resolve(candidate);
182+if (seen.has(resolved)) {
183+continue;
184+}
185+seen.add(resolved);
186+deduped.push(resolved);
187+}
188+return deduped;
189+}
190+191+function listAncestorPluginRuntimeModuleCandidates(params: {
192+starts: readonly (string | undefined)[];
193+orderedKinds: readonly PluginSdkAliasCandidateKind[];
194+maxDepth?: number;
195+}): string[] {
196+const candidates: string[] = [];
197+for (const start of params.starts) {
198+if (!start) {
199+continue;
200+}
201+let cursor = path.resolve(start);
202+const maxDepth = params.maxDepth ?? 12;
203+for (let i = 0; i < maxDepth; i += 1) {
204+appendPluginRuntimeModuleCandidates(candidates, cursor, params.orderedKinds);
205+const parent = path.dirname(cursor);
206+if (parent === cursor) {
207+break;
208+}
209+cursor = parent;
210+}
211+}
212+return dedupeResolvedPaths(candidates);
213+}
214+215+function formatResolutionError(error: unknown): string {
216+return error instanceof Error ? error.message : String(error);
217+}
218+151219function resolveLoaderPluginSdkPackageRoot(
152220params: LoaderModuleResolveParams & { modulePath: string },
153221): string | null {
@@ -958,33 +1026,63 @@ export function buildPluginLoaderAliasMap(
9581026export function resolvePluginRuntimeModulePath(
9591027params: LoaderModuleResolveParams = {},
9601028): string | null {
1029+return resolvePluginRuntimeModulePathWithDiagnostics(params).resolvedPath;
1030+}
1031+1032+export function resolvePluginRuntimeModulePathWithDiagnostics(
1033+params: LoaderModuleResolveParams = {},
1034+): PluginRuntimeModuleResolution {
1035+let modulePath: string | undefined;
1036+let packageRoot: string | null = null;
1037+const candidates: string[] = [];
9611038try {
962-const modulePath = resolveLoaderModulePath(params);
1039+modulePath = resolveLoaderModulePath(params);
9631040const orderedKinds = resolvePluginSdkAliasCandidateOrder({
9641041 modulePath,
9651042isProduction: process.env.NODE_ENV === "production",
9661043pluginSdkResolution: params.pluginSdkResolution,
9671044});
968-const packageRoot = resolveLoaderPackageRoot({ ...params, modulePath });
969-const candidates = packageRoot
970- ? orderedKinds.map((kind) =>
971-kind === "src"
972- ? path.join(packageRoot, "src", "plugins", "runtime", "index.ts")
973- : path.join(packageRoot, "dist", "plugins", "runtime", "index.js"),
974-)
975- : [
976-path.join(path.dirname(modulePath), "runtime", "index.ts"),
977-path.join(path.dirname(modulePath), "runtime", "index.js"),
978-];
979-for (const candidate of candidates) {
1045+packageRoot = resolveLoaderPackageRoot({ ...params, modulePath });
1046+if (packageRoot) {
1047+appendPluginRuntimeModuleCandidates(candidates, packageRoot, orderedKinds);
1048+} else {
1049+candidates.push(
1050+ ...listAncestorPluginRuntimeModuleCandidates({
1051+starts: [
1052+path.dirname(modulePath),
1053+params.cwd,
1054+params.argv1 ? path.dirname(params.argv1) : undefined,
1055+],
1056+ orderedKinds,
1057+}),
1058+);
1059+}
1060+const dedupedCandidates = dedupeResolvedPaths(candidates);
1061+for (const candidate of dedupedCandidates) {
9801062if (fs.existsSync(candidate)) {
981-return candidate;
1063+return {
1064+ modulePath,
1065+ packageRoot,
1066+candidates: dedupedCandidates,
1067+resolvedPath: candidate,
1068+};
9821069}
9831070}
984-} catch {
985-// ignore
1071+} catch (error) {
1072+return {
1073+ modulePath,
1074+ packageRoot,
1075+candidates: dedupeResolvedPaths(candidates),
1076+resolvedPath: null,
1077+error: formatResolutionError(error),
1078+};
9861079}
987-return null;
1080+return {
1081+ modulePath,
1082+ packageRoot,
1083+candidates: dedupeResolvedPaths(candidates),
1084+resolvedPath: null,
1085+};
9881086}
98910879901088export function buildPluginLoaderJitiOptions(aliasMap: Record<string, string>) {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。