UNPKG

@build-script/heft-esbuild-plugin

Version:

lowlevel esbuild plugin for [heft]

57 lines (56 loc) 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createEmitter = createEmitter; const promises_1 = require("node:fs/promises"); const node_path_1 = require("node:path"); const write_helper_js_1 = require("./write.helper.js"); function createEmitter(onEmit) { const cache = { custom: undefined, writeFile: new Map(), }; return async ({ metafile, outputFiles, options, session }) => { const files = []; let writtenFiles = 0; Object.assign(metafile, { root: options.outdir }); const metaOut = (0, node_path_1.resolve)(options.outdir, 'metafile.json'); files.push(new write_helper_js_1.OutputFile(metaOut, JSON.stringify(metafile, null, 4))); for (const file of outputFiles) { files.push(new write_helper_js_1.OutputFile(file.path, file.contents, file.hash)); } if (onEmit) { cache.custom = await onEmit(files, options, cache.custom); } const lastCache = cache.writeFile; cache.writeFile = new Map(); for (const item of files) { if (!item.path || !item.contents) { throw new Error(`invalid file: ${JSON.stringify(item)}`); } const content = item.contents || Buffer.from(item.text, 'utf-8'); cache.writeFile.set(item.path, item.hash); if (lastCache.get(item.path) === cache.writeFile.get(item.path)) { session.logger.terminal.writeDebugLine('write file: ', item.path, ' - unchange'); continue; } try { await (0, promises_1.mkdir)((0, node_path_1.dirname)(item.path), { recursive: true }); await (0, promises_1.writeFile)(item.path, content); writtenFiles++; session.logger.terminal.writeLine('write file: ', item.path, ' - ok'); } catch (e) { session.logger.terminal.writeWarningLine('write file: ', item.path, ' - failed'); throw e; } } for (const file of lastCache.keys()) { if (!cache.writeFile.has(file)) { session.logger.terminal.writeLine('write file: ', file, ' - deleted'); await (0, promises_1.rm)(file); } } return { writtenFiles }; }; } //# sourceMappingURL=write.js.map