(context: ResolutionContext, dir: string, root: string)
| 71 | * ordinary module, so a lone one counts unless the app pins an older Next. |
| 72 | */ |
| 73 | export function nextProxyFor(context: ResolutionContext, dir: string, root: string): NextProxy | null { |
| 74 | const find = (base: string): string | undefined => |
| 75 | PROXY_EXTENSIONS.map((ext) => `${dir}${base}.${ext}`).find((file) => context.fileExists(file)); |
| 76 | const middleware = find('middleware'); |
| 77 | const proxy = middleware ? undefined : find('proxy'); |
| 78 | const file = middleware ?? (proxy && !pinsNextBefore16(context, root) ? proxy : undefined); |
| 79 | if (!file) return null; |
| 80 | |
| 81 | const nodes = context.getNodesInFile(file); |
| 82 | const named = nodes.find((n) => ENTRY_KINDS.has(n.kind) && n.isExported && n.name === (middleware ? 'middleware' : 'proxy')); |
| 83 | if (named) return { file, entry: named }; |
| 84 | |
| 85 | const safe = stripCommentsForRegex(context.readFile(file) ?? '', 'typescript'); |
| 86 | const local = |
| 87 | /\bexport\s+default\s+(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)/.exec(safe)?.[1] ?? |
| 88 | /\bexport\s+default\s+([A-Za-z_$][\w$]*)\s*;?\s*$/m.exec(safe)?.[1] ?? |
| 89 | /\bexport\s*\{[^}]*?\b([A-Za-z_$][\w$]*)\s+as\s+(?:default|middleware|proxy)\b[^}]*\}(?!\s*from\b)/.exec(safe)?.[1]; |
| 90 | const entry = (local ? nodes.find((n) => ENTRY_KINDS.has(n.kind) && n.name === local) : undefined) ?? nodes.find((n) => n.kind === 'file'); |
| 91 | return entry ? { file, entry } : null; |
| 92 | } |
| 93 | |
| 94 | // ============================================================================= |
| 95 | // config.matcher |
no test coverage detected