|
| 1 | +import { promisify } from 'util' |
| 2 | +import globOriginal from 'next/dist/compiled/glob' |
| 3 | +import * as Log from '../build/output/log' |
| 4 | +import path from 'path' |
| 5 | +import fs from 'fs' |
| 6 | +import isError from './is-error' |
| 7 | + |
| 8 | +const glob = promisify(globOriginal) |
| 9 | + |
| 10 | +interface ResolvedBuildPaths { |
| 11 | + appPaths: string[] |
| 12 | + pagePaths: string[] |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Resolves glob patterns and explicit paths to actual file paths |
| 17 | + * Categorizes them into App Router and Pages Router paths |
| 18 | + * |
| 19 | + * @param patterns - Array of glob patterns or explicit paths |
| 20 | + * @param projectDir - Root project directory |
| 21 | + * @returns Object with categorized app and page paths |
| 22 | + */ |
| 23 | +export async function resolveBuildPaths( |
| 24 | + patterns: string[], |
| 25 | + projectDir: string |
| 26 | +): Promise<ResolvedBuildPaths> { |
| 27 | + const appPaths: Set<string> = new Set() |
| 28 | + const pagePaths: Set<string> = new Set() |
| 29 | + |
| 30 | + for (const pattern of patterns) { |
| 31 | + const trimmed = pattern.trim() |
| 32 | + |
| 33 | + if (!trimmed) { |
| 34 | + continue |
| 35 | + } |
| 36 | + |
| 37 | + // Detect if pattern is glob pattern (contains glob special chars) |
| 38 | + const isGlobPattern = /[*?[\]{}!]/.test(trimmed) |
| 39 | + |
| 40 | + if (isGlobPattern) { |
| 41 | + try { |
| 42 | + // Resolve glob pattern |
| 43 | + const matches = (await glob(trimmed, { |
| 44 | + cwd: projectDir, |
| 45 | + })) as string[] |
| 46 | + |
| 47 | + if (matches.length === 0) { |
| 48 | + Log.warn(`Glob pattern "${trimmed}" did not match any files`) |
| 49 | + } |
| 50 | + |
| 51 | + for (const file of matches) { |
| 52 | + // Skip directories, only process files |
| 53 | + if (!fs.statSync(path.join(projectDir, file)).isDirectory()) { |
| 54 | + categorizeAndAddPath(file, appPaths, pagePaths) |
| 55 | + } |
| 56 | + } |
| 57 | + } catch (error) { |
| 58 | + throw new Error( |
| 59 | + `Failed to resolve glob pattern "${trimmed}": ${ |
| 60 | + isError(error) ? error.message : String(error) |
| 61 | + }` |
| 62 | + ) |
| 63 | + } |
| 64 | + } else { |
| 65 | + // Explicit path - categorize based on prefix |
| 66 | + categorizeAndAddPath(trimmed, appPaths, pagePaths, projectDir) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + appPaths: Array.from(appPaths).sort(), |
| 72 | + pagePaths: Array.from(pagePaths).sort(), |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Categorizes a file path to either app or pages router based on its prefix, |
| 78 | + * and normalizes it to the format expected by Next.js internal build system. |
| 79 | + * |
| 80 | + * The internal build system expects: |
| 81 | + * - App router: paths with leading slash (e.g., "/page.tsx", "/dashboard/page.tsx") |
| 82 | + * - Pages router: paths with leading slash (e.g., "/index.tsx", "/about.tsx") |
| 83 | + * |
| 84 | + * Examples: |
| 85 | + * - "app/page.tsx" → appPaths.add("/page.tsx") |
| 86 | + * - "app/dashboard/page.tsx" → appPaths.add("/dashboard/page.tsx") |
| 87 | + * - "pages/index.tsx" → pagePaths.add("/index.tsx") |
| 88 | + * - "pages/about.tsx" → pagePaths.add("/about.tsx") |
| 89 | + * - "/page.tsx" → appPaths.add("/page.tsx") (already in app router format) |
| 90 | + */ |
| 91 | +function categorizeAndAddPath( |
| 92 | + filePath: string, |
| 93 | + appPaths: Set<string>, |
| 94 | + pagePaths: Set<string>, |
| 95 | + projectDir?: string |
| 96 | +): void { |
| 97 | + // Normalize path separators to forward slashes (Windows compatibility) |
| 98 | + const normalized = filePath.replace(/\\/g, '/') |
| 99 | + |
| 100 | + // Skip non-file entries (like directories without extensions) |
| 101 | + if (normalized.endsWith('/')) { |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + if (normalized.startsWith('app/')) { |
| 106 | + // App router path: remove 'app/' prefix and ensure leading slash |
| 107 | + // "app/page.tsx" → "/page.tsx" |
| 108 | + // "app/dashboard/page.tsx" → "/dashboard/page.tsx" |
| 109 | + const withoutPrefix = normalized.slice(4) // Remove "app/" |
| 110 | + appPaths.add('/' + withoutPrefix) |
| 111 | + } else if (normalized.startsWith('pages/')) { |
| 112 | + // Pages router path: remove 'pages/' prefix and add leading slash |
| 113 | + // "pages/index.tsx" → "/index.tsx" |
| 114 | + // "pages/about.tsx" → "/about.tsx" |
| 115 | + const withoutPrefix = normalized.slice(6) // Remove "pages/" |
| 116 | + pagePaths.add('/' + withoutPrefix) |
| 117 | + } else if (normalized.startsWith('/')) { |
| 118 | + // Leading slash suggests app router format (already in correct format) |
| 119 | + // "/page.tsx" → "/page.tsx" (no change needed) |
| 120 | + appPaths.add(normalized) |
| 121 | + } else { |
| 122 | + // No obvious prefix - try to detect based on file existence |
| 123 | + if (projectDir) { |
| 124 | + const appPath = path.join(projectDir, 'app', normalized) |
| 125 | + const pagesPath = path.join(projectDir, 'pages', normalized) |
| 126 | + |
| 127 | + if (fs.existsSync(appPath)) { |
| 128 | + appPaths.add('/' + normalized) |
| 129 | + } else if (fs.existsSync(pagesPath)) { |
| 130 | + pagePaths.add('/' + normalized) |
| 131 | + } else { |
| 132 | + // Default to pages router for paths without clear indicator |
| 133 | + pagePaths.add('/' + normalized) |
| 134 | + } |
| 135 | + } else { |
| 136 | + // Without projectDir context, default to pages router |
| 137 | + pagePaths.add('/' + normalized) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Parse build paths from comma-separated format |
| 144 | + * Supports: |
| 145 | + * - Comma-separated values: "app/page.tsx,app/about/page.tsx" |
| 146 | + * |
| 147 | + * @param input - String input to parse |
| 148 | + * @returns Array of path patterns |
| 149 | + */ |
| 150 | +export function parseBuildPathsInput(input: string): string[] { |
| 151 | + // Comma-separated values |
| 152 | + return input |
| 153 | + .split(',') |
| 154 | + .map((p) => p.trim()) |
| 155 | + .filter((p) => p.length > 0) |
| 156 | +} |
0 commit comments