2022-08-29 00:25:27 +00:00
|
|
|
#!/usr/bin/node
|
2022-08-29 23:42:47 +00:00
|
|
|
import { execSync } from "child_process";
|
2022-08-29 00:25:27 +00:00
|
|
|
import esbuild from "esbuild";
|
2022-08-29 16:11:44 +00:00
|
|
|
import { readdirSync } from "fs";
|
2022-08-29 00:25:27 +00:00
|
|
|
|
2022-08-29 16:11:44 +00:00
|
|
|
/**
|
2022-08-29 16:45:58 +00:00
|
|
|
* @type {esbuild.WatchMode|false}
|
2022-08-29 16:11:44 +00:00
|
|
|
*/
|
2022-09-16 20:40:22 +00:00
|
|
|
const watch = process.argv.includes("--watch");
|
2022-08-29 16:11:44 +00:00
|
|
|
|
|
|
|
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
2022-08-29 16:45:58 +00:00
|
|
|
/**
|
|
|
|
* @type {esbuild.Plugin}
|
|
|
|
*/
|
2022-08-29 16:11:44 +00:00
|
|
|
const makeAllPackagesExternalPlugin = {
|
2022-10-05 22:42:58 +00:00
|
|
|
name: "make-all-packages-external",
|
2022-08-29 16:11:44 +00:00
|
|
|
setup(build) {
|
|
|
|
let filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/; // Must not start with "/" or "./" or "../"
|
|
|
|
build.onResolve({ filter }, args => ({ path: args.path, external: true }));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-08-29 16:45:58 +00:00
|
|
|
/**
|
|
|
|
* @type {esbuild.Plugin}
|
|
|
|
*/
|
2022-08-29 16:11:44 +00:00
|
|
|
const globPlugins = {
|
|
|
|
name: "glob-plugins",
|
|
|
|
setup: build => {
|
|
|
|
build.onResolve({ filter: /^plugins$/ }, args => {
|
|
|
|
return {
|
|
|
|
namespace: "import-plugins",
|
|
|
|
path: args.path
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
build.onLoad({ filter: /^plugins$/, namespace: "import-plugins" }, () => {
|
|
|
|
const files = readdirSync("./src/plugins");
|
|
|
|
let code = "";
|
2022-08-31 21:04:18 +00:00
|
|
|
let obj = "";
|
2022-08-29 16:11:44 +00:00
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
if (files[i] === "index.ts") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const mod = `__pluginMod${i}`;
|
2022-09-08 20:25:21 +00:00
|
|
|
code += `import ${mod} from "./${files[i].replace(/.tsx?$/, "")}";\n`;
|
2022-08-31 21:04:18 +00:00
|
|
|
obj += `[${mod}.name]: ${mod},`;
|
2022-08-29 16:11:44 +00:00
|
|
|
}
|
2022-08-31 21:04:18 +00:00
|
|
|
code += `export default {${obj}}`;
|
2022-08-29 16:11:44 +00:00
|
|
|
return {
|
|
|
|
contents: code,
|
|
|
|
resolveDir: "./src/plugins"
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-29 23:42:47 +00:00
|
|
|
const gitHash = execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim();
|
|
|
|
/**
|
|
|
|
* @type {esbuild.Plugin}
|
|
|
|
*/
|
|
|
|
const gitHashPlugin = {
|
|
|
|
name: "git-hash-plugin",
|
|
|
|
setup: build => {
|
|
|
|
const filter = /^git-hash$/;
|
|
|
|
build.onResolve({ filter }, args => ({
|
|
|
|
namespace: "git-hash", path: args.path
|
|
|
|
}));
|
|
|
|
build.onLoad({ filter, namespace: "git-hash" }, () => ({
|
|
|
|
contents: `export default "${gitHash}"`
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-29 00:25:27 +00:00
|
|
|
await Promise.all([
|
|
|
|
esbuild.build({
|
2022-09-16 20:40:22 +00:00
|
|
|
logLevel: "info",
|
2022-08-29 00:25:27 +00:00
|
|
|
entryPoints: ["src/preload.ts"],
|
|
|
|
outfile: "dist/preload.js",
|
|
|
|
format: "cjs",
|
2022-08-29 16:11:44 +00:00
|
|
|
bundle: true,
|
2022-08-29 00:25:27 +00:00
|
|
|
platform: "node",
|
2022-08-29 16:11:44 +00:00
|
|
|
target: ["esnext"],
|
2022-08-29 18:27:47 +00:00
|
|
|
sourcemap: "linked",
|
2022-08-29 16:11:44 +00:00
|
|
|
plugins: [makeAllPackagesExternalPlugin],
|
|
|
|
watch
|
2022-08-29 00:25:27 +00:00
|
|
|
}),
|
|
|
|
esbuild.build({
|
2022-09-16 20:40:22 +00:00
|
|
|
logLevel: "info",
|
2022-08-29 00:25:27 +00:00
|
|
|
entryPoints: ["src/patcher.ts"],
|
|
|
|
outfile: "dist/patcher.js",
|
2022-08-29 16:11:44 +00:00
|
|
|
bundle: true,
|
2022-08-29 00:25:27 +00:00
|
|
|
format: "cjs",
|
|
|
|
target: ["esnext"],
|
2022-08-29 16:11:44 +00:00
|
|
|
external: ["electron"],
|
|
|
|
platform: "node",
|
2022-08-29 18:27:47 +00:00
|
|
|
sourcemap: "linked",
|
2022-08-29 16:11:44 +00:00
|
|
|
plugins: [makeAllPackagesExternalPlugin],
|
|
|
|
watch
|
2022-08-29 00:25:27 +00:00
|
|
|
}),
|
|
|
|
esbuild.build({
|
2022-09-16 20:40:22 +00:00
|
|
|
logLevel: "info",
|
2022-08-29 00:25:27 +00:00
|
|
|
entryPoints: ["src/Vencord.ts"],
|
|
|
|
outfile: "dist/renderer.js",
|
|
|
|
format: "iife",
|
|
|
|
bundle: true,
|
|
|
|
target: ["esnext"],
|
|
|
|
footer: { js: "//# sourceURL=VencordRenderer" },
|
2022-08-29 16:11:44 +00:00
|
|
|
globalName: "Vencord",
|
2022-08-29 23:42:47 +00:00
|
|
|
external: ["plugins", "git-hash"],
|
2022-08-29 16:11:44 +00:00
|
|
|
plugins: [
|
2022-08-29 23:42:47 +00:00
|
|
|
globPlugins,
|
|
|
|
gitHashPlugin
|
2022-08-29 16:11:44 +00:00
|
|
|
],
|
2022-09-07 13:38:50 +00:00
|
|
|
sourcemap: false,
|
2022-08-29 20:05:22 +00:00
|
|
|
watch,
|
2022-09-16 17:26:03 +00:00
|
|
|
minify: true,
|
2022-10-03 22:52:42 +00:00
|
|
|
}),
|
2022-09-16 20:40:22 +00:00
|
|
|
]).catch(err => {
|
2022-08-29 16:11:44 +00:00
|
|
|
console.error("Build failed");
|
|
|
|
console.error(err.message);
|
2022-10-09 20:35:59 +00:00
|
|
|
// make ci fail
|
|
|
|
if (!watch)
|
|
|
|
process.exitCode = 1;
|
2022-08-29 16:11:44 +00:00
|
|
|
});
|