(options: Options, isWatchCommand: boolean)
| 3467 | } |
| 3468 | |
| 3469 | async runWebpack(options: Options, isWatchCommand: boolean): Promise<void> { |
| 3470 | let compiler: Compiler | MultiCompiler; |
| 3471 | let stringifyChunked: typeof stringifyChunkedType; |
| 3472 | let Readable: typeof ReadableType; |
| 3473 | |
| 3474 | if (options.json) { |
| 3475 | ({ stringifyChunked } = await import("@discoveryjs/json-ext")); |
| 3476 | ({ Readable } = await import("node:stream")); |
| 3477 | } |
| 3478 | |
| 3479 | const callback: WebpackCallback = (error, stats): void => { |
| 3480 | if (error) { |
| 3481 | this.logger.error(error); |
| 3482 | process.exit(2); |
| 3483 | } |
| 3484 | |
| 3485 | if (stats && (stats.hasErrors() || (options.failOnWarnings && stats.hasWarnings()))) { |
| 3486 | process.exitCode = 1; |
| 3487 | } |
| 3488 | |
| 3489 | if (!compiler || !stats) { |
| 3490 | return; |
| 3491 | } |
| 3492 | |
| 3493 | const statsOptions = this.isMultipleCompiler(compiler) |
| 3494 | ? { |
| 3495 | children: compiler.compilers.map((compiler) => compiler.options.stats), |
| 3496 | } |
| 3497 | : compiler.options.stats; |
| 3498 | |
| 3499 | if (options.json) { |
| 3500 | const handleWriteError = (error: Error) => { |
| 3501 | this.logger.error(error); |
| 3502 | process.exit(2); |
| 3503 | }; |
| 3504 | |
| 3505 | if (options.json === true) { |
| 3506 | Readable.from(stringifyChunked(stats.toJson(statsOptions as StatsOptions))) |
| 3507 | .on("error", handleWriteError) |
| 3508 | .pipe(process.stdout) |
| 3509 | .on("error", handleWriteError) |
| 3510 | .on("close", () => process.stdout.write("\n")); |
| 3511 | } else { |
| 3512 | Readable.from(stringifyChunked(stats.toJson(statsOptions as StatsOptions))) |
| 3513 | .on("error", handleWriteError) |
| 3514 | .pipe(fs.createWriteStream(options.json)) |
| 3515 | .on("error", handleWriteError) |
| 3516 | // Use stderr to logging |
| 3517 | .on("close", () => { |
| 3518 | process.stderr.write( |
| 3519 | `[webpack-cli] ${this.colors.green( |
| 3520 | `stats are successfully stored as json to ${options.json}`, |
| 3521 | )}\n`, |
| 3522 | ); |
| 3523 | }); |
| 3524 | } |
| 3525 | } else { |
| 3526 | const printedStats = stats.toString(statsOptions); |
no test coverage detected