diff options
Diffstat (limited to 'scripts/build')
-rwxr-xr-x | scripts/build/build.mjs | 38 | ||||
-rwxr-xr-x | scripts/build/buildWeb.mjs | 6 | ||||
-rw-r--r-- | scripts/build/common.mjs | 42 |
3 files changed, 68 insertions, 18 deletions
diff --git a/scripts/build/build.mjs b/scripts/build/build.mjs index 163f4ce..bbf8105 100755 --- a/scripts/build/build.mjs +++ b/scripts/build/build.mjs @@ -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 => { diff --git a/scripts/build/buildWeb.mjs b/scripts/build/buildWeb.mjs index 009d5e8..f0197b7 100755 --- a/scripts/build/buildWeb.mjs +++ b/scripts/build/buildWeb.mjs @@ -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" } }; diff --git a/scripts/build/common.mjs b/scripts/build/common.mjs index c3afc7f..db87134 100644 --- a/scripts/build/common.mjs +++ b/scripts/build/common.mjs @@ -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 })); @@ -96,10 +99,32 @@ 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"] }; |