From af8dd62b746a6c3ae83ce54a31d0129775efa9a0 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 30 Mar 2022 10:08:51 +0200 Subject: [PATCH] refactor(@angular-devkit/build-angular): remove esbuild-check workaround This check is no longer needed as of `0.14.29` as now esbuild correctly propagates errors. See: https://github.com/evanw/esbuild/blob/master/CHANGELOG.md#01429 --- .../angular_devkit/build_angular/BUILD.bazel | 1 - .../build_angular/esbuild-check.js | 16 -------------- .../src/webpack/plugins/esbuild-executor.ts | 22 +++++-------------- .../plugins/javascript-optimizer-plugin.ts | 2 +- 4 files changed, 7 insertions(+), 34 deletions(-) delete mode 100644 packages/angular_devkit/build_angular/esbuild-check.js diff --git a/packages/angular_devkit/build_angular/BUILD.bazel b/packages/angular_devkit/build_angular/BUILD.bazel index 943ba14831..abbe7180d1 100644 --- a/packages/angular_devkit/build_angular/BUILD.bazel +++ b/packages/angular_devkit/build_angular/BUILD.bazel @@ -81,7 +81,6 @@ ts_library( include = [ "package.json", "builders.json", - "esbuild-check.js", "src/**/schema.json", "src/**/*.js", "src/**/*.html", diff --git a/packages/angular_devkit/build_angular/esbuild-check.js b/packages/angular_devkit/build_angular/esbuild-check.js deleted file mode 100644 index 10295ea96b..0000000000 --- a/packages/angular_devkit/build_angular/esbuild-check.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * @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 - */ - -// If the platform does not support the native variant of esbuild, this will crash. -// This script can then be spawned by the CLI to determine if native usage is supported. -require('esbuild') - .formatMessages([], { kind: 'error ' }) - .then( - () => {}, - () => {}, - ); diff --git a/packages/angular_devkit/build_angular/src/webpack/plugins/esbuild-executor.ts b/packages/angular_devkit/build_angular/src/webpack/plugins/esbuild-executor.ts index 68ab904d48..921ff5a857 100644 --- a/packages/angular_devkit/build_angular/src/webpack/plugins/esbuild-executor.ts +++ b/packages/angular_devkit/build_angular/src/webpack/plugins/esbuild-executor.ts @@ -6,14 +6,12 @@ * found in the LICENSE file at https://angular.io/license */ -import { spawnSync } from 'child_process'; import type { FormatMessagesOptions, PartialMessage, TransformOptions, TransformResult, } from 'esbuild'; -import * as path from 'path'; /** * Provides the ability to execute esbuild regardless of the current platform's support @@ -44,23 +42,15 @@ export class EsbuildExecutor /** * Determines whether the native variant of esbuild can be used on the current platform. * - * @returns True, if the native variant of esbuild is support; False, if the WASM variant is required. + * @returns A promise which resolves to `true`, if the native variant of esbuild is support or `false`, if the WASM variant is required. */ - static hasNativeSupport(): boolean { + static async hasNativeSupport(): Promise { // Try to use native variant to ensure it is functional for the platform. - // Spawning a separate esbuild check process is used to determine if the native - // variant is viable. If check fails, the WASM variant is initialized instead. - // Attempting to call one of the native esbuild functions is not a viable test - // currently since esbuild spawn errors are currently not propagated through the - // call stack for the esbuild function. If this limitation is removed in the future - // then the separate process spawn check can be removed in favor of a direct function - // call check. try { - const { status, error } = spawnSync(process.execPath, [ - path.join(__dirname, '../../../esbuild-check.js'), - ]); + const { formatMessages } = await import('esbuild'); + await formatMessages([], { kind: 'error' }); - return status === 0 && error === undefined; + return true; } catch { return false; } @@ -77,7 +67,7 @@ export class EsbuildExecutor } // If the WASM variant was preferred at class construction or native is not supported, use WASM - if (this.alwaysUseWasm || !EsbuildExecutor.hasNativeSupport()) { + if (this.alwaysUseWasm || !(await EsbuildExecutor.hasNativeSupport())) { await this.useWasm(); this.initialized = true; diff --git a/packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-plugin.ts b/packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-plugin.ts index bc252387f1..b12c9613cc 100644 --- a/packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-plugin.ts +++ b/packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-plugin.ts @@ -178,7 +178,7 @@ export class JavaScriptOptimizerPlugin { // Perform a single native esbuild support check. // This removes the need for each worker to perform the check which would // otherwise require spawning a separate process per worker. - alwaysUseWasm: !EsbuildExecutor.hasNativeSupport(), + alwaysUseWasm: !(await EsbuildExecutor.hasNativeSupport()), }; // Sort scripts so larger scripts start first - worker pool uses a FIFO queue