Skip to content

Commit

Permalink
Improve error/warnings reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
deftomat committed Dec 4, 2018
1 parent 174a81a commit 75400fd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
42 changes: 20 additions & 22 deletions packages/react-dev-utils/WebpackDevServerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function printInstructions(appName, urls, useYarn) {
console.log();
}

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

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

tsMessagesResolver({
errors: msgs.filter(msg => msg.severity === 'error').map(format),
warnings: msgs.filter(msg => msg.severity === 'warning').map(format)
errors: allMsgs.filter(msg => msg.severity === 'error').map(format),
warnings: allMsgs.filter(msg => msg.severity === 'warning').map(format),
});
});

compiler.hooks.afterCompile.tap('afterCompile', async compilation => {
// If any errors already exist, skip this.
if (compilation.errors.length > 0) {
return;
}

const messages = await tsMessagesPromise;
compilation.errors.push(...messages.errors);
compilation.warnings.push(...messages.warnings);
if (messages.errors.length > 0 || messages.warnings.length > 0) {
reload();
}
});
}

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

const tsMessages = await tsMessagesPromise;
statsData.errors.push(...tsMessages.errors);
statsData.warnings.push(...tsMessages.warnings);
const messages = await tsMessagesPromise;
statsData.errors.push(...messages.errors);
statsData.warnings.push(...messages.warnings);
// Push errors and warnings into compilation result
// to show them after page refresh triggered by user.
stats.compilation.errors.push(...messages.errors);
stats.compilation.warnings.push(...messages.warnings);

if (messages.errors.length > 0) {
devSocket.errors(messages.errors);
} else if (messages.warnings.length > 0) {
devSocket.warnings(messages.warnings);
}

process.stdout.clearLine();
process.stdout.cursorTo(0);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/react-dev-utils/typescriptFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ function formatter(message, useColors) {
}

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

return [
messageColor.bold(`Type ${severity.toLowerCase()}: `) +
messageColor.bold(`${types[message.type]} ${severity.toLowerCase()}: `) +
(hasGetters ? message.getContent() : message.content) +
' ' +
messageColor.underline(`TS${message.code}`),
messageColor.underline(
(message.type === 'lint' ? 'Rule: ' : 'TS') + message.code
),
'',
frame,
].join(os.EOL);
Expand Down
8 changes: 2 additions & 6 deletions packages/react-dev-utils/webpackHotDevClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,15 @@ function handleWarnings(warnings) {
}
}

printWarnings();

// Attempt to apply hot updates or reload.
if (isHotUpdate) {
tryApplyUpdates(function onSuccessfulHotUpdate() {
// Only print warnings if we aren't refreshing the page.
// Otherwise they'll disappear right away anyway.
printWarnings();
// Only dismiss it when we're sure it's a hot update.
// Otherwise it would flicker right before the reload.
ErrorOverlay.dismissBuildError();
});
} else {
// Print initial warnings immediately.
printWarnings();
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/react-scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ checkBrowsers(paths.appPath, isInteractive)
const appName = require(paths.appPackageJson).name;
const useTypeScript = fs.existsSync(paths.appTsConfig);
const urls = prepareUrls(protocol, HOST, port);
const reloadFn = () => devServer.sockWrite(devServer.sockets, 'content-changed');
const devSocket = {
warnings: warnings => devServer.sockWrite(devServer.sockets, 'warnings', warnings),
errors: errors => devServer.sockWrite(devServer.sockets, 'errors', errors),
};
// Create a webpack compiler that is configured with custom messages.
const compiler = createCompiler(webpack, config, appName, urls, useYarn, useTypeScript, reloadFn);
const compiler = createCompiler(webpack, config, appName, urls, useYarn, useTypeScript, devSocket);
// Load proxy config
const proxySetting = require(paths.appPackageJson).proxy;
const proxyConfig = prepareProxy(proxySetting, paths.appPublic);
Expand Down

0 comments on commit 75400fd

Please sign in to comment.