UNPKG

@plugjs/plug

Version:
116 lines (115 loc) 4.85 kB
// plugs/esbuild.ts import { basename } from "node:path"; import { build } from "esbuild"; import { assert } from "../asserts.mjs"; import { Files } from "../files.mjs"; import { readFile } from "../fs.mjs"; import { $p, ERROR, WARN } from "../logging.mjs"; import { getAbsoluteParent, resolveAbsolutePath } from "../paths.mjs"; import { install } from "../pipe.mjs"; export * from "./esbuild/bundle-locals.mjs"; export * from "./esbuild/fix-extensions.mjs"; 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); } assert(!(options.outdir && options.outfile), 'Options "outfile" and "outdir" can not coexist'); let builder; if (options.bundle && options.outfile && entryPoints.length === 1) { builder = Files.builder(absWorkingDir); const outputFile = resolveAbsolutePath(absWorkingDir, options.outfile); const entryPoint = resolveAbsolutePath(absWorkingDir, entryPoints[0]); options.outfile = outputFile; context.log.debug("Bundling", $p(entryPoint), "into", $p(outputFile)); } else { assert(options.outdir, 'Option "outdir" must be specified'); builder = Files.builder(context.resolve(options.outdir)); options.outdir = builder.directory; const message = options.bundle ? "Bundling" : "Transpiling"; context.log.debug(message, entryPoints.length, "files to", $p(builder.directory)); } const report = context.log.report("ESBuild Report"); context.log.trace("Running ESBuild", options); let esbuild; try { esbuild = await build({ ...options, metafile: true }); context.log.trace("ESBuild Results", esbuild); report.add(...esbuild.warnings.map((m) => convertMessage(WARN, m, absWorkingDir))); report.add(...esbuild.errors.map((m) => convertMessage(ERROR, m, absWorkingDir))); } catch (error) { const e = error; if (e.warnings) report.add(...e.warnings.map((m) => convertMessage(WARN, m, absWorkingDir))); if (e.errors) report.add(...e.errors.map((m) => convertMessage(ERROR, m, absWorkingDir))); } await report.loadSources(); report.done(); assert(esbuild, "ESBuild did not produce any result"); for (const file in esbuild.metafile.outputs) { builder.add(resolveAbsolutePath(absWorkingDir, file)); } const result = builder.build(); context.log.info("ESBuild produced", result.length, "files into", $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 = 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 = resolveAbsolutePath(directory, "package.json"); try { const json = await 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 = basename(directory); const parent = 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; } } //# sourceMappingURL=esbuild.mjs.map