Skip to content

Commit 4eee0e3

Browse files
timneutkensijjkhuozhi
authored
Update findPagesDir (#36619)
Co-authored-by: JJ Kasper <[email protected]> Co-authored-by: Jiachi Liu <[email protected]>
1 parent 6bb0e91 commit 4eee0e3

File tree

8 files changed

+68
-30
lines changed

8 files changed

+68
-30
lines changed

packages/next/build/entries.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { __ApiPreviewProps } from '../server/api-utils'
1717
import { isTargetLikeServerless } from '../server/utils'
1818
import { warn } from './output/log'
1919
import { parse } from '../build/swc'
20-
import { isFlightPage, withoutRSCExtensions } from './utils'
20+
import { isServerComponentPage, withoutRSCExtensions } from './utils'
2121
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
2222
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
2323

@@ -251,7 +251,10 @@ export function getEdgeServerEntry(opts: {
251251
absolutePagePath: opts.absolutePagePath,
252252
buildId: opts.buildId,
253253
dev: opts.isDev,
254-
isServerComponent: isFlightPage(opts.config, opts.absolutePagePath),
254+
isServerComponent: isServerComponentPage(
255+
opts.config,
256+
opts.absolutePagePath
257+
),
255258
page: opts.page,
256259
stringifiedConfig: JSON.stringify(opts.config),
257260
}

packages/next/build/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ import {
100100
copyTracedFiles,
101101
isReservedPage,
102102
isCustomErrorPage,
103-
isFlightPage,
103+
isServerComponentPage,
104104
} from './utils'
105105
import getBaseWebpackConfig from './webpack-config'
106106
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
@@ -196,7 +196,11 @@ export default async function build(
196196
setGlobal('telemetry', telemetry)
197197

198198
const publicDir = path.join(dir, 'public')
199-
const pagesDir = findPagesDir(dir)
199+
const { pages: pagesDir, root: rootDir } = findPagesDir(
200+
dir,
201+
config.experimental.rootDir
202+
)
203+
200204
const hasPublicDir = await fileExists(publicDir)
201205

202206
telemetry.record(
@@ -230,7 +234,7 @@ export default async function build(
230234
.traceAsyncFn(() =>
231235
verifyTypeScriptSetup(
232236
dir,
233-
pagesDir,
237+
[pagesDir, rootDir].filter(Boolean) as string[],
234238
!ignoreTypeScriptErrors,
235239
config,
236240
cacheDir
@@ -973,7 +977,7 @@ export default async function build(
973977
: undefined
974978

975979
if (hasServerComponents && pagePath) {
976-
if (isFlightPage(config, pagePath)) {
980+
if (isServerComponentPage(config, pagePath)) {
977981
isServerComponent = true
978982
}
979983
}

packages/next/build/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ export function withoutRSCExtensions(pageExtensions: string[]): string[] {
11011101
)
11021102
}
11031103

1104-
export function isFlightPage(
1104+
export function isServerComponentPage(
11051105
nextConfig: NextConfigComplete,
11061106
filePath: string
11071107
): boolean {

packages/next/lib/eslint/runLintCheck.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ async function lint(
171171
}
172172
}
173173

174-
const pagesDir = findPagesDir(baseDir)
174+
// TODO: should we apply these rules to "root" dir as well?
175+
const pagesDir = findPagesDir(baseDir).pages
175176

176177
if (nextEslintPluginIsEnabled) {
177178
let updatedPagesDir = false

packages/next/lib/find-pages-dir.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,37 @@ export const existsSync = (f: string): boolean => {
1010
}
1111
}
1212

13-
export function findPagesDir(dir: string): string {
14-
// prioritize ./pages over ./src/pages
15-
let curDir = path.join(dir, 'pages')
13+
function findDir(dir: string, name: 'pages' | 'root'): string | null {
14+
// prioritize ./${name} over ./src/${name}
15+
let curDir = path.join(dir, name)
1616
if (existsSync(curDir)) return curDir
1717

18-
curDir = path.join(dir, 'src/pages')
18+
curDir = path.join(dir, 'src', name)
1919
if (existsSync(curDir)) return curDir
2020

21-
// Check one level up the tree to see if the pages directory might be there
22-
if (existsSync(path.join(dir, '..', 'pages'))) {
21+
return null
22+
}
23+
24+
export function findPagesDir(
25+
dir: string,
26+
root?: boolean
27+
): { pages: string; root?: string } {
28+
const pagesDir = findDir(dir, 'pages')
29+
let rootDir: undefined | string
30+
31+
if (root) {
32+
rootDir = findDir(dir, 'root') || undefined
33+
}
34+
35+
// TODO: allow "root" dir without pages dir
36+
if (pagesDir === null) {
2337
throw new Error(
24-
'> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?'
38+
"> Couldn't find a `pages` directory. Please create one under the project root"
2539
)
2640
}
2741

28-
throw new Error(
29-
"> Couldn't find a `pages` directory. Please create one under the project root"
30-
)
42+
return {
43+
pages: pagesDir,
44+
root: rootDir,
45+
}
3146
}

packages/next/lib/typescript/getTypeScriptIntent.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type TypeScriptIntent = { firstTimeSetup: boolean }
88

99
export async function getTypeScriptIntent(
1010
baseDir: string,
11-
pagesDir: string,
11+
intentDirs: string[],
1212
config: NextConfigComplete
1313
): Promise<TypeScriptIntent | false> {
1414
const tsConfigPath = path.join(baseDir, config.typescript.tsconfigPath)
@@ -28,13 +28,15 @@ export async function getTypeScriptIntent(
2828
// project for the user when we detect TypeScript files. So, we need to check
2929
// the `pages/` directory for a TypeScript file.
3030
// Checking all directories is too slow, so this is a happy medium.
31-
const typescriptFiles = await recursiveReadDir(
32-
pagesDir,
33-
/.*\.(ts|tsx)$/,
34-
/(node_modules|.*\.d\.ts)/
35-
)
36-
if (typescriptFiles.length) {
37-
return { firstTimeSetup: true }
31+
for (const dir of intentDirs) {
32+
const typescriptFiles = await recursiveReadDir(
33+
dir,
34+
/.*\.(ts|tsx)$/,
35+
/(node_modules|.*\.d\.ts)/
36+
)
37+
if (typescriptFiles.length) {
38+
return { firstTimeSetup: true }
39+
}
3840
}
3941

4042
return false

packages/next/lib/verifyTypeScriptSetup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const requiredPackages = [
3232

3333
export async function verifyTypeScriptSetup(
3434
dir: string,
35-
pagesDir: string,
35+
intentDirs: string[],
3636
typeCheckPreflight: boolean,
3737
config: NextConfigComplete,
3838
cacheDir?: string
@@ -41,7 +41,7 @@ export async function verifyTypeScriptSetup(
4141

4242
try {
4343
// Check if the project uses TypeScript:
44-
const intent = await getTypeScriptIntent(dir, pagesDir, config)
44+
const intent = await getTypeScriptIntent(dir, intentDirs, config)
4545
if (!intent) {
4646
return { version: null }
4747
}

packages/next/server/dev/next-dev-server.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ export default class DevServer extends Server {
9696
protected sortedRoutes?: string[]
9797
private addedUpgradeListener = false
9898
private pagesDir: string
99+
// @ts-ignore TODO: add implementation
100+
private rootDir?: string
99101

100102
protected staticPathsWorker?: { [key: string]: any } & {
101103
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
@@ -175,7 +177,13 @@ export default class DevServer extends Server {
175177
}
176178

177179
this.isCustomServer = !options.isNextDevCommand
178-
this.pagesDir = findPagesDir(this.dir)
180+
// TODO: hot-reload root/pages dirs?
181+
const { pages: pagesDir, root: rootDir } = findPagesDir(
182+
this.dir,
183+
this.nextConfig.experimental.rootDir
184+
)
185+
this.pagesDir = pagesDir
186+
this.rootDir = rootDir
179187
}
180188

181189
protected getBuildId(): string {
@@ -361,7 +369,12 @@ export default class DevServer extends Server {
361369
async prepare(): Promise<void> {
362370
setGlobal('distDir', this.distDir)
363371
setGlobal('phase', PHASE_DEVELOPMENT_SERVER)
364-
await verifyTypeScriptSetup(this.dir, this.pagesDir, false, this.nextConfig)
372+
await verifyTypeScriptSetup(
373+
this.dir,
374+
[this.pagesDir!, this.rootDir].filter(Boolean) as string[],
375+
false,
376+
this.nextConfig
377+
)
365378

366379
this.customRoutes = await loadCustomRoutes(this.nextConfig)
367380

0 commit comments

Comments
 (0)