UNPKG

esbuild-gas-plugin

Version:
45 lines (44 loc) 1.71 kB
import * as dntShim from "./_dnt.shims.js"; import { StringReader, readLines } from "./deps/deno.land/std@0.190.0/io/mod.js"; import { generate } from "gas-entry-generator"; async function countLines(s) { const reader = new StringReader(s); let count = 0; for await (const _ of readLines(reader, { encoding: 'utf8' })) { count++; } return count; } async function deleteBanner(code, banner) { const bannerCount = await countLines(banner); const reader = new StringReader(code); let _count = 0; let _code = ''; for await (const line of readLines(reader, { encoding: 'utf8' })) { _count++; if (_count <= bannerCount) continue; _code += `${line}\n`; } return _code; } export const GasPlugin = { name: "gas-plugin", setup({ onEnd, initialOptions }) { onEnd(async () => { if (initialOptions.outfile === undefined) { throw Error('"outfile" is required. Note that "write: false" is not available.'); } const jsBanner = initialOptions.banner?.js; const code = await dntShim.Deno.readTextFile(initialOptions.outfile); const gas = generate(code, { comment: true }); if (jsBanner === undefined) { await dntShim.Deno.writeTextFile(initialOptions.outfile, `var global = this;\n${gas.entryPointFunctions}\n${code}`); } else { const bannerDeleted = await deleteBanner(code, jsBanner); await dntShim.Deno.writeTextFile(initialOptions.outfile, `${jsBanner}\nvar global = this;\n${gas.entryPointFunctions}${bannerDeleted}`); } }); }, };