vd-plugins/build.mjs

96 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-02-13 00:18:39 +08:00
/* eslint-disable indent */
2023-02-11 23:09:54 +08:00
import commonjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
2023-02-13 00:48:48 +08:00
import { createHash } from "crypto";
import { readdirSync, readFileSync, writeFileSync } from "fs";
2023-02-13 00:18:39 +08:00
import { argv } from "process";
import { watch } from "rollup";
2023-01-24 15:56:43 +00:00
import esbuild from "rollup-plugin-esbuild";
2023-02-13 00:18:39 +08:00
const args = argv.slice(2);
const plugin = args.find(x => !x.startsWith("-"));
const flags = args.map(x => x.toLowerCase()).filter(x => x.startsWith("-"));
const isWatching = flags.includes("--watch") || flags.includes("-w");
console.clear();
for (const plug of plugin ? [plugin] : readdirSync("./plugins")) {
const manifest = JSON.parse(readFileSync(`./plugins/${plug}/manifest.json`).toString());
2023-02-11 23:09:54 +08:00
const entry = "index.js";
const outPath = `./dist/${plug}/${entry}`;
2023-01-24 15:56:43 +00:00
try {
2023-02-13 00:18:39 +08:00
const config = {
2023-01-24 15:56:43 +00:00
input: `./plugins/${plug}/${manifest.main}`,
2023-02-13 00:18:39 +08:00
output: {
file: outPath,
globals(id) {
if (id.startsWith("@vendetta")) return id.substring(1).replace(/\//g, ".");
const map = {
react: "window.React",
};
return map[id] || null;
},
format: "iife",
compact: true,
exports: "named",
},
2023-02-12 22:48:12 +08:00
onwarn: (warning) => {
![
"UNRESOLVED_IMPORT",
"MISSING_NAME_OPTION_FOR_IIFE_EXPORT",
"CIRCULAR_DEPENDENCY"
].includes(warning.code) && console.warn(warning);
},
2023-01-24 15:56:43 +00:00
plugins: [
nodeResolve(),
commonjs(),
esbuild({
target: "esnext",
minify: true,
})
2023-02-13 00:18:39 +08:00
]
};
2023-01-24 15:56:43 +00:00
2023-02-13 00:18:39 +08:00
const watchPlugin = new Promise((resolve, reject) => {
const watcher = watch(config);
watcher.on("event", (event) => {
switch (event.code) {
case "START":
process.stdout.write(`Building ${plug}... `);
break;
2023-02-13 00:48:48 +08:00
case "BUNDLE_END": {
2023-02-13 00:18:39 +08:00
console.log("\x1b[32m", `Succeed! (${event.duration}ms)`, "\x1b[0m");
2023-02-13 00:48:48 +08:00
const toHash = readFileSync(outPath);
manifest.hash = createHash("sha256").update(toHash).digest("hex");
manifest.main = "index.js";
writeFileSync(`./dist/${plug}/manifest.json`, JSON.stringify(manifest));
2023-02-13 00:18:39 +08:00
resolve();
break;
2023-02-13 00:48:48 +08:00
}
2023-02-13 00:18:39 +08:00
case "ERROR":
console.error("\x1b[31m", "Failed! :(", "\x1b[0m");
console.error(event.error);
reject(event.error);
break;
case "FATAL":
reject(event.error);
process.exit(1);
}
});
2023-01-24 15:56:43 +00:00
});
2023-02-11 23:09:54 +08:00
2023-02-13 00:18:39 +08:00
await watchPlugin.catch(console.error);
2023-01-24 15:56:43 +00:00
} catch (e) {
2023-02-13 00:18:39 +08:00
console.error(`Failed to build ${plug}...`, e);
2023-01-24 15:56:43 +00:00
process.exit(1);
}
2023-02-11 23:09:54 +08:00
}
2023-02-13 00:18:39 +08:00
if (!isWatching) process.exit(0);
else console.log("\nWatching for changes...");