Unverified Commit d1122f82 authored by Sukka's avatar Sukka Committed by GitHub

perf: run type check and eslint in parallel (#37105)

parent 4e6b6a5b
...@@ -4,6 +4,7 @@ import chalk from 'next/dist/compiled/chalk' ...@@ -4,6 +4,7 @@ import chalk from 'next/dist/compiled/chalk'
import crypto from 'crypto' import crypto from 'crypto'
import { isMatch } from 'next/dist/compiled/micromatch' import { isMatch } from 'next/dist/compiled/micromatch'
import { promises, writeFileSync } from 'fs' import { promises, writeFileSync } from 'fs'
import { Worker as JestWorker } from 'next/dist/compiled/jest-worker'
import { Worker } from '../lib/worker' import { Worker } from '../lib/worker'
import devalue from 'next/dist/compiled/devalue' import devalue from 'next/dist/compiled/devalue'
import { escapeStringRegexp } from '../shared/lib/escape-regexp' import { escapeStringRegexp } from '../shared/lib/escape-regexp'
...@@ -34,7 +35,6 @@ import { nonNullable } from '../lib/non-nullable' ...@@ -34,7 +35,6 @@ import { nonNullable } from '../lib/non-nullable'
import { recursiveDelete } from '../lib/recursive-delete' import { recursiveDelete } from '../lib/recursive-delete'
import { verifyAndLint } from '../lib/verifyAndLint' import { verifyAndLint } from '../lib/verifyAndLint'
import { verifyPartytownSetup } from '../lib/verify-partytown-setup' import { verifyPartytownSetup } from '../lib/verify-partytown-setup'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import { import {
BUILD_ID_FILE, BUILD_ID_FILE,
BUILD_MANIFEST, BUILD_MANIFEST,
...@@ -229,30 +229,74 @@ export default async function build( ...@@ -229,30 +229,74 @@ export default async function build(
const ignoreTypeScriptErrors = Boolean( const ignoreTypeScriptErrors = Boolean(
config.typescript.ignoreBuildErrors config.typescript.ignoreBuildErrors
) )
const ignoreESLint = Boolean(config.eslint.ignoreDuringBuilds)
const eslintCacheDir = path.join(cacheDir, 'eslint/')
const shouldLint = !ignoreESLint && runLint
if (ignoreTypeScriptErrors) {
Log.info('Skipping validation of types')
}
if (runLint && ignoreESLint) {
// only print log when build requre lint while ignoreESLint is enabled
Log.info('Skipping linting')
}
let typeCheckingAndLintingSpinnerPrefixText: string | undefined
let typeCheckingAndLintingSpinner:
| ReturnType<typeof createSpinner>
| undefined
if (!ignoreTypeScriptErrors && shouldLint) {
typeCheckingAndLintingSpinnerPrefixText =
'Linting and checking validity of types'
} else if (!ignoreTypeScriptErrors) {
typeCheckingAndLintingSpinnerPrefixText = 'Checking validity of types'
} else if (shouldLint) {
typeCheckingAndLintingSpinnerPrefixText = 'Linting'
}
// we will not create a spinner if both ignoreTypeScriptErrors and ignoreESLint are
// enabled, but we will still verifying project's tsconfig and dependencies.
if (typeCheckingAndLintingSpinnerPrefixText) {
typeCheckingAndLintingSpinner = createSpinner({
prefixText: `${Log.prefixes.info} ${typeCheckingAndLintingSpinnerPrefixText}`,
})
}
const typeCheckStart = process.hrtime() const typeCheckStart = process.hrtime()
const typeCheckingSpinner = createSpinner({
prefixText: `${Log.prefixes.info} ${
ignoreTypeScriptErrors
? 'Skipping validation of types'
: 'Checking validity of types'
}`,
})
const verifyResult = await nextBuildSpan const [[verifyResult, typeCheckEnd]] = await Promise.all([
.traceChild('verify-typescript-setup') nextBuildSpan.traceChild('verify-typescript-setup').traceAsyncFn(() =>
.traceAsyncFn(() =>
verifyTypeScriptSetup( verifyTypeScriptSetup(
dir, dir,
[pagesDir, viewsDir].filter(Boolean) as string[], [pagesDir, viewsDir].filter(Boolean) as string[],
!ignoreTypeScriptErrors, !ignoreTypeScriptErrors,
config, config,
cacheDir cacheDir,
) config.experimental.cpus,
) config.experimental.workerThreads
).then((resolved) => {
const checkEnd = process.hrtime(typeCheckStart)
return [resolved, checkEnd] as const
})
),
shouldLint &&
nextBuildSpan.traceChild('verify-and-lint').traceAsyncFn(async () => {
await verifyAndLint(
dir,
eslintCacheDir,
config.eslint?.dirs,
config.experimental.cpus,
config.experimental.workerThreads,
telemetry
)
}),
])
const typeCheckEnd = process.hrtime(typeCheckStart) typeCheckingAndLintingSpinner?.stopAndPersist()
if (!ignoreTypeScriptErrors) { if (!ignoreTypeScriptErrors && verifyResult) {
telemetry.record( telemetry.record(
eventTypeCheckCompleted({ eventTypeCheckCompleted({
durationInSeconds: typeCheckEnd[0], durationInSeconds: typeCheckEnd[0],
...@@ -264,27 +308,6 @@ export default async function build( ...@@ -264,27 +308,6 @@ export default async function build(
) )
} }
if (typeCheckingSpinner) {
typeCheckingSpinner.stopAndPersist()
}
const ignoreESLint = Boolean(config.eslint.ignoreDuringBuilds)
const eslintCacheDir = path.join(cacheDir, 'eslint/')
const shouldLint = !ignoreESLint && runLint
if (shouldLint) {
await nextBuildSpan
.traceChild('verify-and-lint')
.traceAsyncFn(async () => {
await verifyAndLint(
dir,
eslintCacheDir,
config.eslint?.dirs,
config.experimental.cpus,
config.experimental.workerThreads,
telemetry
)
})
}
const buildLintEvent: EventBuildFeatureUsage = { const buildLintEvent: EventBuildFeatureUsage = {
featureName: 'build-lint', featureName: 'build-lint',
invocationCount: shouldLint ? 1 : 0, invocationCount: shouldLint ? 1 : 0,
...@@ -2318,6 +2341,50 @@ export default async function build( ...@@ -2318,6 +2341,50 @@ export default async function build(
} }
} }
/**
* typescript will be loaded in "next/lib/verifyTypeScriptSetup" and
* then passed to "next/lib/typescript/runTypeCheck" as a parameter.
*
* Since it is impossible to pass a function from main thread to a worker,
* instead of running "next/lib/typescript/runTypeCheck" in a worker,
* we will run entire "next/lib/verifyTypeScriptSetup" in a worker instead.
*/
function verifyTypeScriptSetup(
dir: string,
intentDirs: string[],
typeCheckPreflight: boolean,
config: NextConfigComplete,
cacheDir: string | undefined,
numWorkers: number | undefined,
enableWorkerThreads: boolean | undefined
) {
const typeCheckWorker = new JestWorker(
require.resolve('../lib/verifyTypeScriptSetup'),
{
numWorkers,
enableWorkerThreads,
}
) as JestWorker & {
verifyTypeScriptSetup: typeof import('../lib/verifyTypeScriptSetup').verifyTypeScriptSetup
}
typeCheckWorker.getStdout().pipe(process.stdout)
typeCheckWorker.getStderr().pipe(process.stderr)
return typeCheckWorker
.verifyTypeScriptSetup(
dir,
intentDirs,
typeCheckPreflight,
config,
cacheDir
)
.then((result) => {
typeCheckWorker.end()
return result
})
}
function generateClientSsgManifest( function generateClientSsgManifest(
prerenderManifest: PrerenderManifest, prerenderManifest: PrerenderManifest,
{ {
......
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