Main: add watch
This commit is contained in:
parent
4ab72b8dd9
commit
fa69e9e3a9
2 changed files with 60 additions and 30 deletions
|
|
@ -36,6 +36,7 @@
|
||||||
"@typescript-eslint/no-explicit-any": "off",
|
"@typescript-eslint/no-explicit-any": "off",
|
||||||
"@typescript-eslint/ban-ts-comment": "off",
|
"@typescript-eslint/ban-ts-comment": "off",
|
||||||
"@typescript-eslint/triple-slash-reference": "off",
|
"@typescript-eslint/triple-slash-reference": "off",
|
||||||
|
"@typescript-eslint/no-non-null-assertion": "off",
|
||||||
"eol-last": "error"
|
"eol-last": "error"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
89
build.mjs
89
build.mjs
|
|
@ -1,18 +1,42 @@
|
||||||
|
/* eslint-disable indent */
|
||||||
import commonjs from "@rollup/plugin-commonjs";
|
import commonjs from "@rollup/plugin-commonjs";
|
||||||
import nodeResolve from "@rollup/plugin-node-resolve";
|
import nodeResolve from "@rollup/plugin-node-resolve";
|
||||||
import { createHash } from "crypto";
|
import { readdirSync, readFileSync } from "fs";
|
||||||
import { readdir, readFile, writeFile } from "fs/promises";
|
import { argv } from "process";
|
||||||
import { rollup } from "rollup";
|
import { watch } from "rollup";
|
||||||
import esbuild from "rollup-plugin-esbuild";
|
import esbuild from "rollup-plugin-esbuild";
|
||||||
|
|
||||||
for (const plug of await readdir("./plugins")) {
|
const args = argv.slice(2);
|
||||||
const manifest = JSON.parse(await readFile(`./plugins/${plug}/manifest.json`));
|
|
||||||
|
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());
|
||||||
const entry = "index.js";
|
const entry = "index.js";
|
||||||
const outPath = `./dist/${plug}/${entry}`;
|
const outPath = `./dist/${plug}/${entry}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const bundle = await rollup({
|
const config = {
|
||||||
input: `./plugins/${plug}/${manifest.main}`,
|
input: `./plugins/${plug}/${manifest.main}`,
|
||||||
|
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",
|
||||||
|
},
|
||||||
onwarn: (warning) => {
|
onwarn: (warning) => {
|
||||||
![
|
![
|
||||||
"UNRESOLVED_IMPORT",
|
"UNRESOLVED_IMPORT",
|
||||||
|
|
@ -27,33 +51,38 @@ for (const plug of await readdir("./plugins")) {
|
||||||
target: "esnext",
|
target: "esnext",
|
||||||
minify: true,
|
minify: true,
|
||||||
})
|
})
|
||||||
],
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
case "BUNDLE_END":
|
||||||
|
console.log("\x1b[32m", `Succeed! (${event.duration}ms)`, "\x1b[0m");
|
||||||
|
resolve();
|
||||||
|
break;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await bundle.write({
|
await watchPlugin.catch(console.error);
|
||||||
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();
|
|
||||||
|
|
||||||
const toHash = await readFile(outPath);
|
|
||||||
manifest.hash = createHash("sha256").update(toHash).digest("hex");
|
|
||||||
manifest.main = entry;
|
|
||||||
await writeFile(`./dist/${plug}/manifest.json`, JSON.stringify(manifest));
|
|
||||||
|
|
||||||
console.log(`Successfully built ${manifest.name}!`);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Failed to build plugin...", e);
|
console.error(`Failed to build ${plug}...`, e);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isWatching) process.exit(0);
|
||||||
|
else console.log("\nWatching for changes...");
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue