Skip to content

Commit b48f98a

Browse files
committed
feat: add stats to async mode
It's easier to track progress with stats. ✅ Closes: #635
1 parent 0660810 commit b48f98a

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/formatter/stats-formatter.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import chalk from 'chalk';
2+
import type { Stats } from 'webpack';
3+
4+
import type { Issue } from '../issue';
5+
6+
// mimics webpack's stats summary formatter
7+
export function statsFormatter(issues: Issue[], stats: Stats): string {
8+
const errorsNumber = issues.filter((issue) => issue.severity === 'error').length;
9+
const warningsNumber = issues.filter((issue) => issue.severity === 'warning').length;
10+
const errorsFormatted = errorsNumber
11+
? chalk.red.bold(`${errorsNumber} ${errorsNumber === 1 ? 'error' : 'errors'}`)
12+
: '';
13+
const warningsFormatted = warningsNumber
14+
? chalk.yellow.bold(`${warningsNumber} ${warningsNumber === 1 ? 'warning' : 'warnings'}`)
15+
: '';
16+
const timeFormatted = Math.round(Date.now() - stats.startTime);
17+
18+
return [
19+
'Found ',
20+
errorsFormatted,
21+
errorsFormatted && warningsFormatted ? ' and ' : '',
22+
warningsFormatted,
23+
` in ${timeFormatted} ms`,
24+
'.',
25+
].join('');
26+
}

src/hooks/tap-done-to-async-get-issues.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import chalk from 'chalk';
22
import type webpack from 'webpack';
33

4+
import { statsFormatter } from '../formatter/stats-formatter';
45
import { createWebpackFormatter } from '../formatter/webpack-formatter';
56
import { getInfrastructureLogger } from '../infrastructure-logger';
67
import type { Issue } from '../issue';
@@ -17,7 +18,7 @@ function tapDoneToAsyncGetIssues(
1718
state: ForkTsCheckerWebpackPluginState
1819
) {
1920
const hooks = getPluginHooks(compiler);
20-
const { log, debug } = getInfrastructureLogger(compiler);
21+
const { debug } = getInfrastructureLogger(compiler);
2122

2223
compiler.hooks.done.tap('ForkTsCheckerWebpackPlugin', async (stats) => {
2324
if (stats.compilation.compiler !== compiler) {
@@ -31,7 +32,7 @@ function tapDoneToAsyncGetIssues(
3132
try {
3233
if (await isPending(issuesPromise)) {
3334
hooks.waiting.call(stats.compilation);
34-
config.logger.log(chalk.cyan('Issues checking in progress...'));
35+
config.logger.log(chalk.cyan('Type-checking in progress...'));
3536
} else {
3637
// wait 10ms to log issues after webpack stats
3738
await wait(10);
@@ -60,8 +61,11 @@ function tapDoneToAsyncGetIssues(
6061
if (issues.length) {
6162
// follow webpack's approach - one process.write to stderr with all errors and warnings
6263
config.logger.error(issues.map((issue) => formatter(issue)).join('\n'));
64+
65+
// print stats of the compilation
66+
config.logger.log(statsFormatter(issues, stats));
6367
} else {
64-
config.logger.log(chalk.green('No issues found.'));
68+
config.logger.log(chalk.green('No errors found.'));
6569
}
6670

6771
// report issues to webpack-dev-server, if it's listening
@@ -80,10 +84,6 @@ function tapDoneToAsyncGetIssues(
8084
debug('Sending issues to the webpack-dev-server.');
8185
state.webpackDevServerDoneTap.fn(stats);
8286
}
83-
84-
if (stats.startTime) {
85-
log(`Time: ${Math.round(Date.now() - stats.startTime).toString()} ms`);
86-
}
8787
});
8888
}
8989

test/e2e/driver/webpack-dev-server-driver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function createWebpackDevServerDriver(
5757
process.stdout.on('data', (data) => {
5858
const content = stripAnsi(data.toString());
5959

60-
if (async && content.includes('No issues found.')) {
60+
if (async && content.includes('No errors found.')) {
6161
noErrorsListener.resolve();
6262
}
6363

0 commit comments

Comments
 (0)