From ae261192842401eeefa6cf079f57b199ce520984 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Tue, 16 Mar 2021 11:31:13 -0400 Subject: [PATCH] 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. (cherry picked from commit d5645675fd555e7f1afd523d4f2d42095034fc46) --- package.json | 1 + .../angular_devkit/build_angular/BUILD.bazel | 1 + .../angular_devkit/build_angular/package.json | 1 + .../build_angular/src/babel-bazel.d.ts | 22 +++++++++++++++++++ .../build_angular/src/typings.d.ts | 16 ++------------ .../src/webpack/configs/common.ts | 17 +++++++++----- tsconfig.json | 2 +- yarn.lock | 11 +++++++--- 8 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 packages/angular_devkit/build_angular/src/babel-bazel.d.ts diff --git a/package.json b/package.json index 617c5221a359..e1bd0cf5316d 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ "@bazel/buildifier": "4.0.0", "@bazel/jasmine": "3.2.1", "@bazel/typescript": "3.2.1", + "@discoveryjs/json-ext": "0.5.2", "@jsdevtools/coverage-istanbul-loader": "3.0.5", "@types/babel__core": "7.1.12", "@types/babel__template": "7.4.0", diff --git a/packages/angular_devkit/build_angular/BUILD.bazel b/packages/angular_devkit/build_angular/BUILD.bazel index eb1551975fe4..7499a11dee6d 100644 --- a/packages/angular_devkit/build_angular/BUILD.bazel +++ b/packages/angular_devkit/build_angular/BUILD.bazel @@ -113,6 +113,7 @@ ts_library( "@npm//@babel/preset-env", "@npm//@babel/runtime", "@npm//@babel/template", + "@npm//@discoveryjs/json-ext", "@npm//@jsdevtools/coverage-istanbul-loader", "@npm//@types/babel__core", "@npm//@types/babel__template", diff --git a/packages/angular_devkit/build_angular/package.json b/packages/angular_devkit/build_angular/package.json index e8af6ef3bdd7..386c05e88698 100644 --- a/packages/angular_devkit/build_angular/package.json +++ b/packages/angular_devkit/build_angular/package.json @@ -18,6 +18,7 @@ "@babel/preset-env": "7.12.11", "@babel/runtime": "7.12.5", "@babel/template": "7.12.7", + "@discoveryjs/json-ext": "0.5.2", "@jsdevtools/coverage-istanbul-loader": "3.0.5", "@ngtools/webpack": "0.0.0", "ansi-colors": "4.1.1", diff --git a/packages/angular_devkit/build_angular/src/babel-bazel.d.ts b/packages/angular_devkit/build_angular/src/babel-bazel.d.ts new file mode 100644 index 000000000000..9a5d06cfa771 --- /dev/null +++ b/packages/angular_devkit/build_angular/src/babel-bazel.d.ts @@ -0,0 +1,22 @@ +/** + * @license + * Copyright Google Inc. 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 + */ + +// Workaround for https://github.com/bazelbuild/rules_nodejs/issues/1033 +// Alternative approach instead of https://github.com/angular/angular/pull/33226 +declare module '@babel/core' { + export * from '@types/babel__core'; +} +declare module '@babel/generator' { + export { default } from '@types/babel__generator'; +} +declare module '@babel/traverse' { + export { default } from '@types/babel__traverse'; +} +declare module '@babel/template' { + export { default } from '@types/babel__template'; +} diff --git a/packages/angular_devkit/build_angular/src/typings.d.ts b/packages/angular_devkit/build_angular/src/typings.d.ts index 9a5d06cfa771..bdf566191697 100644 --- a/packages/angular_devkit/build_angular/src/typings.d.ts +++ b/packages/angular_devkit/build_angular/src/typings.d.ts @@ -5,18 +5,6 @@ * 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 */ - -// Workaround for https://github.com/bazelbuild/rules_nodejs/issues/1033 -// Alternative approach instead of https://github.com/angular/angular/pull/33226 -declare module '@babel/core' { - export * from '@types/babel__core'; -} -declare module '@babel/generator' { - export { default } from '@types/babel__generator'; -} -declare module '@babel/traverse' { - export { default } from '@types/babel__traverse'; -} -declare module '@babel/template' { - export { default } from '@types/babel__template'; +declare module '@discoveryjs/json-ext' { + export function stringifyStream(value: unknown): import('stream').Readable; } diff --git a/packages/angular_devkit/build_angular/src/webpack/configs/common.ts b/packages/angular_devkit/build_angular/src/webpack/configs/common.ts index f96286ee3c7b..fe28356ff6bc 100644 --- a/packages/angular_devkit/build_angular/src/webpack/configs/common.ts +++ b/packages/angular_devkit/build_angular/src/webpack/configs/common.ts @@ -10,7 +10,7 @@ import { buildOptimizerLoaderPath, } from '@angular-devkit/build-optimizer'; import * as CopyWebpackPlugin from 'copy-webpack-plugin'; -import { existsSync } from 'fs'; +import { createWriteStream, existsSync } from 'fs'; import * as path from 'path'; import { RollupOptions } from 'rollup'; import { ScriptTarget } from 'typescript'; @@ -23,7 +23,6 @@ import { compilation, debug, } from 'webpack'; -import { RawSource } from 'webpack-sources'; import { AssetPatternClass } from '../../browser/schema'; import { BuildBrowserFeatures, maxWorkers } from '../../utils'; import { WebpackConfigOptions } from '../../utils/build-options'; @@ -339,9 +338,17 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration { extraPlugins.push( new (class { apply(compiler: Compiler) { - compiler.hooks.emit.tap('angular-cli-stats', compilation => { - const data = JSON.stringify(compilation.getStats().toJson('verbose'), undefined, 2); - compilation.assets['stats.json'] = new RawSource(data); + compiler.hooks.done.tapPromise('angular-cli-stats', async (stats) => { + const { stringifyStream } = await import('@discoveryjs/json-ext'); + const data = stats.toJson('verbose'); + const statsOutputPath = path.join(stats.compilation.outputOptions.path, 'stats.json'); + + return new Promise((resolve, reject) => + stringifyStream(data) + .pipe(createWriteStream(statsOutputPath)) + .on('close', resolve) + .on('error', reject), + ); }); } })(), diff --git a/tsconfig.json b/tsconfig.json index eae8b3dabd56..08116ccb5bbf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -57,7 +57,7 @@ "suppressTsconfigOverrideWarnings": true }, "exclude": [ - "packages/angular_devkit/build_angular/src/typings.d.ts", + "packages/angular_devkit/build_angular/src/bazel-babel.d.ts", "bazel-out/**/*", "dist/**/*", "dist-schema/**", diff --git a/yarn.lock b/yarn.lock index 284dafeb25ff..8f83d048db53 100644 --- a/yarn.lock +++ b/yarn.lock @@ -87,7 +87,7 @@ "@angular/dev-infra-private@https://github.com/angular/dev-infra-private-builds.git#0fd0f441648577e8c3966e9b59a88fefe350f514": version "0.0.0" - resolved "https://github.com/angular/dev-infra-private-builds.git#0fd0f441648577e8c3966e9b59a88fefe350f514" + resolved "https://github.com/angular/dev-infra-private-builds.git#a6b42dabedcf7c724bd36a614ebe9fa99c777e62" dependencies: "@angular/benchpress" "0.2.1" "@bazel/buildifier" "^0.29.0" @@ -103,13 +103,13 @@ inquirer-autocomplete-prompt "^1.0.2" minimatch "^3.0.4" multimatch "^4.0.0" - node-fetch "^2.6.0" + node-fetch "^2.6.1" node-uuid "1.4.8" ora "^5.0.0" protractor "^5.4.2" selenium-webdriver "3.5.0" semver "^6.3.0" - shelljs "^0.8.3" + shelljs "^0.8.4" ts-node "^8.6.2" tslib "^2.0.0" typed-graphqlify "^2.3.0" @@ -1076,6 +1076,11 @@ resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== +"@discoveryjs/json-ext@0.5.2": + version "0.5.2" + resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.2.tgz#8f03a22a04de437254e8ce8cc84ba39689288752" + integrity sha512-HyYEUDeIj5rRQU2Hk5HTB2uHsbRQpF70nvMhVzi+VJR0X+xNEhjPui4/kBf3VeH/wqD28PT4sVOm8qqLjBrSZg== + "@istanbuljs/schema@^0.1.2": version "0.1.2" resolved "https://registry.yarnpkg.com/@istanbuljs/schema/-/schema-0.1.2.tgz#26520bf09abe4a5644cd5414e37125a8954241dd"