

























@@ -58,11 +58,9 @@ export function listStaticImportSpecifiers(source) {
5858return [...source.matchAll(STATIC_IMPORT_RE)].map((match) => match.groups?.specifier ?? "");
5959}
606061-export function collectCliBootstrapExternalImportErrors(params = {}) {
62-const rootDir = params.rootDir ?? process.cwd();
63-const entrypoints = params.entrypoints ?? DEFAULT_ENTRYPOINTS;
64-const fsImpl = params.fs ?? fs;
65-const queue = entrypoints.map((entrypoint) => path.resolve(rootDir, entrypoint));
61+function walkStaticImportGraph(params) {
62+const { fsImpl, rootDir } = params;
63+const queue = params.roots.map((entrypoint) => path.resolve(rootDir, entrypoint));
6664const visited = new Set();
6765const errors = [];
6866@@ -82,18 +80,12 @@ export function collectCliBootstrapExternalImportErrors(params = {}) {
8280);
8381continue;
8482}
85-8683for (const specifier of listStaticImportSpecifiers(source)) {
8784if (!specifier || isBuiltinSpecifier(specifier)) {
8885continue;
8986}
9087if (!isRelativeSpecifier(specifier)) {
91-errors.push(
92-`CLI bootstrap static graph imports external package "${specifier}" from ${path.relative(
93- rootDir,
94- filePath,
95- )}.`,
96-);
88+params.onExternalSpecifier?.({ filePath, specifier, errors });
9789continue;
9890}
9991const resolved = resolveRelativeImport(filePath, specifier, fsImpl);
@@ -106,12 +98,34 @@ export function collectCliBootstrapExternalImportErrors(params = {}) {
10698);
10799continue;
108100}
101+params.onRelativeSpecifier?.({ filePath, resolved, specifier, errors });
109102if (!visited.has(resolved)) {
110103queue.push(resolved);
111104}
112105}
113106}
114107108+return errors;
109+}
110+111+export function collectCliBootstrapExternalImportErrors(params = {}) {
112+const rootDir = params.rootDir ?? process.cwd();
113+const entrypoints = params.entrypoints ?? DEFAULT_ENTRYPOINTS;
114+const fsImpl = params.fs ?? fs;
115+const errors = walkStaticImportGraph({
116+ fsImpl,
117+ rootDir,
118+roots: entrypoints,
119+onExternalSpecifier: ({ filePath, specifier, errors: graphErrors }) => {
120+graphErrors.push(
121+`CLI bootstrap static graph imports external package "${specifier}" from ${path.relative(
122+ rootDir,
123+ filePath,
124+ )}.`,
125+);
126+},
127+});
128+115129return errors.toSorted((left, right) => left.localeCompare(right));
116130}
117131@@ -176,15 +190,32 @@ export function collectGatewayRunChunkBudgetErrors(params = {}) {
176190);
177191}
178192179-for (const specifier of listStaticImportSpecifiers(source)) {
180-for (const forbidden of GATEWAY_RUN_FORBIDDEN_STATIC_IMPORTS) {
181-if (specifier.includes(forbidden)) {
182-errors.push(
183-`Gateway run chunk ${relativePath} statically imports cold path "${specifier}".`,
193+errors.push(
194+ ...walkStaticImportGraph({
195+ fsImpl,
196+ rootDir,
197+roots: [filePath],
198+onRelativeSpecifier: ({
199+filePath: importerPath,
200+ resolved,
201+ specifier,
202+errors: graphErrors,
203+}) => {
204+const resolvedRelativePath = path.relative(rootDir, resolved) || resolved;
205+const coldPath = [specifier, resolvedRelativePath].find((candidate) =>
206+GATEWAY_RUN_FORBIDDEN_STATIC_IMPORTS.some((forbidden) => candidate.includes(forbidden)),
184207);
185-}
186-}
187-}
208+if (!coldPath) {
209+return;
210+}
211+graphErrors.push(
212+`Gateway run chunk ${relativePath} static graph imports cold path "${coldPath}" from ${
213+ path.relative(rootDir, importerPath) || importerPath
214+ }.`,
215+);
216+},
217+}),
218+);
188219}
189220190221return errors.toSorted((left, right) => left.localeCompare(right));
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。