Skip to content

Commit d564567

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): support writing large Webpack stat outputs
When using the `statsJson` browser builder option, the resulting JSON data is now streamed into a file instead of written in one large block. This mitigates crashes due to the generated string exceeded the Node.js limit.
1 parent f815e08 commit d564567

File tree

8 files changed

+45
-21
lines changed

8 files changed

+45
-21
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"@bazel/buildifier": "4.0.1",
9090
"@bazel/jasmine": "3.2.2",
9191
"@bazel/typescript": "3.2.2",
92+
"@discoveryjs/json-ext": "0.5.2",
9293
"@jsdevtools/coverage-istanbul-loader": "3.0.5",
9394
"@types/babel__core": "7.1.13",
9495
"@types/babel__template": "7.4.0",

packages/angular_devkit/build_angular/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ ts_library(
113113
"@npm//@babel/preset-env",
114114
"@npm//@babel/runtime",
115115
"@npm//@babel/template",
116+
"@npm//@discoveryjs/json-ext",
116117
"@npm//@jsdevtools/coverage-istanbul-loader",
117118
"@npm//@types/babel__core",
118119
"@npm//@types/babel__template",

packages/angular_devkit/build_angular/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@babel/preset-env": "7.13.9",
1919
"@babel/runtime": "7.13.9",
2020
"@babel/template": "7.12.13",
21+
"@discoveryjs/json-ext": "0.5.2",
2122
"@jsdevtools/coverage-istanbul-loader": "3.0.5",
2223
"@ngtools/webpack": "0.0.0",
2324
"ansi-colors": "4.1.1",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
// Workaround for https://github.com/bazelbuild/rules_nodejs/issues/1033
10+
// Alternative approach instead of https://github.com/angular/angular/pull/33226
11+
declare module '@babel/core' {
12+
export * from '@types/babel__core';
13+
}
14+
declare module '@babel/generator' {
15+
export { default } from '@types/babel__generator';
16+
}
17+
declare module '@babel/traverse' {
18+
export { default } from '@types/babel__traverse';
19+
}
20+
declare module '@babel/template' {
21+
export { default } from '@types/babel__template';
22+
}

packages/angular_devkit/build_angular/src/typings.d.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
9-
// Workaround for https://github.com/bazelbuild/rules_nodejs/issues/1033
10-
// Alternative approach instead of https://github.com/angular/angular/pull/33226
11-
declare module '@babel/core' {
12-
export * from '@types/babel__core';
13-
}
14-
declare module '@babel/generator' {
15-
export { default } from '@types/babel__generator';
16-
}
17-
declare module '@babel/traverse' {
18-
export { default } from '@types/babel__traverse';
19-
}
20-
declare module '@babel/template' {
21-
export { default } from '@types/babel__template';
8+
declare module '@discoveryjs/json-ext' {
9+
export function stringifyStream(value: unknown): import('stream').Readable;
2210
}

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
buildOptimizerLoaderPath,
1111
} from '@angular-devkit/build-optimizer';
1212
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
13-
import { existsSync } from 'fs';
13+
import { createWriteStream, existsSync } from 'fs';
1414
import * as path from 'path';
1515
import { ScriptTarget } from 'typescript';
1616
import {
@@ -21,7 +21,6 @@ import {
2121
compilation,
2222
debug,
2323
} from 'webpack';
24-
import { RawSource } from 'webpack-sources';
2524
import { AssetPatternClass } from '../../browser/schema';
2625
import { BuildBrowserFeatures, maxWorkers } from '../../utils';
2726
import { WebpackConfigOptions } from '../../utils/build-options';
@@ -290,9 +289,17 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
290289
extraPlugins.push(
291290
new (class {
292291
apply(compiler: Compiler) {
293-
compiler.hooks.emit.tap('angular-cli-stats', compilation => {
294-
const data = JSON.stringify(compilation.getStats().toJson('verbose'), undefined, 2);
295-
compilation.assets['stats.json'] = new RawSource(data);
292+
compiler.hooks.done.tapPromise('angular-cli-stats', async (stats) => {
293+
const { stringifyStream } = await import('@discoveryjs/json-ext');
294+
const data = stats.toJson('verbose');
295+
const statsOutputPath = path.join(stats.compilation.outputOptions.path, 'stats.json');
296+
297+
return new Promise<void>((resolve, reject) =>
298+
stringifyStream(data)
299+
.pipe(createWriteStream(statsOutputPath))
300+
.on('close', resolve)
301+
.on('error', reject),
302+
);
296303
});
297304
}
298305
})(),

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"suppressTsconfigOverrideWarnings": true
5858
},
5959
"exclude": [
60-
"packages/angular_devkit/build_angular/src/typings.d.ts",
60+
"packages/angular_devkit/build_angular/src/bazel-babel.d.ts",
6161
"bazel-out/**/*",
6262
"dist/**/*",
6363
"dist-schema/**",

yarn.lock

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787

8888
"@angular/dev-infra-private@https://github.com/angular/dev-infra-private-builds.git#a6b42dabedcf7c724bd36a614ebe9fa99c777e62":
8989
version "0.0.0"
90-
uid a6b42dabedcf7c724bd36a614ebe9fa99c777e62
9190
resolved "https://github.com/angular/dev-infra-private-builds.git#a6b42dabedcf7c724bd36a614ebe9fa99c777e62"
9291
dependencies:
9392
"@angular/benchpress" "0.2.1"
@@ -1236,6 +1235,11 @@
12361235
resolved "https://registry.yarnpkg.com/@csstools/convert-colors/-/convert-colors-1.4.0.tgz#ad495dc41b12e75d588c6db8b9834f08fa131eb7"
12371236
integrity sha512-5a6wqoJV/xEdbRNKVo6I4hO3VjyDq//8q2f9I6PBAvMesJHFauXDorcNCsr9RzvsZnaWi5NYCcfyqP1QeFHFbw==
12381237

1238+
"@discoveryjs/[email protected]":
1239+
version "0.5.2"
1240+
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.2.tgz#8f03a22a04de437254e8ce8cc84ba39689288752"
1241+
integrity sha512-HyYEUDeIj5rRQU2Hk5HTB2uHsbRQpF70nvMhVzi+VJR0X+xNEhjPui4/kBf3VeH/wqD28PT4sVOm8qqLjBrSZg==
1242+
12391243
"@istanbuljs/schema@^0.1.2":
12401244
version "0.1.3"
12411245
resolved "https://registry.yarnpkg.com/@istanbuljs/schema/-/schema-0.1.3.tgz#e45e384e4b8ec16bce2fd903af78450f6bf7ec98"

0 commit comments

Comments
 (0)