@plugjs/plug
Version:
PlugJS Build System ===================
139 lines (137 loc) • 6.42 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// plugs/esbuild.ts
var esbuild_exports = {};
module.exports = __toCommonJS(esbuild_exports);
var import_node_path = require("node:path");
var import_esbuild = require("esbuild");
var import_asserts = require("../asserts.cjs");
var import_files = require("../files.cjs");
var import_fs = require("../fs.cjs");
var import_logging = require("../logging.cjs");
var import_paths = require("../paths.cjs");
var import_pipe = require("../pipe.cjs");
__reExport(esbuild_exports, require("./esbuild/bundle-locals.cjs"), module.exports);
__reExport(esbuild_exports, require("./esbuild/fix-extensions.cjs"), module.exports);
(0, import_pipe.install)("esbuild", class ESBuild {
constructor(_options) {
this._options = _options;
}
async pipe(files, context) {
const entryPoints = [...files];
const absWorkingDir = files.directory;
const options = {
/* Default our platform/target to NodeJS, current major version */
platform: "node",
target: `node${process.versions["node"].split(".")[0]}`,
/* The default format (if not specified in options) is from package.json */
format: this._options.format || await _moduleFormat(files.directory, context.log),
/* Output bese directory */
outbase: absWorkingDir,
/* Merge in the caller's options */
...this._options,
/* Always override */
absWorkingDir,
entryPoints,
logLevel: "silent"
};
if (options.format === "cjs") {
options.define = Object.assign({ __fileurl: "__filename" }, options.define);
} else if (options.format === "esm") {
options.define = Object.assign({ __fileurl: "import.meta.url" }, options.define);
}
(0, import_asserts.assert)(!(options.outdir && options.outfile), 'Options "outfile" and "outdir" can not coexist');
let builder;
if (options.bundle && options.outfile && entryPoints.length === 1) {
builder = import_files.Files.builder(absWorkingDir);
const outputFile = (0, import_paths.resolveAbsolutePath)(absWorkingDir, options.outfile);
const entryPoint = (0, import_paths.resolveAbsolutePath)(absWorkingDir, entryPoints[0]);
options.outfile = outputFile;
context.log.debug("Bundling", (0, import_logging.$p)(entryPoint), "into", (0, import_logging.$p)(outputFile));
} else {
(0, import_asserts.assert)(options.outdir, 'Option "outdir" must be specified');
builder = import_files.Files.builder(context.resolve(options.outdir));
options.outdir = builder.directory;
const message = options.bundle ? "Bundling" : "Transpiling";
context.log.debug(message, entryPoints.length, "files to", (0, import_logging.$p)(builder.directory));
}
const report = context.log.report("ESBuild Report");
context.log.trace("Running ESBuild", options);
let esbuild;
try {
esbuild = await (0, import_esbuild.build)({ ...options, metafile: true });
context.log.trace("ESBuild Results", esbuild);
report.add(...esbuild.warnings.map((m) => convertMessage(import_logging.WARN, m, absWorkingDir)));
report.add(...esbuild.errors.map((m) => convertMessage(import_logging.ERROR, m, absWorkingDir)));
} catch (error) {
const e = error;
if (e.warnings) report.add(...e.warnings.map((m) => convertMessage(import_logging.WARN, m, absWorkingDir)));
if (e.errors) report.add(...e.errors.map((m) => convertMessage(import_logging.ERROR, m, absWorkingDir)));
}
await report.loadSources();
report.done();
(0, import_asserts.assert)(esbuild, "ESBuild did not produce any result");
for (const file in esbuild.metafile.outputs) {
builder.add((0, import_paths.resolveAbsolutePath)(absWorkingDir, file));
}
const result = builder.build();
context.log.info("ESBuild produced", result.length, "files into", (0, import_logging.$p)(result.directory));
return result;
}
});
function convertMessage(level, message, directory) {
const record = { level, message: message.text };
record.tags = [message.id, message.pluginName].filter((tag) => !!tag);
if (message.location) {
record.line = message.location.line;
record.column = message.location.column + 1;
record.length = message.location.length;
record.file = (0, import_paths.resolveAbsolutePath)(directory, message.location.file);
}
return record;
}
var _moduleFormatCache = /* @__PURE__ */ new Map();
async function _moduleFormat(directory, log) {
const type = _moduleFormatCache.get(directory);
if (type) return type;
const file = (0, import_paths.resolveAbsolutePath)(directory, "package.json");
try {
const json = await (0, import_fs.readFile)(file, "utf-8");
const data = JSON.parse(json);
const type2 = data.type === "module" ? "esm" : "cjs";
log.debug(`File "${file}" defines module type as "${data.type}" (${type2})`);
_moduleFormatCache.set(directory, type2);
return type2;
} catch (cause) {
if (cause.code !== "ENOENT" && cause.code !== "EISDIR") throw cause;
}
const name = (0, import_node_path.basename)(directory);
const parent = (0, import_paths.getAbsoluteParent)(directory);
if (name === "node_modules" || parent === directory) {
_moduleFormatCache.set(directory, "cjs");
return "cjs";
} else {
const type2 = await _moduleFormat(parent, log);
_moduleFormatCache.set(directory, type2);
return type2;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
...require("./esbuild/bundle-locals.cjs"),
...require("./esbuild/fix-extensions.cjs")
});
//# sourceMappingURL=esbuild.cjs.map