rsnext/packages/next/lib/verifyAndLint.ts
Jiachi Liu 03eb4b1d61
Bypass empty pages folder for layouts (#40132)
Check `pagesDir` to bypass empty pages folder when appDir is enabled

* Output empty loadable manifest for now if there's no `pagesDir`
* No custom aliases with all page extensions for `/_app`, `_document` if pagesDir is empty, only keep the built-in ones
* Check pagesDir in build/dev-server/eslint
* Type safe: change arguments of some APIs from optional to required, so that we won't mess up with default arguments
2022-09-03 00:13:47 +00:00

84 lines
2.3 KiB
TypeScript

import chalk from 'next/dist/compiled/chalk'
import { Worker } from 'next/dist/compiled/jest-worker'
import { existsSync } from 'fs'
import { join } from 'path'
import { ESLINT_DEFAULT_DIRS } from './constants'
import { Telemetry } from '../telemetry/storage'
import { eventLintCheckCompleted } from '../telemetry/events'
import { CompileError } from './compile-error'
import isError from './is-error'
export async function verifyAndLint(
dir: string,
cacheLocation: string,
configLintDirs: string[] | undefined,
numWorkers: number | undefined,
enableWorkerThreads: boolean | undefined,
telemetry: Telemetry,
hasAppDir: boolean
): Promise<void> {
try {
const lintWorkers = new Worker(require.resolve('./eslint/runLintCheck'), {
numWorkers,
enableWorkerThreads,
maxRetries: 0,
}) as Worker & {
runLintCheck: typeof import('./eslint/runLintCheck').runLintCheck
}
lintWorkers.getStdout().pipe(process.stdout)
lintWorkers.getStderr().pipe(process.stderr)
const lintDirs = (configLintDirs ?? ESLINT_DEFAULT_DIRS).reduce(
(res: string[], d: string) => {
const currDir = join(dir, d)
if (!existsSync(currDir)) return res
res.push(currDir)
return res
},
[]
)
const lintResults = await lintWorkers.runLintCheck(dir, lintDirs, {
hasAppDir,
lintDuringBuild: true,
eslintOptions: {
cacheLocation,
},
})
const lintOutput =
typeof lintResults === 'string' ? lintResults : lintResults?.output
if (typeof lintResults !== 'string' && lintResults?.eventInfo) {
telemetry.record(
eventLintCheckCompleted({
...lintResults.eventInfo,
buildLint: true,
})
)
}
if (typeof lintResults !== 'string' && lintResults?.isError && lintOutput) {
await telemetry.flush()
throw new CompileError(lintOutput)
}
if (lintOutput) {
console.log(lintOutput)
}
lintWorkers.end()
} catch (err) {
if (isError(err)) {
if (err.type === 'CompileError' || err instanceof CompileError) {
console.error(chalk.red('\nFailed to compile.'))
console.error(err.message)
process.exit(1)
} else if (err.type === 'FatalError') {
console.error(err.message)
process.exit(1)
}
}
throw err
}
}