Unverified Commit 4eee0e32 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Update findPagesDir (#36619)

Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
Co-authored-by: 's avatarJiachi Liu <4800338+huozhi@users.noreply.github.com>
parent 6bb0e91a
...@@ -17,7 +17,7 @@ import { __ApiPreviewProps } from '../server/api-utils' ...@@ -17,7 +17,7 @@ import { __ApiPreviewProps } from '../server/api-utils'
import { isTargetLikeServerless } from '../server/utils' import { isTargetLikeServerless } from '../server/utils'
import { warn } from './output/log' import { warn } from './output/log'
import { parse } from '../build/swc' import { parse } from '../build/swc'
import { isFlightPage, withoutRSCExtensions } from './utils' import { isServerComponentPage, withoutRSCExtensions } from './utils'
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep' import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
...@@ -251,7 +251,10 @@ export function getEdgeServerEntry(opts: { ...@@ -251,7 +251,10 @@ export function getEdgeServerEntry(opts: {
absolutePagePath: opts.absolutePagePath, absolutePagePath: opts.absolutePagePath,
buildId: opts.buildId, buildId: opts.buildId,
dev: opts.isDev, dev: opts.isDev,
isServerComponent: isFlightPage(opts.config, opts.absolutePagePath), isServerComponent: isServerComponentPage(
opts.config,
opts.absolutePagePath
),
page: opts.page, page: opts.page,
stringifiedConfig: JSON.stringify(opts.config), stringifiedConfig: JSON.stringify(opts.config),
} }
......
...@@ -100,7 +100,7 @@ import { ...@@ -100,7 +100,7 @@ import {
copyTracedFiles, copyTracedFiles,
isReservedPage, isReservedPage,
isCustomErrorPage, isCustomErrorPage,
isFlightPage, isServerComponentPage,
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin' import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
...@@ -196,7 +196,11 @@ export default async function build( ...@@ -196,7 +196,11 @@ export default async function build(
setGlobal('telemetry', telemetry) setGlobal('telemetry', telemetry)
const publicDir = path.join(dir, 'public') const publicDir = path.join(dir, 'public')
const pagesDir = findPagesDir(dir) const { pages: pagesDir, root: rootDir } = findPagesDir(
dir,
config.experimental.rootDir
)
const hasPublicDir = await fileExists(publicDir) const hasPublicDir = await fileExists(publicDir)
telemetry.record( telemetry.record(
...@@ -230,7 +234,7 @@ export default async function build( ...@@ -230,7 +234,7 @@ export default async function build(
.traceAsyncFn(() => .traceAsyncFn(() =>
verifyTypeScriptSetup( verifyTypeScriptSetup(
dir, dir,
pagesDir, [pagesDir, rootDir].filter(Boolean) as string[],
!ignoreTypeScriptErrors, !ignoreTypeScriptErrors,
config, config,
cacheDir cacheDir
...@@ -973,7 +977,7 @@ export default async function build( ...@@ -973,7 +977,7 @@ export default async function build(
: undefined : undefined
if (hasServerComponents && pagePath) { if (hasServerComponents && pagePath) {
if (isFlightPage(config, pagePath)) { if (isServerComponentPage(config, pagePath)) {
isServerComponent = true isServerComponent = true
} }
} }
......
...@@ -1101,7 +1101,7 @@ export function withoutRSCExtensions(pageExtensions: string[]): string[] { ...@@ -1101,7 +1101,7 @@ export function withoutRSCExtensions(pageExtensions: string[]): string[] {
) )
} }
export function isFlightPage( export function isServerComponentPage(
nextConfig: NextConfigComplete, nextConfig: NextConfigComplete,
filePath: string filePath: string
): boolean { ): boolean {
......
...@@ -171,7 +171,8 @@ async function lint( ...@@ -171,7 +171,8 @@ async function lint(
} }
} }
const pagesDir = findPagesDir(baseDir) // TODO: should we apply these rules to "root" dir as well?
const pagesDir = findPagesDir(baseDir).pages
if (nextEslintPluginIsEnabled) { if (nextEslintPluginIsEnabled) {
let updatedPagesDir = false let updatedPagesDir = false
......
...@@ -10,22 +10,37 @@ export const existsSync = (f: string): boolean => { ...@@ -10,22 +10,37 @@ export const existsSync = (f: string): boolean => {
} }
} }
export function findPagesDir(dir: string): string { function findDir(dir: string, name: 'pages' | 'root'): string | null {
// prioritize ./pages over ./src/pages // prioritize ./${name} over ./src/${name}
let curDir = path.join(dir, 'pages') let curDir = path.join(dir, name)
if (existsSync(curDir)) return curDir if (existsSync(curDir)) return curDir
curDir = path.join(dir, 'src/pages') curDir = path.join(dir, 'src', name)
if (existsSync(curDir)) return curDir if (existsSync(curDir)) return curDir
// Check one level up the tree to see if the pages directory might be there return null
if (existsSync(path.join(dir, '..', 'pages'))) { }
export function findPagesDir(
dir: string,
root?: boolean
): { pages: string; root?: string } {
const pagesDir = findDir(dir, 'pages')
let rootDir: undefined | string
if (root) {
rootDir = findDir(dir, 'root') || undefined
}
// TODO: allow "root" dir without pages dir
if (pagesDir === null) {
throw new Error( throw new Error(
'> No `pages` directory found. Did you mean to run `next` in the parent (`../`) directory?' "> Couldn't find a `pages` directory. Please create one under the project root"
) )
} }
throw new Error( return {
"> Couldn't find a `pages` directory. Please create one under the project root" pages: pagesDir,
) root: rootDir,
}
} }
...@@ -8,7 +8,7 @@ export type TypeScriptIntent = { firstTimeSetup: boolean } ...@@ -8,7 +8,7 @@ export type TypeScriptIntent = { firstTimeSetup: boolean }
export async function getTypeScriptIntent( export async function getTypeScriptIntent(
baseDir: string, baseDir: string,
pagesDir: string, intentDirs: string[],
config: NextConfigComplete config: NextConfigComplete
): Promise<TypeScriptIntent | false> { ): Promise<TypeScriptIntent | false> {
const tsConfigPath = path.join(baseDir, config.typescript.tsconfigPath) const tsConfigPath = path.join(baseDir, config.typescript.tsconfigPath)
...@@ -28,13 +28,15 @@ export async function getTypeScriptIntent( ...@@ -28,13 +28,15 @@ export async function getTypeScriptIntent(
// project for the user when we detect TypeScript files. So, we need to check // project for the user when we detect TypeScript files. So, we need to check
// the `pages/` directory for a TypeScript file. // the `pages/` directory for a TypeScript file.
// Checking all directories is too slow, so this is a happy medium. // Checking all directories is too slow, so this is a happy medium.
const typescriptFiles = await recursiveReadDir( for (const dir of intentDirs) {
pagesDir, const typescriptFiles = await recursiveReadDir(
/.*\.(ts|tsx)$/, dir,
/(node_modules|.*\.d\.ts)/ /.*\.(ts|tsx)$/,
) /(node_modules|.*\.d\.ts)/
if (typescriptFiles.length) { )
return { firstTimeSetup: true } if (typescriptFiles.length) {
return { firstTimeSetup: true }
}
} }
return false return false
......
...@@ -32,7 +32,7 @@ const requiredPackages = [ ...@@ -32,7 +32,7 @@ const requiredPackages = [
export async function verifyTypeScriptSetup( export async function verifyTypeScriptSetup(
dir: string, dir: string,
pagesDir: string, intentDirs: string[],
typeCheckPreflight: boolean, typeCheckPreflight: boolean,
config: NextConfigComplete, config: NextConfigComplete,
cacheDir?: string cacheDir?: string
...@@ -41,7 +41,7 @@ export async function verifyTypeScriptSetup( ...@@ -41,7 +41,7 @@ export async function verifyTypeScriptSetup(
try { try {
// Check if the project uses TypeScript: // Check if the project uses TypeScript:
const intent = await getTypeScriptIntent(dir, pagesDir, config) const intent = await getTypeScriptIntent(dir, intentDirs, config)
if (!intent) { if (!intent) {
return { version: null } return { version: null }
} }
......
...@@ -96,6 +96,8 @@ export default class DevServer extends Server { ...@@ -96,6 +96,8 @@ export default class DevServer extends Server {
protected sortedRoutes?: string[] protected sortedRoutes?: string[]
private addedUpgradeListener = false private addedUpgradeListener = false
private pagesDir: string private pagesDir: string
// @ts-ignore TODO: add implementation
private rootDir?: string
protected staticPathsWorker?: { [key: string]: any } & { protected staticPathsWorker?: { [key: string]: any } & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
...@@ -175,7 +177,13 @@ export default class DevServer extends Server { ...@@ -175,7 +177,13 @@ export default class DevServer extends Server {
} }
this.isCustomServer = !options.isNextDevCommand this.isCustomServer = !options.isNextDevCommand
this.pagesDir = findPagesDir(this.dir) // TODO: hot-reload root/pages dirs?
const { pages: pagesDir, root: rootDir } = findPagesDir(
this.dir,
this.nextConfig.experimental.rootDir
)
this.pagesDir = pagesDir
this.rootDir = rootDir
} }
protected getBuildId(): string { protected getBuildId(): string {
...@@ -361,7 +369,12 @@ export default class DevServer extends Server { ...@@ -361,7 +369,12 @@ export default class DevServer extends Server {
async prepare(): Promise<void> { async prepare(): Promise<void> {
setGlobal('distDir', this.distDir) setGlobal('distDir', this.distDir)
setGlobal('phase', PHASE_DEVELOPMENT_SERVER) setGlobal('phase', PHASE_DEVELOPMENT_SERVER)
await verifyTypeScriptSetup(this.dir, this.pagesDir, false, this.nextConfig) await verifyTypeScriptSetup(
this.dir,
[this.pagesDir!, this.rootDir].filter(Boolean) as string[],
false,
this.nextConfig
)
this.customRoutes = await loadCustomRoutes(this.nextConfig) this.customRoutes = await loadCustomRoutes(this.nextConfig)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment