Vencord/build.mjs

130 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-08-28 20:25:27 -04:00
#!/usr/bin/node
import { execSync } from "child_process";
2022-08-28 20:25:27 -04:00
import esbuild from "esbuild";
2022-08-29 12:11:44 -04:00
import { readdirSync } from "fs";
import { performance } from "perf_hooks";
2022-08-28 20:25:27 -04:00
2022-08-29 12:11:44 -04:00
/**
2022-08-29 12:45:58 -04:00
* @type {esbuild.WatchMode|false}
2022-08-29 12:11:44 -04:00
*/
2022-08-29 12:45:58 -04:00
const watch = process.argv.includes("--watch") ? {
2022-08-29 12:11:44 -04:00
onRebuild: (err) => {
if (err) console.error("Build Error", err.message);
else console.log("Rebuilt!");
}
2022-08-29 12:45:58 -04:00
} : false;
2022-08-29 12:11:44 -04:00
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
2022-08-29 12:45:58 -04:00
/**
* @type {esbuild.Plugin}
*/
2022-08-29 12:11:44 -04:00
const makeAllPackagesExternalPlugin = {
name: 'make-all-packages-external',
setup(build) {
let filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/; // Must not start with "/" or "./" or "../"
build.onResolve({ filter }, args => ({ path: args.path, external: true }));
},
};
2022-08-29 12:45:58 -04:00
/**
* @type {esbuild.Plugin}
*/
2022-08-29 12:11:44 -04: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 = "";
let obj = "";
2022-08-29 12:11:44 -04:00
for (let i = 0; i < files.length; i++) {
if (files[i] === "index.ts") {
continue;
}
const mod = `__pluginMod${i}`;
code += `import ${mod} from "./${files[i].replace(/.tsx?$/, "")}";\n`;
obj += `[${mod}.name]: ${mod},`;
2022-08-29 12:11:44 -04:00
}
code += `export default {${obj}}`;
2022-08-29 12:11:44 -04:00
return {
contents: code,
resolveDir: "./src/plugins"
};
});
}
};
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 12:11:44 -04:00
const begin = performance.now();
2022-08-28 20:25:27 -04:00
await Promise.all([
esbuild.build({
entryPoints: ["src/preload.ts"],
outfile: "dist/preload.js",
format: "cjs",
2022-08-29 12:11:44 -04:00
bundle: true,
2022-08-28 20:25:27 -04:00
platform: "node",
2022-08-29 12:11:44 -04:00
target: ["esnext"],
2022-08-29 14:27:47 -04:00
sourcemap: "linked",
2022-08-29 12:11:44 -04:00
plugins: [makeAllPackagesExternalPlugin],
watch
2022-08-28 20:25:27 -04:00
}),
esbuild.build({
entryPoints: ["src/patcher.ts"],
outfile: "dist/patcher.js",
2022-08-29 12:11:44 -04:00
bundle: true,
2022-08-28 20:25:27 -04:00
format: "cjs",
target: ["esnext"],
2022-08-29 12:11:44 -04:00
external: ["electron"],
platform: "node",
2022-08-29 14:27:47 -04:00
sourcemap: "linked",
2022-08-29 12:11:44 -04:00
plugins: [makeAllPackagesExternalPlugin],
watch
2022-08-28 20:25:27 -04:00
}),
esbuild.build({
entryPoints: ["src/Vencord.ts"],
outfile: "dist/renderer.js",
format: "iife",
bundle: true,
target: ["esnext"],
footer: { js: "//# sourceURL=VencordRenderer" },
2022-08-29 12:11:44 -04:00
globalName: "Vencord",
external: ["plugins", "git-hash"],
2022-08-29 12:11:44 -04:00
plugins: [
globPlugins,
gitHashPlugin
2022-08-29 12:11:44 -04:00
],
sourcemap: false,
2022-08-29 16:05:22 -04:00
watch,
2022-08-30 22:07:16 -04:00
minify: false,
2022-08-28 20:25:27 -04:00
})
2022-08-29 12:11:44 -04:00
]).then(res => {
const took = performance.now() - begin;
console.log(`Built in ${took.toFixed(2)}ms`);
}).catch(err => {
console.error("Build failed");
console.error(err.message);
});
2022-08-28 20:25:27 -04:00
2022-08-29 12:11:44 -04:00
if (watch) console.log("Watching...");