2023-02-11 23:09:54 +08:00
|
|
|
import commonjs from "@rollup/plugin-commonjs";
|
|
|
|
|
import nodeResolve from "@rollup/plugin-node-resolve";
|
2023-01-24 15:56:43 +00:00
|
|
|
import { createHash } from "crypto";
|
2023-02-11 23:09:54 +08:00
|
|
|
import { readdir, readFile, writeFile } from "fs/promises";
|
|
|
|
|
import { rollup } from "rollup";
|
2023-01-24 15:56:43 +00:00
|
|
|
import esbuild from "rollup-plugin-esbuild";
|
|
|
|
|
|
2023-02-11 23:09:54 +08:00
|
|
|
for (const plug of await readdir("./plugins")) {
|
2023-01-24 15:56:43 +00:00
|
|
|
const manifest = JSON.parse(await readFile(`./plugins/${plug}/manifest.json`));
|
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 {
|
|
|
|
|
const bundle = await rollup({
|
|
|
|
|
input: `./plugins/${plug}/${manifest.main}`,
|
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-11 23:09:54 +08:00
|
|
|
|
2023-01-24 15:56:43 +00:00
|
|
|
await bundle.write({
|
|
|
|
|
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",
|
|
|
|
|
});
|
|
|
|
|
await bundle.close();
|
2023-02-11 23:09:54 +08:00
|
|
|
|
2023-01-24 15:56:43 +00:00
|
|
|
const toHash = await readFile(outPath);
|
|
|
|
|
manifest.hash = createHash("sha256").update(toHash).digest("hex");
|
2023-02-11 23:09:54 +08:00
|
|
|
manifest.main = entry;
|
2023-01-24 15:56:43 +00:00
|
|
|
await writeFile(`./dist/${plug}/manifest.json`, JSON.stringify(manifest));
|
2023-02-11 23:09:54 +08:00
|
|
|
|
2023-01-24 15:56:43 +00:00
|
|
|
console.log(`Successfully built ${manifest.name}!`);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Failed to build plugin...", e);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2023-02-11 23:09:54 +08:00
|
|
|
}
|