@synaptic-simulations/mach
Version:
The last MSFS instrument bundler you'll ever need.
116 lines • 5 kB
JavaScript
;
/*
* SPDX-FileCopyrightText: 2022 Synaptic Simulations and its contributors
* SPDX-License-Identifier: MIT
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildInstrument = buildInstrument;
exports.watchInstrument = watchInstrument;
const node_path_1 = __importDefault(require("node:path"));
const chokidar_1 = __importDefault(require("chokidar"));
const esbuild_1 = __importDefault(require("esbuild"));
const plugins_1 = require("./plugins");
const types_1 = require("./types");
function getBuildOptions(args, instrument) {
var _a, _b, _c, _d, _e, _f;
const bundlesDir = (_a = args.bundles) !== null && _a !== void 0 ? _a : "./bundles";
const options = {
absWorkingDir: process.cwd(),
target: "es2017",
logLevel: "silent",
logOverride: args.werror ? types_1.ESBUILD_ERRORS : undefined,
sourcemap: args.sourcemaps,
minify: args.minify,
...args.config.esbuild,
entryPoints: [instrument.index],
outfile: node_path_1.default.join(bundlesDir, instrument.name, "bundle.js"),
format: "iife",
metafile: true,
bundle: true,
loader: { ".otf": "file", ".ttf": "file", ...(_b = args.config.esbuild) === null || _b === void 0 ? void 0 : _b.loader },
external: ["/Images/*", "/Fonts/*", ...((_d = (_c = args.config.esbuild) === null || _c === void 0 ? void 0 : _c.external) !== null && _d !== void 0 ? _d : [])],
plugins: [plugins_1.environment, ...((_f = (_e = args.config.esbuild) === null || _e === void 0 ? void 0 : _e.plugins) !== null && _f !== void 0 ? _f : [])],
};
if (args.outputMetafile) {
options.plugins.push(plugins_1.writeMetafile);
}
if (instrument.modules) {
options.alias = Object.fromEntries(instrument.modules.map(({ resolve, index }) => [resolve, node_path_1.default.resolve(index)]));
}
if (instrument.simulatorPackage && !args.skipSimulatorPackage) {
options.plugins.push((0, plugins_1.writePackageSources)(args, instrument));
}
return options;
}
async function buildInstrument(args, instrument, logger) {
const options = getBuildOptions(args, instrument);
const startTime = performance.now();
return await esbuild_1.default
.build(options)
.then((result) => {
logger.buildComplete(instrument.name, performance.now() - startTime, result);
return result;
})
.catch((failure) => {
logger.buildFailed(failure.errors);
throw failure;
});
}
function resolveFilename(input) {
const cwdIndex = input.indexOf(process.cwd());
return node_path_1.default.resolve(cwdIndex >= 0 ? input.slice(cwdIndex) : input);
}
async function watchInstrument(args, instrument, logger) {
const options = getBuildOptions(args, instrument);
const context = await esbuild_1.default.context(options);
const startTime = performance.now();
const result = await context
.rebuild()
.then((result) => {
logger.buildComplete(instrument.name, performance.now() - startTime, result);
return result;
})
.catch((failure) => {
console.error(failure);
logger.buildFailed(failure.errors);
throw failure;
});
const builtFiles = Object.keys(result.metafile.inputs).map(resolveFilename);
const watcher = chokidar_1.default.watch(builtFiles);
watcher.on("change", async (filePath) => {
logger.changeDetected(filePath);
const startTime = performance.now();
await context
.rebuild()
.then((result) => {
var _a;
logger.buildComplete(instrument.name, performance.now() - startTime, result);
const watchedFiles = watcher.getWatched();
const bundledFiles = Object.keys(result.metafile.inputs).map(resolveFilename);
// Watch files that have been added to the bundle
for (const file of bundledFiles) {
if (!((_a = watchedFiles[node_path_1.default.dirname(file)]) === null || _a === void 0 ? void 0 : _a.includes(node_path_1.default.basename(file)))) {
watcher.add(file);
}
}
// Unwatch files that are no longer included in the bundle
for (const [dir, files] of Object.entries(watchedFiles)) {
for (const file of files) {
const filePath = node_path_1.default.join(dir, file);
if (!bundledFiles.includes(filePath)) {
watcher.unwatch(filePath);
}
}
}
})
.catch((failure) => {
console.error(failure);
logger.buildFailed(failure.errors);
});
});
return result;
}
//# sourceMappingURL=esbuild.js.map