Skip to content

Commit 75400fd

Browse files
committed
Improve error/warnings reporting
1 parent 174a81a commit 75400fd

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

packages/react-dev-utils/WebpackDevServerUtils.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function printInstructions(appName, urls, useYarn) {
100100
console.log();
101101
}
102102

103-
function createCompiler(webpack, config, appName, urls, useYarn, useTypeScript, reload) {
103+
function createCompiler(webpack, config, appName, urls, useYarn, useTypeScript, devSocket) {
104104
// "Compiler" is a low-level interface to Webpack.
105105
// It lets us listen to some events and provide our own custom messages.
106106
let compiler;
@@ -136,28 +136,15 @@ function createCompiler(webpack, config, appName, urls, useYarn, useTypeScript,
136136
});
137137
});
138138

139-
compiler.hooks.forkTsCheckerReceive.tap('fork-ts-checker-done', msgs => {
140-
const format = (message) => `${message.file}\n${typescriptFormatter(message, true)}`;
139+
compiler.hooks.forkTsCheckerReceive.tap('afterTypeScriptCheck', (diagnostics, lints) => {
140+
const allMsgs = [...diagnostics, ...lints];
141+
const format = message =>`${message.file}\n${typescriptFormatter(message, true)}`;
141142

142143
tsMessagesResolver({
143-
errors: msgs.filter(msg => msg.severity === 'error').map(format),
144-
warnings: msgs.filter(msg => msg.severity === 'warning').map(format)
144+
errors: allMsgs.filter(msg => msg.severity === 'error').map(format),
145+
warnings: allMsgs.filter(msg => msg.severity === 'warning').map(format),
145146
});
146147
});
147-
148-
compiler.hooks.afterCompile.tap('afterCompile', async compilation => {
149-
// If any errors already exist, skip this.
150-
if (compilation.errors.length > 0) {
151-
return;
152-
}
153-
154-
const messages = await tsMessagesPromise;
155-
compilation.errors.push(...messages.errors);
156-
compilation.warnings.push(...messages.warnings);
157-
if (messages.errors.length > 0 || messages.warnings.length > 0) {
158-
reload();
159-
}
160-
});
161148
}
162149

163150
// "done" event fires when Webpack has finished recompiling the bundle.
@@ -179,9 +166,20 @@ function createCompiler(webpack, config, appName, urls, useYarn, useTypeScript,
179166
chalk.yellow('Files successfully emitted, waiting for typecheck results...')
180167
);
181168

182-
const tsMessages = await tsMessagesPromise;
183-
statsData.errors.push(...tsMessages.errors);
184-
statsData.warnings.push(...tsMessages.warnings);
169+
const messages = await tsMessagesPromise;
170+
statsData.errors.push(...messages.errors);
171+
statsData.warnings.push(...messages.warnings);
172+
// Push errors and warnings into compilation result
173+
// to show them after page refresh triggered by user.
174+
stats.compilation.errors.push(...messages.errors);
175+
stats.compilation.warnings.push(...messages.warnings);
176+
177+
if (messages.errors.length > 0) {
178+
devSocket.errors(messages.errors);
179+
} else if (messages.warnings.length > 0) {
180+
devSocket.warnings(messages.warnings);
181+
}
182+
185183
process.stdout.clearLine();
186184
process.stdout.cursorTo(0);
187185
}

packages/react-dev-utils/typescriptFormatter.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ function formatter(message, useColors) {
4545
}
4646

4747
const severity = hasGetters ? message.getSeverity() : message.severity;
48+
const types = { diagnostic: 'TypeScript', lint: 'TSLint' };
4849

4950
return [
50-
messageColor.bold(`Type ${severity.toLowerCase()}: `) +
51+
messageColor.bold(`${types[message.type]} ${severity.toLowerCase()}: `) +
5152
(hasGetters ? message.getContent() : message.content) +
5253
' ' +
53-
messageColor.underline(`TS${message.code}`),
54+
messageColor.underline(
55+
(message.type === 'lint' ? 'Rule: ' : 'TS') + message.code
56+
),
5457
'',
5558
frame,
5659
].join(os.EOL);

packages/react-dev-utils/webpackHotDevClient.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,15 @@ function handleWarnings(warnings) {
140140
}
141141
}
142142

143+
printWarnings();
144+
143145
// Attempt to apply hot updates or reload.
144146
if (isHotUpdate) {
145147
tryApplyUpdates(function onSuccessfulHotUpdate() {
146-
// Only print warnings if we aren't refreshing the page.
147-
// Otherwise they'll disappear right away anyway.
148-
printWarnings();
149148
// Only dismiss it when we're sure it's a hot update.
150149
// Otherwise it would flicker right before the reload.
151150
ErrorOverlay.dismissBuildError();
152151
});
153-
} else {
154-
// Print initial warnings immediately.
155-
printWarnings();
156152
}
157153
}
158154

packages/react-scripts/scripts/start.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,12 @@ checkBrowsers(paths.appPath, isInteractive)
9696
const appName = require(paths.appPackageJson).name;
9797
const useTypeScript = fs.existsSync(paths.appTsConfig);
9898
const urls = prepareUrls(protocol, HOST, port);
99-
const reloadFn = () => devServer.sockWrite(devServer.sockets, 'content-changed');
99+
const devSocket = {
100+
warnings: warnings => devServer.sockWrite(devServer.sockets, 'warnings', warnings),
101+
errors: errors => devServer.sockWrite(devServer.sockets, 'errors', errors),
102+
};
100103
// Create a webpack compiler that is configured with custom messages.
101-
const compiler = createCompiler(webpack, config, appName, urls, useYarn, useTypeScript, reloadFn);
104+
const compiler = createCompiler(webpack, config, appName, urls, useYarn, useTypeScript, devSocket);
102105
// Load proxy config
103106
const proxySetting = require(paths.appPackageJson).proxy;
104107
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);

0 commit comments

Comments
 (0)