vd-plugins/build.mjs

145 lines
4.6 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";
2023-02-13 22:22:21 +08:00
import { rollup, watch } from "rollup";
import swc from "@swc/core";
import esbuild from "rollup-plugin-esbuild";
2023-01-24 15:56:43 +00:00
2023-02-13 00:18:39 +08:00
const args = argv.slice(2);
2023-02-13 22:22:21 +08:00
console.clear();
2023-02-13 00:18:39 +08:00
2023-02-13 20:42:39 +08:00
const inputPlugins = args.filter(x => !x.startsWith("-"));
2023-02-13 00:18:39 +08:00
const flags = args.map(x => x.toLowerCase()).filter(x => x.startsWith("-"));
2023-02-13 20:42:39 +08:00
const isWatch = flags.includes("--watch") || flags.includes("-w");
const toBuild = inputPlugins.length ? inputPlugins : readdirSync("./plugins");
2023-02-13 22:22:21 +08:00
console.log(`Building ${toBuild.length} plugin(s)...`);
2023-02-13 20:42:39 +08:00
let failed = 0;
2023-02-13 22:22:21 +08:00
for (const plugin of toBuild) {
try {
await buildPlugin(plugin);
} catch (e) {
2023-02-15 15:04:37 +08:00
console.error(e.stack || e);
2023-02-13 22:22:21 +08:00
failed++;
}
}
2023-02-13 20:42:39 +08:00
2023-02-13 22:22:21 +08:00
console.log("\n" + (
2023-02-13 21:48:12 +08:00
failed
2023-02-15 15:04:37 +08:00
? "\x1b[31m" + `Failed to build ${failed} plugin(s)` + "\x1b[0m"
2023-02-13 22:22:21 +08:00
: "\x1b[32m" + "All plugin(s) built successfully!" + "\x1b[0m"
2023-02-13 21:48:12 +08:00
));
2023-02-13 20:42:39 +08:00
2023-02-13 22:22:21 +08:00
isWatch && console.log("\nWatching for changes...");
2023-02-13 20:42:39 +08:00
async function buildPlugin(plugin) {
const manifest = JSON.parse(readFileSync(`./plugins/${plugin}/manifest.json`).toString());
2023-02-11 23:09:54 +08:00
const entry = "index.js";
2023-02-13 20:42:39 +08:00
const outPath = `./dist/${plugin}/${entry}`;
2023-01-24 15:56:43 +00:00
2023-02-15 15:04:37 +08:00
/** @type {import("rollup").RollupOptions} */
2023-02-13 22:22:21 +08:00
const options = {
2023-02-13 20:42:39 +08:00
input: `./plugins/${plugin}/${manifest.main}`,
output: {
file: outPath,
globals(id) {
if (id.startsWith("@vendetta"))
return id.substring(1).replace(/\//g, ".");
2023-02-13 20:42:39 +08:00
const map = {
2023-02-15 15:04:37 +08:00
react: "window.React"
2023-02-13 20:42:39 +08:00
};
2023-02-13 00:18:39 +08:00
2023-02-13 20:42:39 +08:00
return map[id] || null;
2023-02-12 22:48:12 +08:00
},
2023-02-13 20:42:39 +08:00
format: "iife",
compact: true,
exports: "named",
},
onwarn: (warning) => {
![
"UNRESOLVED_IMPORT",
"MISSING_NAME_OPTION_FOR_IIFE_EXPORT",
"CIRCULAR_DEPENDENCY"
].includes(warning.code) && console.warn(warning);
},
plugins: [
nodeResolve(),
commonjs(),
{
name: "swc",
transform(code, id) {
return swc.transform(code, {
filename: id,
jsc: {
parser: {
syntax: "typescript",
tsx: true,
},
externalHelpers: true,
},
env: {
targets: "defaults",
include: [
"transform-classes",
"transform-arrow-functions",
],
},
});
2023-02-15 15:04:37 +08:00
},
},
esbuild({ minify: true }),
2023-02-13 20:42:39 +08:00
]
2023-02-13 22:22:21 +08:00
};
if (!isWatch) {
2023-02-15 15:04:37 +08:00
const bundle = await rollup(options);
await bundle.write(options.output);
await bundle.close();
2023-02-13 22:22:21 +08:00
const toHash = readFileSync(outPath);
manifest.hash = createHash("sha256").update(toHash).digest("hex");
manifest.main = "index.js";
writeFileSync(`./dist/${plugin}/manifest.json`, JSON.stringify(manifest));
2023-02-15 15:04:37 +08:00
console.log(`${plugin}: ` + "\x1b[32m" + "Build succeed!" + "\x1b[0m");
return;
2023-02-13 22:22:21 +08:00
}
const watcher = watch(options);
2023-02-13 00:48:48 +08:00
2023-02-13 20:42:39 +08:00
return await new Promise((resolve, reject) => {
watcher.on("event", (event) => {
switch (event.code) {
case "BUNDLE_END": {
2023-02-13 22:22:21 +08:00
event.result.close();
2023-02-13 20:42:39 +08:00
const toHash = readFileSync(outPath);
manifest.hash = createHash("sha256").update(toHash).digest("hex");
manifest.main = "index.js";
writeFileSync(`./dist/${plugin}/manifest.json`, JSON.stringify(manifest));
2023-02-13 09:05:36 +08:00
2023-02-13 22:22:21 +08:00
console.log(`${plugin}: ` + "\x1b[32m" + `Build succeed! (${event.duration}ms)` + "\x1b[0m");
2023-02-13 20:42:39 +08:00
resolve();
break;
2023-02-13 00:18:39 +08:00
}
2023-02-13 20:42:39 +08:00
case "ERROR":
console.error(`${plugin}: ` + "\x1b[31m", "Failed! :(", "\x1b[0m");
2023-02-15 15:04:37 +08:00
reject(event.error);
2023-02-13 20:42:39 +08:00
break;
case "FATAL":
console.error(`${plugin}: ` + "\x1b[31m", "Failed! :(", "\x1b[0m");
2023-02-15 15:04:37 +08:00
reject(event.error);
2023-02-13 20:42:39 +08:00
process.exit(1);
}
2023-01-24 15:56:43 +00:00
});
2023-02-13 20:42:39 +08:00
});
2023-02-11 23:09:54 +08:00
}