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