-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathjson-stats-plugin.ts
40 lines (36 loc) · 1.27 KB
/
json-stats-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { createWriteStream, promises as fsPromises } from 'fs';
import { dirname } from 'path';
import { Compiler } from 'webpack';
import { assertIsError } from '../../../utils/error';
import { addError } from '../../../utils/webpack-diagnostics';
export class JsonStatsPlugin {
constructor(private readonly statsOutputPath: string) {}
apply(compiler: Compiler) {
compiler.hooks.done.tapPromise('angular-json-stats', async (stats) => {
const { stringifyStream } = await import('@discoveryjs/json-ext');
const data = stats.toJson('verbose');
try {
await fsPromises.mkdir(dirname(this.statsOutputPath), { recursive: true });
await new Promise<void>((resolve, reject) =>
stringifyStream(data)
.pipe(createWriteStream(this.statsOutputPath))
.on('close', resolve)
.on('error', reject),
);
} catch (error) {
assertIsError(error);
addError(
stats.compilation,
`Unable to write stats file: ${error.message || 'unknown error'}`,
);
}
});
}
}