Equicord/scripts/build/buildWeb.mjs

210 lines
6.7 KiB
JavaScript
Raw Normal View History

2026-02-18 23:21:01 +01:00
#!/usr/bin/env bun
2022-10-22 01:22:44 +02:00
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// @ts-check
2026-02-18 23:21:01 +01:00
import { appendFile, mkdir, readdir, rm } from "fs/promises";
import { join } from "path";
2025-12-16 23:57:41 -05:00
import Zip from "zip-local";
2022-10-22 23:35:30 +02:00
import { BUILD_TIMESTAMP, commonOpts, globPlugins, IS_DEV, IS_REPORTER, IS_COMPANION_TEST, VERSION, commonRendererPlugins, buildOrWatchAll, stringifyValues, IS_ANTI_CRASH_TEST } from "./common.mjs";
/**
* @type {import("esbuild").BuildOptions}
*/
2024-09-04 19:20:15 -04:00
const commonOptions = {
...commonOpts,
entryPoints: ["browser/Vencord.ts"],
format: "iife",
globalName: "Vencord",
external: ["~plugins", "~git-hash", "/assets/*"],
target: ["esnext"],
plugins: [
globPlugins("web"),
...commonRendererPlugins
],
define: stringifyValues({
IS_WEB: true,
IS_EXTENSION: false,
IS_USERSCRIPT: false,
IS_STANDALONE: true,
IS_DEV,
IS_REPORTER,
IS_COMPANION_TEST,
IS_ANTI_CRASH_TEST,
IS_DISCORD_DESKTOP: false,
IS_VESKTOP: false,
2025-02-12 20:38:37 -05:00
IS_EQUIBOP: false,
IS_UPDATER_DISABLED: true,
VERSION,
BUILD_TIMESTAMP
})
};
const MonacoWorkerEntryPoints = [
"vs/language/css/css.worker.js",
"vs/editor/editor.worker.js"
];
/** @type {import("esbuild").BuildOptions[]} */
const buildConfigs = [
{
entryPoints: MonacoWorkerEntryPoints.map(entry => `node_modules/monaco-editor/esm/${entry}`),
bundle: true,
minify: true,
format: "iife",
outbase: "node_modules/monaco-editor/esm/",
2025-02-23 11:41:11 -05:00
outdir: "dist/browser/vendor/monaco"
},
{
entryPoints: ["browser/monaco.ts"],
bundle: true,
minify: true,
format: "iife",
2025-02-23 11:33:05 -05:00
outfile: "dist/browser/vendor/monaco/index.js",
loader: {
".ttf": "file"
}
},
{
...commonOptions,
2025-02-12 20:35:31 -05:00
outfile: "dist/browser/browser.js",
footer: { js: "//# sourceURL=file:///VencordWeb" }
},
{
...commonOptions,
2025-02-23 11:33:05 -05:00
outfile: "dist/browser/extension.js",
define: {
...commonOptions.define,
IS_EXTENSION: "true"
},
footer: { js: "//# sourceURL=file:///VencordWeb" }
},
{
...commonOptions,
inject: ["browser/GMPolyfill.js", ...(commonOptions?.inject || [])],
define: {
...commonOptions.define,
IS_USERSCRIPT: "true",
window: "unsafeWindow",
},
2025-10-27 09:42:44 -04:00
outfile: "dist/Equicord.user.js",
banner: {
2026-02-18 23:21:01 +01:00
js: (await Bun.file("browser/userscript.meta.js").text()).replace("%version%", `${VERSION}.${Date.now()}`)
},
footer: {
// UserScripts get wrapped in an iife, so define Vencord prop on window that returns our local
js: "Object.defineProperty(unsafeWindow,'Vencord',{get:()=>Vencord});"
}
}
2023-09-21 18:19:59 +02:00
];
await buildOrWatchAll(buildConfigs);
2024-09-04 19:20:15 -04:00
/**
* @type {(dir: string) => Promise<string[]>}
*/
async function globDir(dir) {
const files = [];
for (const child of await readdir(dir, { withFileTypes: true })) {
const p = join(dir, child.name);
if (child.isDirectory())
files.push(...await globDir(p));
else
files.push(p);
}
return files;
}
2024-09-04 19:20:15 -04:00
/**
* @type {(dir: string, basePath?: string) => Promise<Record<string, string>>}
*/
async function loadDir(dir, basePath = "") {
const files = await globDir(dir);
2024-09-04 19:20:15 -04:00
return Object.fromEntries(
await Promise.all(
files.map(
async f =>
2026-02-18 23:21:01 +01:00
[f.slice(basePath.length), Buffer.from(await Bun.file(f).arrayBuffer())]
2024-09-04 19:20:15 -04:00
)
)
);
}
2024-09-04 19:20:15 -04:00
/**
* @type {(target: string, files: string[]) => Promise<void>}
*/
async function buildExtension(target, files) {
const entries = {
2026-02-18 23:21:01 +01:00
"dist/Equicord.js": Buffer.from(await Bun.file("dist/browser/extension.js").arrayBuffer()),
"dist/Equicord.css": Buffer.from(await Bun.file("dist/browser/extension.css").arrayBuffer()),
2025-02-23 11:33:05 -05:00
...await loadDir("dist/browser/vendor/monaco", "dist/browser/"),
2023-01-26 22:38:02 +01:00
...Object.fromEntries(await Promise.all(files.map(async f => {
2026-02-18 23:21:01 +01:00
let content = Buffer.from(await Bun.file(join("browser", f)).arrayBuffer());
2023-01-26 22:38:02 +01:00
if (f.startsWith("manifest")) {
const json = JSON.parse(content.toString("utf-8"));
json.version = VERSION;
2026-02-18 23:21:01 +01:00
content = Buffer.from(JSON.stringify(json));
2023-01-26 22:38:02 +01:00
}
return [
f.startsWith("manifest") ? "manifest.json" : f,
content
2024-09-04 19:20:15 -04:00
];
})))
2022-12-01 19:16:09 +01:00
};
await rm(target, { recursive: true, force: true });
await Promise.all(Object.entries(entries).map(async ([file, content]) => {
2024-07-18 04:34:09 +02:00
const dest = join("dist/browser", target, file);
const parentDirectory = join(dest, "..");
await mkdir(parentDirectory, { recursive: true });
2026-02-18 23:21:01 +01:00
await Bun.write(dest, content);
}));
2024-07-18 04:34:09 +02:00
console.info("Unpacked Extension written to dist/browser/" + target);
2022-12-01 19:16:09 +01:00
}
2026-02-18 23:21:01 +01:00
const appendCssRuntime = Bun.file("dist/Equicord.user.css").text().then(content => {
const cssRuntime = `unsafeWindow._vcUserScriptRendererCss=\`${content.replaceAll("`", "\\`")}\``;
2025-10-27 09:42:44 -04:00
return appendFile("dist/Equicord.user.js", cssRuntime);
2023-01-26 22:38:02 +01:00
});
if (!process.argv.includes("--skip-extension")) {
await Promise.all([
appendCssRuntime,
buildExtension("chromium-unpacked", ["modifyResponseHeaders.json", "content.js", "manifest.json", "icon.png"]),
buildExtension("firefox-unpacked", ["background.js", "content.js", "manifestv2.json", "icon.png"]),
]);
2022-12-01 19:16:09 +01:00
2025-12-16 23:57:41 -05:00
Zip.zip("dist/browser/chromium-unpacked", (_err, zip) => {
zip.compress().save("dist/extension-chrome.zip");
console.info("Packed Chromium Extension written to dist/extension-chrome.zip");
});
Zip.zip("dist/browser/firefox-unpacked", (_err, zip) => {
zip.compress().save("dist/extension-firefox.zip");
console.info("Packed Firefox Extension written to dist/extension-firefox.zip");
});
} else {
await appendCssRuntime;
}