esbuild-gas-plugin
Version:
esbuild plugin for Google Apps Script.
71 lines (70 loc) • 2.92 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GasPlugin = void 0;
const dntShim = __importStar(require("./_dnt.shims.js"));
const mod_js_1 = require("./deps/deno.land/std@0.190.0/io/mod.js");
const gas_entry_generator_1 = require("gas-entry-generator");
async function countLines(s) {
const reader = new mod_js_1.StringReader(s);
let count = 0;
for await (const _ of (0, mod_js_1.readLines)(reader, { encoding: 'utf8' })) {
count++;
}
return count;
}
async function deleteBanner(code, banner) {
const bannerCount = await countLines(banner);
const reader = new mod_js_1.StringReader(code);
let _count = 0;
let _code = '';
for await (const line of (0, mod_js_1.readLines)(reader, { encoding: 'utf8' })) {
_count++;
if (_count <= bannerCount)
continue;
_code += `${line}\n`;
}
return _code;
}
exports.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 = (0, gas_entry_generator_1.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}`);
}
});
},
};