@angular-devkit/build-angular
Version:
Angular Webpack Build Facade
134 lines (133 loc) • 5.4 kB
JavaScript
/**
* @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.dev/license
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.EsbuildExecutor = void 0;
/**
* Provides the ability to execute esbuild regardless of the current platform's support
* for using the native variant of esbuild. The native variant will be preferred (assuming
* the `alwaysUseWasm` constructor option is `false) due to its inherent performance advantages.
* At first use of esbuild, a supportability test will be automatically performed and the
* WASM-variant will be used if needed by the platform.
*/
class EsbuildExecutor {
alwaysUseWasm;
esbuildTransform;
esbuildFormatMessages;
initialized = false;
/**
* Constructs an instance of the `EsbuildExecutor` class.
*
* @param alwaysUseWasm If true, the WASM-variant will be preferred and no support test will be
* performed; if false (default), the native variant will be preferred.
*/
constructor(alwaysUseWasm = false) {
this.alwaysUseWasm = alwaysUseWasm;
this.esbuildTransform = this.esbuildFormatMessages = () => {
throw new Error('esbuild implementation missing');
};
}
/**
* Determines whether the native variant of esbuild can be used on the current platform.
*
* @returns A promise which resolves to `true`, if the native variant of esbuild is support or `false`, if the WASM variant is required.
*/
static async hasNativeSupport() {
// Try to use native variant to ensure it is functional for the platform.
try {
const { formatMessages } = await Promise.resolve().then(() => __importStar(require('esbuild')));
await formatMessages([], { kind: 'error' });
return true;
}
catch {
return false;
}
}
/**
* Initializes the esbuild transform and format messages functions.
*
* @returns A promise that fulfills when esbuild has been loaded and available for use.
*/
async ensureEsbuild() {
if (this.initialized) {
return;
}
// If the WASM variant was preferred at class construction or native is not supported, use WASM
if (this.alwaysUseWasm || !(await EsbuildExecutor.hasNativeSupport())) {
await this.useWasm();
this.initialized = true;
return;
}
try {
// Use the faster native variant if available.
const { transform, formatMessages } = await Promise.resolve().then(() => __importStar(require('esbuild')));
this.esbuildTransform = transform;
this.esbuildFormatMessages = formatMessages;
}
catch {
// If the native variant is not installed then use the WASM-based variant
await this.useWasm();
}
this.initialized = true;
}
/**
* Transitions an executor instance to use the WASM-variant of esbuild.
*/
async useWasm() {
const { transform, formatMessages } = await Promise.resolve().then(() => __importStar(require('esbuild-wasm')));
this.esbuildTransform = transform;
this.esbuildFormatMessages = formatMessages;
// The ESBUILD_BINARY_PATH environment variable cannot exist when attempting to use the
// WASM variant. If it is then the binary located at the specified path will be used instead
// of the WASM variant.
delete process.env.ESBUILD_BINARY_PATH;
this.alwaysUseWasm = true;
}
async transform(input, options) {
await this.ensureEsbuild();
return this.esbuildTransform(input, options);
}
async formatMessages(messages, options) {
await this.ensureEsbuild();
return this.esbuildFormatMessages(messages, options);
}
}
exports.EsbuildExecutor = EsbuildExecutor;
;