[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
| 10 | if (k2 === undefined) k2 = k;
|
---|
| 11 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
| 12 | }) : (function(o, m, k, k2) {
|
---|
| 13 | if (k2 === undefined) k2 = k;
|
---|
| 14 | o[k2] = m[k];
|
---|
| 15 | }));
|
---|
| 16 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
| 17 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
| 18 | }) : function(o, v) {
|
---|
| 19 | o["default"] = v;
|
---|
| 20 | });
|
---|
| 21 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
| 22 | if (mod && mod.__esModule) return mod;
|
---|
| 23 | var result = {};
|
---|
| 24 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
| 25 | __setModuleDefault(result, mod);
|
---|
| 26 | return result;
|
---|
| 27 | };
|
---|
| 28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 29 | exports.EsbuildExecutor = void 0;
|
---|
| 30 | const child_process_1 = require("child_process");
|
---|
| 31 | const path = __importStar(require("path"));
|
---|
| 32 | /**
|
---|
| 33 | * Provides the ability to execute esbuild regardless of the current platform's support
|
---|
| 34 | * for using the native variant of esbuild. The native variant will be preferred (assuming
|
---|
| 35 | * the `alwaysUseWasm` constructor option is `false) due to its inherent performance advantages.
|
---|
| 36 | * At first use of esbuild, a supportability test will be automatically performed and the
|
---|
| 37 | * WASM-variant will be used if needed by the platform.
|
---|
| 38 | */
|
---|
| 39 | class EsbuildExecutor {
|
---|
| 40 | /**
|
---|
| 41 | * Constructs an instance of the `EsbuildExecutor` class.
|
---|
| 42 | *
|
---|
| 43 | * @param alwaysUseWasm If true, the WASM-variant will be preferred and no support test will be
|
---|
| 44 | * performed; if false (default), the native variant will be preferred.
|
---|
| 45 | */
|
---|
| 46 | constructor(alwaysUseWasm = false) {
|
---|
| 47 | this.alwaysUseWasm = alwaysUseWasm;
|
---|
| 48 | this.initialized = false;
|
---|
| 49 | this.esbuildTransform = this.esbuildFormatMessages = () => {
|
---|
| 50 | throw new Error('esbuild implementation missing');
|
---|
| 51 | };
|
---|
| 52 | }
|
---|
| 53 | /**
|
---|
| 54 | * Determines whether the native variant of esbuild can be used on the current platform.
|
---|
| 55 | *
|
---|
| 56 | * @returns True, if the native variant of esbuild is support; False, if the WASM variant is required.
|
---|
| 57 | */
|
---|
| 58 | static hasNativeSupport() {
|
---|
| 59 | // Try to use native variant to ensure it is functional for the platform.
|
---|
| 60 | // Spawning a separate esbuild check process is used to determine if the native
|
---|
| 61 | // variant is viable. If check fails, the WASM variant is initialized instead.
|
---|
| 62 | // Attempting to call one of the native esbuild functions is not a viable test
|
---|
| 63 | // currently since esbuild spawn errors are currently not propagated through the
|
---|
| 64 | // call stack for the esbuild function. If this limitation is removed in the future
|
---|
| 65 | // then the separate process spawn check can be removed in favor of a direct function
|
---|
| 66 | // call check.
|
---|
| 67 | try {
|
---|
| 68 | const { status, error } = child_process_1.spawnSync(process.execPath, [
|
---|
| 69 | path.join(__dirname, '../../../esbuild-check.js'),
|
---|
| 70 | ]);
|
---|
| 71 | return status === 0 && error === undefined;
|
---|
| 72 | }
|
---|
| 73 | catch {
|
---|
| 74 | return false;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | /**
|
---|
| 78 | * Initializes the esbuild transform and format messages functions.
|
---|
| 79 | *
|
---|
| 80 | * @returns A promise that fulfills when esbuild has been loaded and available for use.
|
---|
| 81 | */
|
---|
| 82 | async ensureEsbuild() {
|
---|
| 83 | if (this.initialized) {
|
---|
| 84 | return;
|
---|
| 85 | }
|
---|
| 86 | // If the WASM variant was preferred at class construction or native is not supported, use WASM
|
---|
| 87 | if (this.alwaysUseWasm || !EsbuildExecutor.hasNativeSupport()) {
|
---|
| 88 | await this.useWasm();
|
---|
| 89 | this.initialized = true;
|
---|
| 90 | return;
|
---|
| 91 | }
|
---|
| 92 | try {
|
---|
| 93 | // Use the faster native variant if available.
|
---|
| 94 | const { transform, formatMessages } = await Promise.resolve().then(() => __importStar(require('esbuild')));
|
---|
| 95 | this.esbuildTransform = transform;
|
---|
| 96 | this.esbuildFormatMessages = formatMessages;
|
---|
| 97 | }
|
---|
| 98 | catch {
|
---|
| 99 | // If the native variant is not installed then use the WASM-based variant
|
---|
| 100 | await this.useWasm();
|
---|
| 101 | }
|
---|
| 102 | this.initialized = true;
|
---|
| 103 | }
|
---|
| 104 | /**
|
---|
| 105 | * Transitions an executor instance to use the WASM-variant of esbuild.
|
---|
| 106 | */
|
---|
| 107 | async useWasm() {
|
---|
| 108 | const { transform, formatMessages } = await Promise.resolve().then(() => __importStar(require('esbuild-wasm')));
|
---|
| 109 | this.esbuildTransform = transform;
|
---|
| 110 | this.esbuildFormatMessages = formatMessages;
|
---|
| 111 | // The ESBUILD_BINARY_PATH environment variable cannot exist when attempting to use the
|
---|
| 112 | // WASM variant. If it is then the binary located at the specified path will be used instead
|
---|
| 113 | // of the WASM variant.
|
---|
| 114 | delete process.env.ESBUILD_BINARY_PATH;
|
---|
| 115 | this.alwaysUseWasm = true;
|
---|
| 116 | }
|
---|
| 117 | async transform(input, options) {
|
---|
| 118 | await this.ensureEsbuild();
|
---|
| 119 | return this.esbuildTransform(input, options);
|
---|
| 120 | }
|
---|
| 121 | async formatMessages(messages, options) {
|
---|
| 122 | await this.ensureEsbuild();
|
---|
| 123 | return this.esbuildFormatMessages(messages, options);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | exports.EsbuildExecutor = EsbuildExecutor;
|
---|