Unverified Commit fcadad99 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

TypeScript report on active pages only (#8703)

* TypeScript report on active pages only
The TypeScript integration needs to report on active pages only.
When inactive pages are reported, they block the compilation and prevent the user from fixing the error (because the file isn't watched).

We cannot add the file to the webpack watcher because the compilation has already been sealed.

* Remove TODO message

* Test that inactive pages are not type checked
parent e5aa9614
...@@ -16,6 +16,11 @@ export function startedDevelopmentServer(appUrl: string) { ...@@ -16,6 +16,11 @@ export function startedDevelopmentServer(appUrl: string) {
let previousClient: any = null let previousClient: any = null
let previousServer: any = null let previousServer: any = null
type CompilerDiagnosticsWithFile = {
errors: { file: string | undefined; message: string }[] | null
warnings: { file: string | undefined; message: string }[] | null
}
type CompilerDiagnostics = { type CompilerDiagnostics = {
errors: string[] | null errors: string[] | null
warnings: string[] | null warnings: string[] | null
...@@ -231,8 +236,8 @@ export function watchCompilers( ...@@ -231,8 +236,8 @@ export function watchCompilers(
hasTypeChecking: boolean, hasTypeChecking: boolean,
onEvent: (status: WebpackStatus) => void onEvent: (status: WebpackStatus) => void
) { ) {
let tsMessagesPromise: Promise<CompilerDiagnostics> | undefined let tsMessagesPromise: Promise<CompilerDiagnosticsWithFile> | undefined
let tsMessagesResolver: (diagnostics: CompilerDiagnostics) => void let tsMessagesResolver: (diagnostics: CompilerDiagnosticsWithFile) => void
compiler.hooks.invalid.tap(`NextJsInvalid-${key}`, () => { compiler.hooks.invalid.tap(`NextJsInvalid-${key}`, () => {
tsMessagesPromise = undefined tsMessagesPromise = undefined
...@@ -259,10 +264,16 @@ export function watchCompilers( ...@@ -259,10 +264,16 @@ export function watchCompilers(
const errors = allMsgs const errors = allMsgs
.filter(msg => msg.severity === 'error') .filter(msg => msg.severity === 'error')
.map(format) .map(d => ({
file: d.file,
message: format(d),
}))
const warnings = allMsgs const warnings = allMsgs
.filter(msg => msg.severity === 'warning') .filter(msg => msg.severity === 'warning')
.map(format) .map(d => ({
file: d.file,
message: format(d),
}))
tsMessagesResolver({ tsMessagesResolver({
errors: errors.length ? errors : null, errors: errors.length ? errors : null,
...@@ -298,8 +309,29 @@ export function watchCompilers( ...@@ -298,8 +309,29 @@ export function watchCompilers(
return return
} }
stats.compilation.errors.push(...(typeMessages.errors || [])) const reportFiles = stats.compilation.modules
stats.compilation.warnings.push(...(typeMessages.warnings || [])) .map((m: any) => m.resource)
.filter(Boolean)
let filteredErrors = typeMessages.errors
? typeMessages.errors
.filter(({ file }) => file && reportFiles.includes(file))
.map(({ message }) => message)
: null
if (filteredErrors && filteredErrors.length < 1) {
filteredErrors = null
}
let filteredWarnings = typeMessages.warnings
? typeMessages.warnings
.filter(({ file }) => file && reportFiles.includes(file))
.map(({ message }) => message)
: null
if (filteredWarnings && filteredWarnings.length < 1) {
filteredWarnings = null
}
stats.compilation.errors.push(...(filteredErrors || []))
stats.compilation.warnings.push(...(filteredWarnings || []))
onTypeChecked({ onTypeChecked({
errors: stats.compilation.errors.length errors: stats.compilation.errors.length
? stats.compilation.errors ? stats.compilation.errors
...@@ -312,10 +344,10 @@ export function watchCompilers( ...@@ -312,10 +344,10 @@ export function watchCompilers(
onEvent({ onEvent({
loading: false, loading: false,
typeChecking: false, typeChecking: false,
errors: typeMessages.errors, errors: filteredErrors,
warnings: hasWarnings warnings: hasWarnings
? [...warnings, ...(typeMessages.warnings || [])] ? [...warnings, ...(filteredWarnings || [])]
: typeMessages.warnings, : filteredWarnings,
}) })
}) })
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
/* global jasmine */ /* global jasmine */
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import { writeFile, remove } from 'fs-extra'
import { import {
renderViaHTTP, renderViaHTTP,
nextBuild, nextBuild,
...@@ -34,6 +35,28 @@ describe('TypeScript Features', () => { ...@@ -34,6 +35,28 @@ describe('TypeScript Features', () => {
const $ = await get$('/hello') const $ = await get$('/hello')
expect($('body').text()).toMatch(/Hello World/) expect($('body').text()).toMatch(/Hello World/)
}) })
it('should not fail to render when an inactive page has an error', async () => {
await killApp(app)
let evilFile = join(appDir, 'pages', 'evil.tsx')
try {
await writeFile(
evilFile,
`import React from 'react'
export default function EvilPage(): JSX.Element {
return <div notARealProp />
}
`
)
app = await launchApp(appDir, appPort)
const $ = await get$('/hello')
expect($('body').text()).toMatch(/Hello World/)
} finally {
await remove(evilFile)
}
})
}) })
it('should compile the app', async () => { it('should compile the app', async () => {
......
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