Vencord Standalone without git/node (#148)

This commit is contained in:
Ven 2022-10-23 23:23:52 +02:00 committed by GitHub
parent ffbb52512c
commit 5fac8be0ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 373 additions and 129 deletions

View file

@ -18,7 +18,22 @@
*/
import esbuild from "esbuild";
import { commonOpts, fileIncludePlugin, gitHashPlugin, globPlugins, makeAllPackagesExternalPlugin } from "./common.mjs";
import { commonOpts, gitHash, globPlugins, isStandalone } from "./common.mjs";
const defines = {
IS_STANDALONE: isStandalone
};
if (defines.IS_STANDALONE === "false")
// If this is a local build (not standalone), optimise
// for the specific platform we're on
defines["process.platform"] = JSON.stringify(process.platform);
const header = `
// Vencord ${gitHash}
// Standalone: ${defines.IS_STANDALONE}
// Platform: ${defines["process.platform"] || "Universal"}
`.trim();
/**
* @type {esbuild.BuildOptions}
@ -30,8 +45,11 @@ const nodeCommonOpts = {
target: ["esnext"],
minify: true,
bundle: true,
sourcemap: "linked",
external: ["electron"]
external: ["electron", ...commonOpts.external],
define: defines,
banner: {
js: header
}
};
await Promise.all([
@ -39,11 +57,15 @@ await Promise.all([
...nodeCommonOpts,
entryPoints: ["src/preload.ts"],
outfile: "dist/preload.js",
footer: { js: "//# sourceURL=VencordPreload\n//# sourceMappingURL=vencord://preload.js.map" },
sourcemap: "external",
}),
esbuild.build({
...nodeCommonOpts,
entryPoints: ["src/patcher.ts"],
outfile: "dist/patcher.js",
footer: { js: "//# sourceURL=VencordPatcher\n//# sourceMappingURL=vencord://patcher.js.map" },
sourcemap: "external",
}),
esbuild.build({
...commonOpts,
@ -51,16 +73,16 @@ await Promise.all([
outfile: "dist/renderer.js",
format: "iife",
target: ["esnext"],
footer: { js: "//# sourceURL=VencordRenderer" },
footer: { js: "//# sourceURL=VencordRenderer\n//# sourceMappingURL=vencord://renderer.js.map" },
globalName: "Vencord",
external: ["plugins", "git-hash"],
sourcemap: "external",
plugins: [
globPlugins,
gitHashPlugin,
fileIncludePlugin
...commonOpts.plugins
],
define: {
IS_WEB: "false"
IS_WEB: "false",
IS_STANDALONE: isStandalone
}
}),
]).catch(err => {

View file

@ -26,7 +26,7 @@ import { join } from "path";
// wtf is this assert syntax
import PackageJSON from "../../package.json" assert { type: "json" };
import { commonOpts, fileIncludePlugin, gitHashPlugin, globPlugins } from "./common.mjs";
import { commonOpts, fileIncludePlugin, gitHashPlugin, gitRemotePlugin, globPlugins } from "./common.mjs";
/**
* @type {esbuild.BuildOptions}
@ -40,11 +40,13 @@ const commonOptions = {
plugins: [
globPlugins,
gitHashPlugin,
gitRemotePlugin,
fileIncludePlugin
],
target: ["esnext"],
define: {
IS_WEB: "true"
IS_WEB: "true",
IS_STANDALONE: "true"
}
};

View file

@ -16,13 +16,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { execSync } from "child_process";
import { exec, execSync } from "child_process";
import esbuild from "esbuild";
import { existsSync } from "fs";
import { readdir, readFile } from "fs/promises";
import { join } from "path";
import { promisify } from "util";
const watch = process.argv.includes("--watch");
export const watch = process.argv.includes("--watch");
export const isStandalone = JSON.stringify(process.argv.includes("--standalone"));
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
/**
@ -42,14 +44,15 @@ export const makeAllPackagesExternalPlugin = {
export const globPlugins = {
name: "glob-plugins",
setup: build => {
build.onResolve({ filter: /^plugins$/ }, args => {
const filter = /^~plugins$/;
build.onResolve({ filter }, args => {
return {
namespace: "import-plugins",
path: args.path
};
});
build.onLoad({ filter: /^plugins$/, namespace: "import-plugins" }, async () => {
build.onLoad({ filter, namespace: "import-plugins" }, async () => {
const pluginDirs = ["plugins", "userplugins"];
let code = "";
let plugins = "\n";
@ -76,14 +79,14 @@ export const globPlugins = {
}
};
const gitHash = execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim();
export const gitHash = execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim();
/**
* @type {esbuild.Plugin}
*/
export const gitHashPlugin = {
name: "git-hash-plugin",
setup: build => {
const filter = /^git-hash$/;
const filter = /^~git-hash$/;
build.onResolve({ filter }, args => ({
namespace: "git-hash", path: args.path
}));
@ -93,13 +96,35 @@ export const gitHashPlugin = {
}
};
/**
* @type {esbuild.Plugin}
*/
export const gitRemotePlugin = {
name: "git-remote-plugin",
setup: build => {
const filter = /^~git-remote$/;
build.onResolve({ filter }, args => ({
namespace: "git-remote", path: args.path
}));
build.onLoad({ filter, namespace: "git-remote" }, async () => {
const res = await promisify(exec)("git remote get-url origin", { encoding: "utf-8" });
const remote = res.stdout.trim()
.replace("https://github.com/", "")
.replace("git@github.com:", "")
.replace(/.git$/, "");
return { contents: `export default "${remote}"` };
});
}
};
/**
* @type {esbuild.Plugin}
*/
export const fileIncludePlugin = {
name: "file-include-plugin",
setup: build => {
const filter = /^@fileContent\/.+$/;
const filter = /^~fileContent\/.+$/;
build.onResolve({ filter }, args => ({
namespace: "include-file",
path: args.path,
@ -126,5 +151,6 @@ export const commonOpts = {
minify: !watch,
sourcemap: watch ? "inline" : "",
legalComments: "linked",
plugins: [fileIncludePlugin]
plugins: [fileIncludePlugin, gitHashPlugin, gitRemotePlugin],
external: ["~plugins", "~git-hash", "~git-remote"]
};