File size: 7,039 Bytes
94786da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts
index 71e1ce168e49e76f0c27b110953453a3dff0b70d..aa9d289830a22bc2271ee219b4755aed10c1fd52 100644
--- a/packages/next/src/build/index.ts
+++ b/packages/next/src/build/index.ts
@@ -10,6 +10,7 @@ import type { CacheControl, Revalidate } from '../server/lib/cache-control'
 import type { PrefetchHints } from '../shared/lib/app-router-types'
 
 import '../lib/setup-exception-listeners'
+import { resolveBuildPaths } from '../lib/resolve-build-paths'
 
 import { loadEnvConfig, type LoadedEnvFiles } from '@next/env'
 import { bold, yellow } from '../lib/picocolors'
@@ -937,7 +938,7 @@ export default async function build(
   bundler = Bundler.Turbopack,
   experimentalBuildMode: 'default' | 'compile' | 'generate' | 'generate-env',
   traceUploadUrl: string | undefined,
-  debugBuildPaths: { app: string[]; pages: string[] } | undefined,
+  debugBuildPathsPatterns: string[] | undefined,
   enabledFeatures: Record<string, unknown> = {}
 ): Promise<void> {
   const isCompileMode = experimentalBuildMode === 'compile'
@@ -977,7 +978,6 @@ export default async function build(
     NextBuildContext.reactProductionProfiling = reactProductionProfiling
     NextBuildContext.noMangling = noMangling
     NextBuildContext.debugPrerender = debugPrerender
-    NextBuildContext.debugBuildPaths = debugBuildPaths
 
     await nextBuildSpan.traceAsyncFn(async () => {
       // attempt to load global env values so they are available in next.config.js
@@ -1018,6 +1018,19 @@ export default async function build(
         )
       loadedConfig = config
 
+      // Resolve selective build paths now that the page extensions are known.
+      const debugBuildPaths = debugBuildPathsPatterns
+        ? await (async () => {
+            const resolved = await resolveBuildPaths(
+              debugBuildPathsPatterns,
+              dir,
+              config.pageExtensions
+            )
+            return { app: resolved.appPaths, pages: resolved.pagePaths }
+          })()
+        : undefined
+      NextBuildContext.debugBuildPaths = debugBuildPaths
+
       // Validate deploymentId if provided
       if (config.deploymentId !== undefined) {
         if (typeof config.deploymentId !== 'string') {
diff --git a/packages/next/src/cli/next-build.ts b/packages/next/src/cli/next-build.ts
index bc2ac3537e3cfa4b75301538f7a8ed2f8626c480..a0e3cdcdb3f41814f80e511c82cad7d4a5533822 100755
--- a/packages/next/src/cli/next-build.ts
+++ b/packages/next/src/cli/next-build.ts
@@ -11,10 +11,7 @@ import { getProjectDir } from '../lib/get-project-dir'
 import { enableMemoryDebuggingMode } from '../lib/memory/startup'
 import { disableMemoryDebuggingMode } from '../lib/memory/shutdown'
 import { Bundler, parseBundlerArgs } from '../lib/bundler'
-import {
-  resolveBuildPaths,
-  parseBuildPathsInput,
-} from '../lib/resolve-build-paths'
+import { parseBuildPathsInput } from '../lib/resolve-build-paths'
 
 export type NextBuildOptions = {
   experimentalAnalyze?: boolean
@@ -104,24 +101,13 @@ const nextBuild = async (options: NextBuildOptions, directory?: string) => {
     printAndExit(`> No such directory exists as the project root: ${dir}`)
   }
 
-  // Resolve selective build paths
-  let resolvedBuildPaths: { app: string[]; pages: string[] } | undefined
+  let debugBuildPathsPatterns: string[] | undefined
 
   if (debugBuildPaths) {
-    try {
-      const patterns = parseBuildPathsInput(debugBuildPaths)
-
-      if (patterns.length > 0) {
-        const resolved = await resolveBuildPaths(patterns, dir)
-        resolvedBuildPaths = {
-          app: resolved.appPaths,
-          pages: resolved.pagePaths,
-        }
-      }
-    } catch (err) {
-      printAndExit(
-        `Failed to resolve build paths: ${isError(err) ? err.message : String(err)}`
-      )
+    const patterns = parseBuildPathsInput(debugBuildPaths)
+
+    if (patterns.length > 0) {
+      debugBuildPathsPatterns = patterns
     }
   }
 
@@ -145,7 +131,7 @@ const nextBuild = async (options: NextBuildOptions, directory?: string) => {
     bundler,
     experimentalBuildMode,
     traceUploadUrl,
-    resolvedBuildPaths,
+    debugBuildPathsPatterns,
     enabledFeatures
   )
     .catch((err) => {
diff --git a/packages/next/src/lib/resolve-build-paths.ts b/packages/next/src/lib/resolve-build-paths.ts
index 9ffb4c70b7bc044c7eb58fe652b79625afe264e1..b8759df8a0b7d7a85bc19c238c9095e86ed929ad 100644
--- a/packages/next/src/lib/resolve-build-paths.ts
+++ b/packages/next/src/lib/resolve-build-paths.ts
@@ -4,6 +4,8 @@ import * as Log from '../build/output/log'
 import path from 'path'
 import fs from 'fs'
 import isError from './is-error'
+import { createValidFileMatcher } from '../server/lib/find-page-file'
+import type { PageExtensions } from '../build/page-extensions-type'
 
 const glob = promisify(globOriginal)
 
@@ -35,10 +37,12 @@ function escapeBrackets(pattern: string): string {
  */
 export async function resolveBuildPaths(
   patterns: string[],
-  projectDir: string
+  projectDir: string,
+  pageExtensions: PageExtensions
 ): Promise<ResolvedBuildPaths> {
   const appPaths: Set<string> = new Set()
   const pagePaths: Set<string> = new Set()
+  const validFileMatcher = createValidFileMatcher(pageExtensions, undefined)
 
   // Detect whether the project keeps its routes under `src/` so we can accept
   // patterns written with or without that prefix (e.g. both `app/foo/page.tsx`
@@ -89,7 +93,7 @@ export async function resolveBuildPaths(
 
     for (const file of matches) {
       if (!fs.statSync(path.join(projectDir, file)).isDirectory()) {
-        categorizeAndAddPath(file, appPaths, pagePaths)
+        categorizeAndAddPath(file, appPaths, pagePaths, validFileMatcher)
       }
     }
   } catch (error) {
@@ -128,7 +132,7 @@ function addSrcPrefixIfNeeded(
 
 /**
  * Categorizes a file path to either app or pages router based on its prefix.
- * For app router, only route-defining files (page.*, route.*) are included.
+ * For app router, only route-defining files are included.
  *
  * Accepts both top-level (`app/...`, `pages/...`) and src-prefixed
  * (`src/app/...`, `src/pages/...`) project structures.
@@ -142,7 +146,8 @@ function addSrcPrefixIfNeeded(
 function categorizeAndAddPath(
   filePath: string,
   appPaths: Set<string>,
-  pagePaths: Set<string>
+  pagePaths: Set<string>,
+  validFileMatcher: ReturnType<typeof createValidFileMatcher>
 ): void {
   let normalized = filePath.replace(/\\/g, '/')
 
@@ -151,9 +156,9 @@ function categorizeAndAddPath(
   }
 
   if (normalized.startsWith('app/')) {
-    // Only include route-defining files (page.* or route.*)
-    if (/\/(page|route)\.[^/]+$/.test(normalized)) {
-      appPaths.add('/' + normalized.slice(4))
+    const appRelativePath = '/' + normalized.slice(4)
+    if (validFileMatcher.isAppRouterPage(appRelativePath)) {
+      appPaths.add(appRelativePath)
     }
   } else if (normalized.startsWith('pages/')) {
     pagePaths.add('/' + normalized.slice(6))