|
| 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 | +} |
0 commit comments