Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update findPagesDir #36619

Merged
merged 3 commits into from May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/next/build/index.ts
Expand Up @@ -196,7 +196,11 @@ export default async function build(
setGlobal('telemetry', telemetry)

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)

telemetry.record(
Expand Down Expand Up @@ -230,7 +234,7 @@ export default async function build(
.traceAsyncFn(() =>
verifyTypeScriptSetup(
dir,
pagesDir,
[pagesDir, rootDir].filter(Boolean) as string[],
!ignoreTypeScriptErrors,
config,
cacheDir
Expand Down
3 changes: 2 additions & 1 deletion packages/next/lib/eslint/runLintCheck.ts
Expand Up @@ -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) {
let updatedPagesDir = false
Expand Down
35 changes: 25 additions & 10 deletions packages/next/lib/find-pages-dir.ts
Expand Up @@ -10,22 +10,37 @@ export const existsSync = (f: string): boolean => {
}
}

export function findPagesDir(dir: string): string {
// prioritize ./pages over ./src/pages
let curDir = path.join(dir, 'pages')
function findDir(dir: string, name: string): string | null {
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
// prioritize ./${name} over ./src/${name}
let curDir = path.join(dir, name)
if (existsSync(curDir)) return curDir

curDir = path.join(dir, 'src/pages')
curDir = path.join(dir, 'src', name)
if (existsSync(curDir)) return curDir

// Check one level up the tree to see if the pages directory might be there
if (existsSync(path.join(dir, '..', 'pages'))) {
return null
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
}

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(
'> 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(
"> Couldn't find a `pages` directory. Please create one under the project root"
)
return {
pages: pagesDir,
root: rootDir,
}
}
18 changes: 10 additions & 8 deletions packages/next/lib/typescript/getTypeScriptIntent.ts
Expand Up @@ -8,7 +8,7 @@ export type TypeScriptIntent = { firstTimeSetup: boolean }

export async function getTypeScriptIntent(
baseDir: string,
pagesDir: string,
intentDirs: string[],
config: NextConfigComplete
): Promise<TypeScriptIntent | false> {
const tsConfigPath = path.join(baseDir, config.typescript.tsconfigPath)
Expand All @@ -28,13 +28,15 @@ export async function getTypeScriptIntent(
// project for the user when we detect TypeScript files. So, we need to check
// the `pages/` directory for a TypeScript file.
// Checking all directories is too slow, so this is a happy medium.
const typescriptFiles = await recursiveReadDir(
pagesDir,
/.*\.(ts|tsx)$/,
/(node_modules|.*\.d\.ts)/
)
if (typescriptFiles.length) {
return { firstTimeSetup: true }
for (const dir of intentDirs) {
const typescriptFiles = await recursiveReadDir(
dir,
/.*\.(ts|tsx)$/,
/(node_modules|.*\.d\.ts)/
)
if (typescriptFiles.length) {
return { firstTimeSetup: true }
}
}

return false
Expand Down
4 changes: 2 additions & 2 deletions packages/next/lib/verifyTypeScriptSetup.ts
Expand Up @@ -32,7 +32,7 @@ const requiredPackages = [

export async function verifyTypeScriptSetup(
dir: string,
pagesDir: string,
intentDirs: string[],
typeCheckPreflight: boolean,
config: NextConfigComplete,
cacheDir?: string
Expand All @@ -41,7 +41,7 @@ export async function verifyTypeScriptSetup(

try {
// Check if the project uses TypeScript:
const intent = await getTypeScriptIntent(dir, pagesDir, config)
const intent = await getTypeScriptIntent(dir, intentDirs, config)
if (!intent) {
return { version: null }
}
Expand Down
17 changes: 15 additions & 2 deletions packages/next/server/dev/next-dev-server.ts
Expand Up @@ -96,6 +96,8 @@ export default class DevServer extends Server {
protected sortedRoutes?: string[]
private addedUpgradeListener = false
private pagesDir: string
// @ts-ignore TODO: add implementation
private rootDir?: string

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

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 {
Expand Down Expand Up @@ -361,7 +369,12 @@ export default class DevServer extends Server {
async prepare(): Promise<void> {
setGlobal('distDir', this.distDir)
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)

Expand Down