vd-plugins/build.mjs

152 lines
4.7 KiB
JavaScript
Raw Normal View History

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";
2023-04-05 16:27:53 +08:00
import { readFile, readdir, writeFile } from "fs/promises";
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");
2023-04-05 16:27:53 +08:00
const toBuild = inputPlugins.length ? inputPlugins : await readdir("./plugins");
2023-02-13 20:42:39 +08:00
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) {
2023-04-05 16:27:53 +08:00
if (plugin.endsWith(".ts")) return;
const manifest = Object.assign(
JSON.parse(await readFile("./base_manifest.json")),
JSON.parse(await readFile(`./plugins/${plugin}/manifest.json`))
);
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 = {
"react": "window.React",
2023-04-05 16:35:45 +08:00
"react-native": "vendetta.metro.common.ReactNative"
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",
2023-07-09 08:17:28 +08:00
inlineDynamicImports: true,
2023-02-13 20:42:39 +08:00
},
onwarn: (warning) => {
![
"UNRESOLVED_IMPORT",
"MISSING_NAME_OPTION_FOR_IIFE_EXPORT",
"CIRCULAR_DEPENDENCY"
].includes(warning.code) && console.warn(warning);
},
plugins: [
nodeResolve({
resolveOnly: (id) => !["react", "react-native"].includes(id)
}),
2023-02-13 20:42:39 +08:00
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
};
2023-04-05 16:35:45 +08:00
const applyHash = async () => Object.assign(manifest, {
2023-04-05 16:27:53 +08:00
hash: createHash("sha256").update(await readFile(outPath)).digest("hex"),
main: entry
});
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
2023-04-05 16:35:45 +08:00
await applyHash();
2023-04-05 16:27:53 +08:00
await writeFile(`./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) => {
2023-04-05 16:27:53 +08:00
watcher.on("event", async (event) => {
2023-02-13 20:42:39 +08:00
switch (event.code) {
case "BUNDLE_END": {
2023-02-13 22:22:21 +08:00
event.result.close();
2023-04-05 16:35:45 +08:00
await applyHash();
2023-04-05 16:27:53 +08:00
await writeFile(`./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":
case "FATAL":
console.error(`${plugin}: ` + "\x1b[31m", "Failed! :(", "\x1b[0m");
2023-02-15 15:04:37 +08:00
reject(event.error);
2023-04-05 16:27:53 +08:00
break;
2023-02-13 20:42:39 +08:00
}
2023-01-24 15:56:43 +00:00
});
2023-02-13 20:42:39 +08:00
});
2023-02-11 23:09:54 +08:00
}