aboutsummaryrefslogtreecommitdiff
path: root/scripts/build/buildWeb.mjs
diff options
context:
space:
mode:
authorVen <vendicated@riseup.net>2022-12-25 20:47:35 +0100
committerGitHub <noreply@github.com>2022-12-25 20:47:35 +0100
commit2e5d27b6b63097e96e25819df7a8cdd667c521b3 (patch)
tree082b0f1c7cb0210d208c7cb8017e9da97b3b4196 /scripts/build/buildWeb.mjs
parent2172cae779fb24f9bcc8c54a0b6538da0b52bafd (diff)
downloadVencord-2e5d27b6b63097e96e25819df7a8cdd667c521b3.tar.gz
Vencord-2e5d27b6b63097e96e25819df7a8cdd667c521b3.tar.bz2
Vencord-2e5d27b6b63097e96e25819df7a8cdd667c521b3.zip
feat: Proper CSS api & css bundle (#269)
Co-authored-by: Vap0r1ze <superdash993@gmail.com>
Diffstat (limited to 'scripts/build/buildWeb.mjs')
-rw-r--r--[-rwxr-xr-x]scripts/build/buildWeb.mjs74
1 files changed, 47 insertions, 27 deletions
diff --git a/scripts/build/buildWeb.mjs b/scripts/build/buildWeb.mjs
index c85d8aa..3ad43b2 100755..100644
--- a/scripts/build/buildWeb.mjs
+++ b/scripts/build/buildWeb.mjs
@@ -20,13 +20,13 @@
import esbuild from "esbuild";
import { zip } from "fflate";
-import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
-import { readFile } from "fs/promises";
-import { join, resolve } from "path";
+import { readFileSync } from "fs";
+import { appendFile, mkdir, readFile, rm, writeFile } from "fs/promises";
+import { join } from "path";
// wtf is this assert syntax
import PackageJSON from "../../package.json" assert { type: "json" };
-import { commonOpts, fileIncludePlugin, gitHashPlugin, gitRemotePlugin, globPlugins, watch } from "./common.mjs";
+import { commonOpts, globPlugins, watch } from "./common.mjs";
/**
* @type {esbuild.BuildOptions}
@@ -39,9 +39,7 @@ const commonOptions = {
external: ["plugins", "git-hash"],
plugins: [
globPlugins,
- gitHashPlugin,
- gitRemotePlugin,
- fileIncludePlugin
+ ...commonOpts.plugins,
],
target: ["esnext"],
define: {
@@ -77,9 +75,13 @@ await Promise.all(
]
);
+/**
+ * @type {(target: string, files: string[], shouldZip: boolean) => Promise<void>}
+ */
async function buildPluginZip(target, files, shouldZip) {
const entries = {
- "dist/Vencord.js": readFileSync("dist/browser.js"),
+ "dist/Vencord.js": await readFile("dist/browser.js"),
+ "dist/Vencord.css": await readFile("dist/browser.css"),
...Object.fromEntries(await Promise.all(files.map(async f => [
(f.startsWith("manifest") ? "manifest.json" : f),
await readFile(join("browser", f))
@@ -87,29 +89,47 @@ async function buildPluginZip(target, files, shouldZip) {
};
if (shouldZip) {
- zip(entries, {}, (err, data) => {
- if (err) {
- console.error(err);
- process.exitCode = 1;
- } else {
- writeFileSync("dist/" + target, data);
- console.info("Extension written to dist/" + target);
- }
+ return new Promise((resolve, reject) => {
+ zip(entries, {}, (err, data) => {
+ if (err) {
+ reject(err);
+ } else {
+ const out = join("dist", target);
+ writeFile(out, data).then(() => {
+ console.info("Extension written to " + out);
+ resolve();
+ }).catch(reject);
+ }
+ });
});
} else {
- if (existsSync(target))
- rmSync(target, { recursive: true });
- for (const entry in entries) {
- const destination = "dist/" + target + "/" + entry;
- const parentDirectory = resolve(destination, "..");
- mkdirSync(parentDirectory, { recursive: true });
- writeFileSync(destination, entries[entry]);
- }
+ await rm(target, { recursive: true, force: true });
+ await Promise.all(Object.entries(entries).map(async ([file, content]) => {
+ const dest = join("dist", target, file);
+ const parentDirectory = join(dest, "..");
+ await mkdir(parentDirectory, { recursive: true });
+ await writeFile(dest, content);
+ }));
+
console.info("Unpacked Extension written to dist/" + target);
}
}
-await buildPluginZip("extension-v3.zip", ["modifyResponseHeaders.json", "content.js", "manifestv3.json"], true);
-await buildPluginZip("extension-v2.zip", ["background.js", "content.js", "manifestv2.json"], true);
-await buildPluginZip("extension-v2-unpacked", ["background.js", "content.js", "manifestv2.json"], false);
+const cssText = "`" + readFileSync("dist/Vencord.user.css", "utf-8").replaceAll("`", "\\`") + "`";
+const cssRuntime = `
+;document.addEventListener("DOMContentLoaded", () => document.documentElement.appendChild(
+ Object.assign(document.createElement("style"), {
+ textContent: ${cssText},
+ id: "vencord-css-core"
+ }),
+ { once: true }
+));
+`;
+
+await Promise.all([
+ appendFile("dist/Vencord.user.js", cssRuntime),
+ buildPluginZip("extension-v3.zip", ["modifyResponseHeaders.json", "content.js", "manifestv3.json"], true),
+ buildPluginZip("extension-v2.zip", ["background.js", "content.js", "manifestv2.json"], true),
+ buildPluginZip("extension-v2-unpacked", ["background.js", "content.js", "manifestv2.json"], false),
+]);