Skip to content

Commit d856b4d

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): support WASM-based esbuild optimizer fallback
In the event that the Angular CLI is executed on a platform that does not yet have native support for esbuild, the WASM-based variant of esbuild will now be used. If the first attempt to optimize a file fails to execute the native variant of esbuild, future executions will instead use the WASM-based variant instead which will execute regardless of the native platform. The WASM-based variant, unfortunately, can be significantly slower than the native version (some cases can be several times slower). For install time concerns regarding the esbuild post-install step, esbuild is now listed as an optional dependency which will allow the post-install step to fail but allow the full npm install to pass. This install scenario should only occur in the event that the esbuild native binary cannot be installed or is otherwise unavailable.
1 parent 2e752e7 commit d856b4d

File tree

10 files changed

+302
-21
lines changed

10 files changed

+302
-21
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@
148148
"css-loader": "6.2.0",
149149
"css-minimizer-webpack-plugin": "3.0.2",
150150
"debug": "^4.1.1",
151-
"esbuild": "0.12.24",
151+
"esbuild": "0.12.29",
152+
"esbuild-wasm": "0.12.29",
152153
"eslint": "7.31.0",
153154
"eslint-config-prettier": "8.3.0",
154155
"eslint-plugin-header": "3.1.1",

packages/angular_devkit/build_angular/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ ts_library(
9191
include = [
9292
"package.json",
9393
"builders.json",
94+
"esbuild-check.js",
9495
"src/**/schema.json",
9596
"src/**/*.js",
9697
"src/**/*.html",
@@ -150,6 +151,7 @@ ts_library(
150151
"@npm//css-loader",
151152
"@npm//css-minimizer-webpack-plugin",
152153
"@npm//esbuild",
154+
"@npm//esbuild-wasm",
153155
"@npm//find-cache-dir",
154156
"@npm//glob",
155157
"@npm//https-proxy-agent",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC 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+
// If the platform does not support the native variant of esbuild, this will crash.
10+
// This script can then be spawned by the CLI to determine if native usage is supported.
11+
require('esbuild')
12+
.formatMessages([], { kind: 'error ' })
13+
.then(
14+
() => {},
15+
() => {},
16+
);

packages/angular_devkit/build_angular/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"critters": "0.0.10",
3535
"css-loader": "6.2.0",
3636
"css-minimizer-webpack-plugin": "3.0.2",
37-
"esbuild": "0.12.24",
37+
"esbuild-wasm": "0.12.29",
3838
"find-cache-dir": "3.3.1",
3939
"glob": "7.1.7",
4040
"https-proxy-agent": "5.0.0",
@@ -76,6 +76,9 @@
7676
"webpack-merge": "5.8.0",
7777
"webpack-subresource-integrity": "1.5.2"
7878
},
79+
"optionalDependencies": {
80+
"esbuild": "0.12.29"
81+
},
7982
"peerDependencies": {
8083
"@angular/compiler-cli": "^12.0.0",
8184
"@angular/localize": "^12.0.0",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
RemoveHashPlugin,
2121
SuppressExtractedTextChunksWebpackPlugin,
2222
} from '../plugins';
23+
import { EsbuildExecutor } from '../plugins/esbuild-executor';
2324
import {
2425
assetNameTemplateFactory,
2526
getOutputHashFormat,
@@ -277,7 +278,7 @@ export function getStylesConfig(wco: WebpackConfigOptions): webpack.Configuratio
277278
const extraMinimizers = [];
278279
if (buildOptions.optimization.styles.minify) {
279280
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
280-
const esbuild = require('esbuild') as typeof import('esbuild');
281+
const esbuild = new EsbuildExecutor();
281282

282283
const cssnanoOptions = {
283284
preset: [
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC 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+
import { spawnSync } from 'child_process';
10+
import type {
11+
FormatMessagesOptions,
12+
PartialMessage,
13+
TransformOptions,
14+
TransformResult,
15+
} from 'esbuild';
16+
import * as path from 'path';
17+
18+
/**
19+
* Provides the ability to execute esbuild regardless of the current platform's support
20+
* for using the native variant of esbuild. The native variant will be preferred (assuming
21+
* the `alwaysUseWasm` constructor option is `false) due to its inherent performance advantages.
22+
* At first use of esbuild, a supportability test will be automatically performed and the
23+
* WASM-variant will be used if needed by the platform.
24+
*/
25+
export class EsbuildExecutor
26+
implements Pick<typeof import('esbuild'), 'transform' | 'formatMessages'>
27+
{
28+
private esbuildTransform: this['transform'];
29+
private esbuildFormatMessages: this['formatMessages'];
30+
private initialized = false;
31+
32+
/**
33+
* Constructs an instance of the `EsbuildExecutor` class.
34+
*
35+
* @param alwaysUseWasm If true, the WASM-variant will be preferred and no support test will be
36+
* performed; if false (default), the native variant will be preferred.
37+
*/
38+
constructor(private alwaysUseWasm = false) {
39+
this.esbuildTransform = this.esbuildFormatMessages = () => {
40+
throw new Error('esbuild implementation missing');
41+
};
42+
}
43+
44+
/**
45+
* Determines whether the native variant of esbuild can be used on the current platform.
46+
*
47+
* @returns True, if the native variant of esbuild is support; False, if the WASM variant is required.
48+
*/
49+
static hasNativeSupport(): boolean {
50+
// Try to use native variant to ensure it is functional for the platform.
51+
// Spawning a separate esbuild check process is used to determine if the native
52+
// variant is viable. If check fails, the WASM variant is initialized instead.
53+
// Attempting to call one of the native esbuild functions is not a viable test
54+
// currently since esbuild spawn errors are currently not propagated through the
55+
// call stack for the esbuild function. If this limitation is removed in the future
56+
// then the separate process spawn check can be removed in favor of a direct function
57+
// call check.
58+
try {
59+
const { status, error } = spawnSync(process.execPath, [
60+
path.join(__dirname, '../../../esbuild-check.js'),
61+
]);
62+
63+
return status === 0 && error === undefined;
64+
} catch {
65+
return false;
66+
}
67+
}
68+
69+
/**
70+
* Initializes the esbuild transform and format messages functions.
71+
*
72+
* @returns A promise that fulfills when esbuild has been loaded and available for use.
73+
*/
74+
private async ensureEsbuild(): Promise<void> {
75+
if (this.initialized) {
76+
return;
77+
}
78+
79+
// If the WASM variant was preferred at class construction or native is not supported, use WASM
80+
if (this.alwaysUseWasm || !EsbuildExecutor.hasNativeSupport()) {
81+
await this.useWasm();
82+
this.initialized = true;
83+
84+
return;
85+
}
86+
87+
try {
88+
// Use the faster native variant if available.
89+
const { transform, formatMessages } = await import('esbuild');
90+
91+
this.esbuildTransform = transform;
92+
this.esbuildFormatMessages = formatMessages;
93+
} catch {
94+
// If the native variant is not installed then use the WASM-based variant
95+
await this.useWasm();
96+
}
97+
98+
this.initialized = true;
99+
}
100+
101+
/**
102+
* Transitions an executor instance to use the WASM-variant of esbuild.
103+
*/
104+
private async useWasm(): Promise<void> {
105+
const { transform, formatMessages } = await import('esbuild-wasm');
106+
this.esbuildTransform = transform;
107+
this.esbuildFormatMessages = formatMessages;
108+
109+
// The ESBUILD_BINARY_PATH environment variable cannot exist when attempting to use the
110+
// WASM variant. If it is then the binary located at the specified path will be used instead
111+
// of the WASM variant.
112+
delete process.env.ESBUILD_BINARY_PATH;
113+
114+
this.alwaysUseWasm = true;
115+
}
116+
117+
async transform(input: string, options?: TransformOptions): Promise<TransformResult> {
118+
await this.ensureEsbuild();
119+
120+
return this.esbuildTransform(input, options);
121+
}
122+
123+
async formatMessages(
124+
messages: PartialMessage[],
125+
options: FormatMessagesOptions,
126+
): Promise<string[]> {
127+
await this.ensureEsbuild();
128+
129+
return this.esbuildFormatMessages(messages, options);
130+
}
131+
}

packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-plugin.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Piscina from 'piscina';
1010
import { ScriptTarget } from 'typescript';
1111
import type { Compiler, sources } from 'webpack';
1212
import { maxWorkers } from '../../utils/environment-options';
13+
import { EsbuildExecutor } from './esbuild-executor';
1314

1415
/**
1516
* The maximum number of Workers that will be created to execute optimize tasks.
@@ -160,6 +161,10 @@ export class JavaScriptOptimizerPlugin {
160161
target,
161162
removeLicenses: this.options.removeLicenses,
162163
advanced: this.options.advanced,
164+
// Perform a single native esbuild support check.
165+
// This removes the need for each worker to perform the check which would
166+
// otherwise require spawning a separate process per worker.
167+
alwaysUseWasm: !EsbuildExecutor.hasNativeSupport(),
163168
};
164169

165170
// Sort scripts so larger scripts start first - worker pool uses a FIFO queue

0 commit comments

Comments
 (0)