Unverified Commit 90af4259 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Development: Remove TypeScript from the hot path during bootup (#84090)

## What?

Changes the way TypeScript config validation behaves. It no longer does
`require('typescript')` and instead reads the `tsconfig.json`.

This introduces one new limitation which is that `extends` and
`references` cause the automatic config rewriting to be skipped, i.e. it
does not verify your `extends`​ / `references`​.

In my opinion that tradeoff is fine though because if you're using these
you likely already don't want Next.js to actively write into the project
config file. At least that has been a common feedback from the
community.

In cpu profiles this change shows 115ms -> 4ms.

Why does it take 115ms? TypeScript itself is quite large, lot of code to
parse. However, we're only reading a few values from it like the
version, some config options, and the config reading logic itself. In
the CPU profile it really is showing 4ms spent in Next.js and the rest
is the process of requiring TypeScript.
parent fa57e47f
......@@ -43,6 +43,8 @@ export function hasNecessaryDependencies(
)
const pkgDir = dirname(pkgPath)
resolutions.set(join(p.pkg, 'package.json'), pkgPath)
if (p.exportsRestrict) {
const fileNameToVerify = relative(p.pkg, p.file)
if (fileNameToVerify) {
......
......@@ -6,36 +6,41 @@ import { FatalError } from '../fatal-error'
import isError from '../is-error'
export async function getTypeScriptConfiguration(
ts: typeof import('typescript'),
typescript: typeof import('typescript'),
tsConfigPath: string,
metaOnly?: boolean
): Promise<import('typescript').ParsedCommandLine> {
try {
const formatDiagnosticsHost: import('typescript').FormatDiagnosticsHost = {
getCanonicalFileName: (fileName: string) => fileName,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getCurrentDirectory: typescript.sys.getCurrentDirectory,
getNewLine: () => os.EOL,
}
const { config, error } = ts.readConfigFile(tsConfigPath, ts.sys.readFile)
const { config, error } = typescript.readConfigFile(
tsConfigPath,
typescript.sys.readFile
)
if (error) {
throw new FatalError(ts.formatDiagnostic(error, formatDiagnosticsHost))
throw new FatalError(
typescript.formatDiagnostic(error, formatDiagnosticsHost)
)
}
let configToParse: any = config
const result = ts.parseJsonConfigFileContent(
const result = typescript.parseJsonConfigFileContent(
configToParse,
// When only interested in meta info,
// avoid enumerating all files (for performance reasons)
metaOnly
? {
...ts.sys,
...typescript.sys,
readDirectory(_path, extensions, _excludes, _includes, _depth) {
return [extensions ? `file${extensions[0]}` : `file.ts`]
},
}
: ts.sys,
: typescript.sys,
path.dirname(tsConfigPath)
)
......@@ -49,7 +54,7 @@ export async function getTypeScriptConfiguration(
if (result.errors?.length) {
throw new FatalError(
ts.formatDiagnostic(result.errors[0], formatDiagnosticsHost)
typescript.formatDiagnostic(result.errors[0], formatDiagnosticsHost)
)
}
......
import { existsSync, promises as fs } from 'fs'
import { existsSync, readFileSync } from 'fs'
import path from 'path'
import { recursiveReadDir } from '../recursive-readdir'
......@@ -15,12 +15,10 @@ export async function getTypeScriptIntent(
// project.
const hasTypeScriptConfiguration = existsSync(resolvedTsConfigPath)
if (hasTypeScriptConfiguration) {
const content = await fs
.readFile(resolvedTsConfigPath, { encoding: 'utf8' })
.then(
(txt) => txt.trim(),
() => null
)
const content = readFileSync(resolvedTsConfigPath, {
encoding: 'utf8',
}).trim()
return { firstTimeSetup: content === '' || content === '{}' }
}
......
......@@ -15,7 +15,7 @@ export interface TypeCheckResult {
}
export async function runTypeCheck(
ts: typeof import('typescript'),
typescript: typeof import('typescript'),
baseDir: string,
distDir: string,
tsConfigPath: string,
......@@ -23,7 +23,7 @@ export async function runTypeCheck(
isAppDirEnabled?: boolean
): Promise<TypeCheckResult> {
const effectiveConfiguration = await getTypeScriptConfiguration(
ts,
typescript,
tsConfigPath
)
......@@ -35,7 +35,7 @@ export async function runTypeCheck(
incremental: false,
}
}
const requiredConfig = getRequiredConfiguration(ts)
const requiredConfig = getRequiredConfiguration(typescript)
const options = {
...requiredConfig,
......@@ -56,7 +56,7 @@ export async function runTypeCheck(
)
}
incremental = true
program = ts.createIncrementalProgram({
program = typescript.createIncrementalProgram({
rootNames: effectiveConfiguration.fileNames,
options: {
...options,
......@@ -66,7 +66,10 @@ export async function runTypeCheck(
},
})
} else {
program = ts.createProgram(effectiveConfiguration.fileNames, options)
program = typescript.createProgram(
effectiveConfiguration.fileNames,
options
)
}
const result = program.emit()
......@@ -81,25 +84,35 @@ export async function runTypeCheck(
ignoreRegex.map((r) => r.source).join('|')
)
const allDiagnostics = ts
const allDiagnostics = typescript
.getPreEmitDiagnostics(program as import('typescript').Program)
.concat(result.diagnostics)
.filter((d) => !(d.file && regexIgnoredFile.test(d.file.fileName)))
const firstError =
allDiagnostics.find(
(d) => d.category === ts.DiagnosticCategory.Error && Boolean(d.file)
) ?? allDiagnostics.find((d) => d.category === ts.DiagnosticCategory.Error)
(d) =>
d.category === typescript.DiagnosticCategory.Error && Boolean(d.file)
) ??
allDiagnostics.find(
(d) => d.category === typescript.DiagnosticCategory.Error
)
// In test mode, we want to check all diagnostics, not just the first one.
if (process.env.__NEXT_TEST_MODE) {
if (firstError) {
const allErrors = allDiagnostics
.filter((d) => d.category === ts.DiagnosticCategory.Error)
.filter((d) => d.category === typescript.DiagnosticCategory.Error)
.map(
(d) =>
'[Test Mode] ' +
getFormattedDiagnostic(ts, baseDir, distDir, d, isAppDirEnabled)
getFormattedDiagnostic(
typescript,
baseDir,
distDir,
d,
isAppDirEnabled
)
)
console.error(
......@@ -115,14 +128,20 @@ export async function runTypeCheck(
if (firstError) {
throw new CompileError(
getFormattedDiagnostic(ts, baseDir, distDir, firstError, isAppDirEnabled)
getFormattedDiagnostic(
typescript,
baseDir,
distDir,
firstError,
isAppDirEnabled
)
)
}
const warnings = allDiagnostics
.filter((d) => d.category === ts.DiagnosticCategory.Warning)
.filter((d) => d.category === typescript.DiagnosticCategory.Warning)
.map((d) =>
getFormattedDiagnostic(ts, baseDir, distDir, d, isAppDirEnabled)
getFormattedDiagnostic(typescript, baseDir, distDir, d, isAppDirEnabled)
)
return {
......
......@@ -40,7 +40,7 @@ describe('writeConfigurationDefaults()', () => {
})
await writeConfigurationDefaults(
ts,
ts.version,
tsConfigPath,
isFirstTimeSetup,
hasAppDir,
......@@ -128,7 +128,7 @@ describe('writeConfigurationDefaults()', () => {
)
await writeConfigurationDefaults(
ts,
ts.version,
tsConfigPath,
isFirstTimeSetup,
hasAppDir,
......@@ -150,7 +150,7 @@ describe('writeConfigurationDefaults()', () => {
nextAppTypes = `${distDir}/types/**/*.ts`
})
it('should support empty includes when base provides it', async () => {
it('should not change tsconfig with extends', async () => {
const include = ['**/*.ts', '**/*.tsx', nextAppTypes, '**/*.mts']
const content = { extends: './tsconfig.base.json' }
const baseContent = { include }
......@@ -160,7 +160,7 @@ describe('writeConfigurationDefaults()', () => {
await expect(
writeConfigurationDefaults(
ts,
ts.version,
tsConfigPath,
isFirstTimeSetup,
hasAppDir,
......@@ -173,62 +173,9 @@ describe('writeConfigurationDefaults()', () => {
const parsed = JSON.parse(output)
expect(parsed.include).toBeUndefined()
})
it('should replace includes when base is missing appTypes', async () => {
const include = ['**/*.ts', '**/*.tsx', '**/*.mts']
const content = { extends: './tsconfig.base.json' }
const baseContent = { include }
await writeFile(tsConfigPath, JSON.stringify(content, null, 2))
await writeFile(tsConfigBasePath, JSON.stringify(baseContent, null, 2))
await expect(
writeConfigurationDefaults(
ts,
tsConfigPath,
isFirstTimeSetup,
hasAppDir,
distDir,
hasPagesDir
)
).resolves.not.toThrow()
const output = await readFile(tsConfigPath, 'utf8')
const parsed = JSON.parse(output)
expect(parsed.include.sort()).toMatchInlineSnapshot(`
[
"**/*.mts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
]
`)
})
it('should not add strictNullChecks if base provides it', async () => {
const content = { extends: './tsconfig.base.json' }
const baseContent = {
compilerOptions: { strictNullChecks: true, strict: true },
}
await writeFile(tsConfigPath, JSON.stringify(content, null, 2))
await writeFile(tsConfigBasePath, JSON.stringify(baseContent, null, 2))
await writeConfigurationDefaults(
ts,
tsConfigPath,
isFirstTimeSetup,
hasAppDir,
distDir,
hasPagesDir
)
const output = await readFile(tsConfigPath, 'utf8')
const parsed = JSON.parse(output)
expect(parsed.compilerOptions.strictNullChecks).toBeUndefined()
expect(parsed).toStrictEqual({
extends: './tsconfig.base.json',
})
})
})
})
......
import { bold, cyan, red, yellow } from './picocolors'
import path from 'path'
import path, { join } from 'path'
import { hasNecessaryDependencies } from './has-necessary-dependencies'
import type { NecessaryDependencies } from './has-necessary-dependencies'
......@@ -106,20 +106,22 @@ export async function verifyTypeScriptSetup({
}
// Load TypeScript after we're sure it exists:
const tsPath = deps.resolved.get('typescript')!
const ts = (await Promise.resolve(
require(tsPath)
)) as typeof import('typescript')
const tsPackageJsonPath = deps.resolved.get(
join('typescript', 'package.json')
)!
const typescriptPackageJson = require(tsPackageJsonPath)
if (semver.lt(ts.version, '4.5.2')) {
const typescriptVersion = typescriptPackageJson.version
if (semver.lt(typescriptVersion, '4.5.2')) {
log.warn(
`Minimum recommended TypeScript version is v4.5.2, older versions can potentially be incompatible with Next.js. Detected: ${ts.version}`
`Minimum recommended TypeScript version is v4.5.2, older versions can potentially be incompatible with Next.js. Detected: ${typescriptVersion}`
)
}
// Reconfigure (or create) the user's `tsconfig.json` for them:
await writeConfigurationDefaults(
ts,
typescriptVersion,
resolvedTsConfigPath,
intent.firstTimeSetup,
hasAppDir,
......@@ -141,9 +143,14 @@ export async function verifyTypeScriptSetup({
const { runTypeCheck } =
require('./typescript/runTypeCheck') as typeof import('./typescript/runTypeCheck')
const tsPath = deps.resolved.get('typescript')!
const typescript = (await Promise.resolve(
require(tsPath)
)) as typeof import('typescript')
// Verify the project passes type-checking before we go to webpack phase:
result = await runTypeCheck(
ts,
typescript,
dir,
distDir,
resolvedTsConfigPath,
......@@ -151,7 +158,7 @@ export async function verifyTypeScriptSetup({
hasAppDir
)
}
return { result, version: ts.version }
return { result, version: typescriptVersion }
} catch (err) {
// These are special errors that should not show a stack trace:
if (err instanceof CompileError) {
......
{
"extends": "./tsconfig.base.json"
"extends": "./tsconfig.base.json",
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"plugins": [
{
"name": "next"
}
],
"strictNullChecks": false
},
"include": [
"next-env.d.ts",
".next/types/**/*.ts",
"**/*.mts",
"**/*.ts",
"**/*.tsx"
],
"exclude": ["node_modules"]
}
......@@ -592,21 +592,9 @@ import path from 'path'
await writeFile(
tsConfigBase,
`{ "compilerOptions": { "verbatimModuleSyntax": true } }`
)
await writeFile(tsConfig, `{ "extends": "./tsconfig.base.json" }`)
await new Promise((resolve) => setTimeout(resolve, 500))
const { code, stderr, stdout } = await nextBuild(appDir, undefined, {
stderr: true,
stdout: true,
})
expect(stderr + stdout).not.toContain('isolatedModules')
expect(code).toBe(0)
expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(`
"{
"extends": "./tsconfig.base.json",
"compilerOptions": {
`{
"compilerOptions": {
"verbatimModuleSyntax": true,
"target": "ES2017",
"lib": [
"dom",
......@@ -640,9 +628,20 @@ import path from 'path'
"exclude": [
"node_modules"
]
}
"
`)
}`
)
await writeFile(tsConfig, `{ "extends": "./tsconfig.base.json" }`)
await new Promise((resolve) => setTimeout(resolve, 500))
const { code, stderr, stdout } = await nextBuild(appDir, undefined, {
stderr: true,
stdout: true,
})
expect(stderr + stdout).not.toContain('isolatedModules')
expect(code).toBe(0)
expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(
`"{ "extends": "./tsconfig.base.json" }"`
)
})
it('allows you to extend another configuration file', async () => {
......@@ -703,15 +702,9 @@ import path from 'path'
expect(stderr + stdout).not.toContain('moduleResolution')
expect(code).toBe(0)
expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(`
"{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"target": "ES2017"
}
}
"
`)
expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(
`"{ "extends": "./tsconfig.base.json" }"`
)
})
it('creates compilerOptions when you extend another config', async () => {
......@@ -771,16 +764,9 @@ import path from 'path'
expect(stderr + stdout).not.toContain('moduleResolution')
expect(code).toBe(0)
expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(`
"{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"target": "ES2017",
"incremental": true
}
}
"
`)
expect(await readFile(tsConfig, 'utf8')).toMatchInlineSnapshot(
`"{ "extends": "./tsconfig.base.json" }"`
)
})
// TODO: Enable this test when repo has upgraded to TypeScript 5.4. Currently tested as E2E: tsconfig-module-preserve
......
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