Unverified Commit 4e8fac93 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

cache typechecking with incremental compilation (#24559)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 42c4743f
...@@ -124,7 +124,7 @@ ...@@ -124,7 +124,7 @@
"tailwindcss": "1.1.3", "tailwindcss": "1.1.3",
"taskr": "1.1.0", "taskr": "1.1.0",
"tree-kill": "1.2.2", "tree-kill": "1.2.2",
"typescript": "3.8.3", "typescript": "4.3.0-beta",
"wait-port": "0.2.2", "wait-port": "0.2.2",
"web-streams-polyfill": "2.1.1", "web-streams-polyfill": "2.1.1",
"webpack-bundle-analyzer": "4.3.0", "webpack-bundle-analyzer": "4.3.0",
......
...@@ -146,8 +146,8 @@ export default async function build( ...@@ -146,8 +146,8 @@ export default async function build(
const { headers, rewrites, redirects } = customRoutes const { headers, rewrites, redirects } = customRoutes
if (ciEnvironment.isCI && !ciEnvironment.hasNextSupport) {
const cacheDir = path.join(distDir, 'cache') const cacheDir = path.join(distDir, 'cache')
if (ciEnvironment.isCI && !ciEnvironment.hasNextSupport) {
const hasCache = await fileExists(cacheDir) const hasCache = await fileExists(cacheDir)
if (!hasCache) { if (!hasCache) {
...@@ -193,7 +193,7 @@ export default async function build( ...@@ -193,7 +193,7 @@ export default async function build(
const verifyResult = await nextBuildSpan const verifyResult = await nextBuildSpan
.traceChild('verify-typescript-setup') .traceChild('verify-typescript-setup')
.traceAsyncFn(() => .traceAsyncFn(() =>
verifyTypeScriptSetup(dir, pagesDir, !ignoreTypeScriptErrors) verifyTypeScriptSetup(dir, pagesDir, !ignoreTypeScriptErrors, cacheDir)
) )
const typeCheckEnd = process.hrtime(typeCheckStart) const typeCheckEnd = process.hrtime(typeCheckStart)
......
import path from 'path'
import { import {
DiagnosticCategory, DiagnosticCategory,
getFormattedDiagnostic, getFormattedDiagnostic,
...@@ -18,7 +19,8 @@ export interface TypeCheckResult { ...@@ -18,7 +19,8 @@ export interface TypeCheckResult {
export async function runTypeCheck( export async function runTypeCheck(
ts: typeof import('typescript'), ts: typeof import('typescript'),
baseDir: string, baseDir: string,
tsConfigPath: string tsConfigPath: string,
cacheDir?: string
): Promise<TypeCheckResult> { ): Promise<TypeCheckResult> {
const effectiveConfiguration = await getTypeScriptConfiguration( const effectiveConfiguration = await getTypeScriptConfiguration(
ts, ts,
...@@ -35,11 +37,28 @@ export async function runTypeCheck( ...@@ -35,11 +37,28 @@ export async function runTypeCheck(
} }
const requiredConfig = getRequiredConfiguration(ts) const requiredConfig = getRequiredConfiguration(ts)
const program = ts.createProgram(effectiveConfiguration.fileNames, { const options = {
...effectiveConfiguration.options, ...effectiveConfiguration.options,
...requiredConfig, ...requiredConfig,
noEmit: true, noEmit: true,
}
let program: import('typescript').Program
let incremental = false
if (options.incremental && cacheDir) {
incremental = true
const builderProgram = ts.createIncrementalProgram({
rootNames: effectiveConfiguration.fileNames,
options: {
...options,
incremental: true,
tsBuildInfoFile: path.join(cacheDir, '.tsbuildinfo'),
},
}) })
program = builderProgram.getProgram()
} else {
program = ts.createProgram(effectiveConfiguration.fileNames, options)
}
const result = program.emit() const result = program.emit()
// Intended to match: // Intended to match:
...@@ -79,6 +98,6 @@ export async function runTypeCheck( ...@@ -79,6 +98,6 @@ export async function runTypeCheck(
warnings, warnings,
inputFilesCount: effectiveConfiguration.fileNames.length, inputFilesCount: effectiveConfiguration.fileNames.length,
totalFilesCount: program.getSourceFiles().length, totalFilesCount: program.getSourceFiles().length,
incremental: false, incremental,
} }
} }
import { promises as fs } from 'fs' import { promises as fs } from 'fs'
import chalk from 'chalk' import chalk from 'chalk'
import * as CommentJson from 'next/dist/compiled/comment-json' import * as CommentJson from 'next/dist/compiled/comment-json'
import semver from 'next/dist/compiled/semver'
import os from 'os' import os from 'os'
import { getTypeScriptConfiguration } from './getTypeScriptConfiguration' import { getTypeScriptConfiguration } from './getTypeScriptConfiguration'
...@@ -28,6 +29,9 @@ function getDesiredCompilerOptions( ...@@ -28,6 +29,9 @@ function getDesiredCompilerOptions(
strict: { suggested: false }, strict: { suggested: false },
forceConsistentCasingInFileNames: { suggested: true }, forceConsistentCasingInFileNames: { suggested: true },
noEmit: { suggested: true }, noEmit: { suggested: true },
...(semver.gte(ts.version, '4.3.0-beta')
? { incremental: { suggested: true } }
: undefined),
// These values are required and cannot be changed by the user // These values are required and cannot be changed by the user
// Keep this in sync with the webpack config // Keep this in sync with the webpack config
......
...@@ -15,7 +15,8 @@ import { writeConfigurationDefaults } from './typescript/writeConfigurationDefau ...@@ -15,7 +15,8 @@ import { writeConfigurationDefaults } from './typescript/writeConfigurationDefau
export async function verifyTypeScriptSetup( export async function verifyTypeScriptSetup(
dir: string, dir: string,
pagesDir: string, pagesDir: string,
typeCheckPreflight: boolean typeCheckPreflight: boolean,
cacheDir?: string
): Promise<{ result?: TypeCheckResult; version: string | null }> { ): Promise<{ result?: TypeCheckResult; version: string | null }> {
const tsConfigPath = path.join(dir, 'tsconfig.json') const tsConfigPath = path.join(dir, 'tsconfig.json')
...@@ -48,7 +49,7 @@ export async function verifyTypeScriptSetup( ...@@ -48,7 +49,7 @@ export async function verifyTypeScriptSetup(
const { runTypeCheck } = require('./typescript/runTypeCheck') const { runTypeCheck } = require('./typescript/runTypeCheck')
// 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(ts, dir, tsConfigPath) result = await runTypeCheck(ts, dir, tsConfigPath, cacheDir)
} }
return { result, version: ts.version } return { result, version: ts.version }
} catch (err) { } catch (err) {
......
...@@ -11,7 +11,7 @@ export const ShadowPortal: React.FC<ShadowPortalProps> = function Portal({ ...@@ -11,7 +11,7 @@ export const ShadowPortal: React.FC<ShadowPortalProps> = function Portal({
let mountNode = React.useRef<HTMLDivElement | null>(null) let mountNode = React.useRef<HTMLDivElement | null>(null)
let portalNode = React.useRef<HTMLElement | null>(null) let portalNode = React.useRef<HTMLElement | null>(null)
let shadowNode = React.useRef<ShadowRoot | null>(null) let shadowNode = React.useRef<ShadowRoot | null>(null)
let [, forceUpdate] = React.useState() let [, forceUpdate] = React.useState<{} | undefined>()
React.useLayoutEffect(() => { React.useLayoutEffect(() => {
const ownerDocument = mountNode.current!.ownerDocument! const ownerDocument = mountNode.current!.ownerDocument!
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
"strict": false, "strict": false,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"esModuleInterop": true, "esModuleInterop": true,
"module": "esnext", "module": "esnext",
"moduleResolution": "node", "moduleResolution": "node",
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"strict": false, "strict": false,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
"strict": false, "strict": false,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"esModuleInterop": true, "esModuleInterop": true,
"module": "esnext", "module": "esnext",
"moduleResolution": "node", "moduleResolution": "node",
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -39,6 +39,7 @@ describe('tsconfig.json verifier', () => { ...@@ -39,6 +39,7 @@ describe('tsconfig.json verifier', () => {
\\"strict\\": false, \\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true, \\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true, \\"noEmit\\": true,
\\"incremental\\": true,
\\"esModuleInterop\\": true, \\"esModuleInterop\\": true,
\\"module\\": \\"esnext\\", \\"module\\": \\"esnext\\",
\\"moduleResolution\\": \\"node\\", \\"moduleResolution\\": \\"node\\",
...@@ -83,6 +84,7 @@ describe('tsconfig.json verifier', () => { ...@@ -83,6 +84,7 @@ describe('tsconfig.json verifier', () => {
\\"strict\\": false, \\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true, \\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true, \\"noEmit\\": true,
\\"incremental\\": true,
\\"esModuleInterop\\": true, \\"esModuleInterop\\": true,
\\"module\\": \\"esnext\\", \\"module\\": \\"esnext\\",
\\"moduleResolution\\": \\"node\\", \\"moduleResolution\\": \\"node\\",
...@@ -150,6 +152,7 @@ describe('tsconfig.json verifier', () => { ...@@ -150,6 +152,7 @@ describe('tsconfig.json verifier', () => {
\\"strict\\": false, \\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true, \\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true, \\"noEmit\\": true,
\\"incremental\\": true,
\\"moduleResolution\\": \\"node\\", \\"moduleResolution\\": \\"node\\",
\\"resolveJsonModule\\": true, \\"resolveJsonModule\\": true,
\\"isolatedModules\\": true, \\"isolatedModules\\": true,
...@@ -198,6 +201,7 @@ describe('tsconfig.json verifier', () => { ...@@ -198,6 +201,7 @@ describe('tsconfig.json verifier', () => {
\\"strict\\": false, \\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true, \\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true, \\"noEmit\\": true,
\\"incremental\\": true,
\\"moduleResolution\\": \\"node\\", \\"moduleResolution\\": \\"node\\",
\\"resolveJsonModule\\": true, \\"resolveJsonModule\\": true,
\\"isolatedModules\\": true, \\"isolatedModules\\": true,
...@@ -243,6 +247,7 @@ describe('tsconfig.json verifier', () => { ...@@ -243,6 +247,7 @@ describe('tsconfig.json verifier', () => {
\\"strict\\": false, \\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true, \\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true, \\"noEmit\\": true,
\\"incremental\\": true,
\\"moduleResolution\\": \\"node\\", \\"moduleResolution\\": \\"node\\",
\\"resolveJsonModule\\": true, \\"resolveJsonModule\\": true,
\\"isolatedModules\\": true, \\"isolatedModules\\": true,
...@@ -281,6 +286,7 @@ describe('tsconfig.json verifier', () => { ...@@ -281,6 +286,7 @@ describe('tsconfig.json verifier', () => {
"strict": false, "strict": false,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"esModuleInterop": true, "esModuleInterop": true,
"module": "esnext", "module": "esnext",
"moduleResolution": "node", "moduleResolution": "node",
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -7,10 +7,12 @@ jest.setTimeout(1000 * 60 * 2) ...@@ -7,10 +7,12 @@ jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '..') const appDir = join(__dirname, '..')
const nextConfigFile = new File(join(appDir, 'next.config.js')) const nextConfigFile = new File(join(appDir, 'next.config.js'))
const tsConfigFile = new File(join(appDir, 'tsconfig.json'))
describe('TypeScript with error handling options', () => { describe('TypeScript with error handling options', () => {
// Dev can no longer show errors (for now), logbox will cover this in the // Dev can no longer show errors (for now), logbox will cover this in the
// future. // future.
for (const incremental of [false, true]) {
for (const ignoreBuildErrors of [false, true]) { for (const ignoreBuildErrors of [false, true]) {
describe(`ignoreBuildErrors: ${ignoreBuildErrors}`, () => { describe(`ignoreBuildErrors: ${ignoreBuildErrors}`, () => {
beforeAll(() => { beforeAll(() => {
...@@ -18,15 +20,33 @@ describe('TypeScript with error handling options', () => { ...@@ -18,15 +20,33 @@ describe('TypeScript with error handling options', () => {
typescript: { ignoreBuildErrors }, typescript: { ignoreBuildErrors },
} }
nextConfigFile.write('module.exports = ' + JSON.stringify(nextConfig)) nextConfigFile.write('module.exports = ' + JSON.stringify(nextConfig))
const tsconfig = JSON.parse(tsConfigFile.originalContent)
tsConfigFile.write(
JSON.stringify(
{
...tsconfig,
compilerOptions: {
...tsconfig.compilerOptions,
incremental,
},
},
null,
2
)
)
}) })
afterAll(() => { afterAll(() => {
nextConfigFile.restore() nextConfigFile.restore()
tsConfigFile.restore()
}) })
it( it(
ignoreBuildErrors (ignoreBuildErrors
? 'Next builds the application despite type errors' ? 'Next builds the application despite type errors'
: 'Next fails to build the application despite type errors', : 'Next fails to build the application despite type errors') +
(incremental
? ' in incremental mode'
: ' without incremental mode'),
async () => { async () => {
const { stdout, stderr } = await nextBuild(appDir, [], { const { stdout, stderr } = await nextBuild(appDir, [], {
stdout: true, stdout: true,
...@@ -47,4 +67,5 @@ describe('TypeScript with error handling options', () => { ...@@ -47,4 +67,5 @@ describe('TypeScript with error handling options', () => {
) )
}) })
} }
}
}) })
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"strict": true, "strict": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"noEmit": true, "noEmit": true,
"incremental": true,
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true "isolatedModules": true
......
...@@ -16122,6 +16122,11 @@ typescript@3.8.3: ...@@ -16122,6 +16122,11 @@ typescript@3.8.3:
version "3.8.3" version "3.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061" resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
typescript@4.3.0-beta:
version "4.3.0-beta"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.0-beta.tgz#8098e7be29032f09827e94b8e3a6befc9ff70c77"
integrity sha512-bl3wxSVL6gWLQFa466Vm5Vk3z0BNx+QxWhb9wFiYEHm6H8oqFd8Wo3XjgCVxAa5yiSFFKgo/ngBpXdIwqo5o0A==
typescript@^4.1.3: typescript@^4.1.3:
version "4.1.3" version "4.1.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
......
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