2022-03-04 09:26:41 +00:00
|
|
|
import esbuild from "esbuild";
|
|
|
|
import { readFile, unlink, writeFile } from "fs/promises";
|
|
|
|
import path from "path";
|
|
|
|
|
|
|
|
import yargs from "yargs";
|
|
|
|
import { hideBin } from "yargs/helpers";
|
|
|
|
import { Manifest } from "../../webapp/src/plugins/types";
|
|
|
|
|
2022-03-04 10:21:11 +00:00
|
|
|
async function compile(filePath: string, sourceMap: boolean) {
|
2022-03-04 09:26:41 +00:00
|
|
|
let tempFile = "out.js";
|
|
|
|
let js = await esbuild.build({
|
|
|
|
entryPoints: [filePath],
|
|
|
|
bundle: true,
|
|
|
|
format: "iife",
|
|
|
|
globalName: "mod",
|
|
|
|
platform: "neutral",
|
|
|
|
sourcemap: sourceMap ? "inline" : false,
|
|
|
|
minify: true,
|
|
|
|
outfile: tempFile,
|
|
|
|
});
|
|
|
|
|
|
|
|
let jsCode = (await readFile(tempFile)).toString();
|
|
|
|
jsCode = jsCode.replace(/^var mod ?= ?/, "");
|
|
|
|
await unlink(tempFile);
|
|
|
|
return jsCode;
|
|
|
|
}
|
|
|
|
|
2022-03-04 10:21:11 +00:00
|
|
|
async function bundle(manifestPath: string, sourceMaps: boolean) {
|
2022-03-04 09:26:41 +00:00
|
|
|
const rootPath = path.dirname(manifestPath);
|
|
|
|
const manifest = JSON.parse(
|
|
|
|
(await readFile(manifestPath)).toString()
|
|
|
|
) as Manifest;
|
|
|
|
|
|
|
|
for (let [name, def] of Object.entries(manifest.functions)) {
|
|
|
|
let jsFunctionName = def.functionName,
|
|
|
|
filePath = path.join(rootPath, def.path);
|
|
|
|
if (filePath.indexOf(":") !== -1) {
|
|
|
|
[filePath, jsFunctionName] = filePath.split(":");
|
|
|
|
} else if (!jsFunctionName) {
|
|
|
|
jsFunctionName = "default";
|
|
|
|
}
|
|
|
|
|
|
|
|
def.code = await compile(filePath, sourceMaps);
|
|
|
|
def.path = filePath;
|
|
|
|
def.functionName = jsFunctionName;
|
|
|
|
}
|
|
|
|
return manifest;
|
|
|
|
}
|
|
|
|
async function run() {
|
|
|
|
let args = await yargs(hideBin(process.argv))
|
|
|
|
.option("debug", {
|
|
|
|
type: "boolean",
|
|
|
|
})
|
|
|
|
.parse();
|
|
|
|
|
2022-03-04 10:21:11 +00:00
|
|
|
let generatedManifest = await bundle(args._[0] as string, !!args.debug);
|
2022-03-04 09:26:41 +00:00
|
|
|
writeFile(args._[1] as string, JSON.stringify(generatedManifest, null, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
run().catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
process.exit(1);
|
|
|
|
});
|