You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.6 KiB

2 years ago
#!/usr/bin/node
import { execSync } from "child_process";
2 years ago
import esbuild from "esbuild";
2 years ago
import { readdirSync } from "fs";
import { performance } from "perf_hooks";
2 years ago
2 years ago
/**
* @type {esbuild.WatchMode|false}
2 years ago
*/
const watch = process.argv.includes("--watch") ? {
2 years ago
onRebuild: (err) => {
if (err) console.error("Build Error", err.message);
else console.log("Rebuilt!");
}
} : false;
2 years ago
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
/**
* @type {esbuild.Plugin}
*/
2 years ago
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 }));
},
};
/**
* @type {esbuild.Plugin}
*/
2 years ago
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 = "";
2 years ago
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(".ts", "")}";\n`;
obj += `[${mod}.name]: ${mod},`;
2 years ago
}
code += `export default {${obj}}`;
2 years ago
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}"`
}));
}
};
2 years ago
const begin = performance.now();
2 years ago
await Promise.all([
esbuild.build({
entryPoints: ["src/preload.ts"],
outfile: "dist/preload.js",
format: "cjs",
2 years ago
bundle: true,
2 years ago
platform: "node",
2 years ago
target: ["esnext"],
2 years ago
sourcemap: "linked",
2 years ago
plugins: [makeAllPackagesExternalPlugin],
watch
2 years ago
}),
esbuild.build({
entryPoints: ["src/patcher.ts"],
outfile: "dist/patcher.js",
2 years ago
bundle: true,
2 years ago
format: "cjs",
target: ["esnext"],
2 years ago
external: ["electron"],
platform: "node",
2 years ago
sourcemap: "linked",
2 years ago
plugins: [makeAllPackagesExternalPlugin],
watch
2 years ago
}),
esbuild.build({
entryPoints: ["src/Vencord.ts"],
outfile: "dist/renderer.js",
format: "iife",
bundle: true,
target: ["esnext"],
footer: { js: "//# sourceURL=VencordRenderer" },
2 years ago
globalName: "Vencord",
external: ["plugins", "git-hash"],
2 years ago
plugins: [
globPlugins,
gitHashPlugin
2 years ago
],
sourcemap: false,
2 years ago
watch,
minify: false,
2 years ago
})
2 years ago
]).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);
});
2 years ago
2 years ago
if (watch) console.log("Watching...");