mirror of
https://github.com/Equicord/Equicord.git
synced 2025-06-08 06:03:03 -04:00
feat: Proper CSS api & css bundle (#269)
Co-authored-by: Vap0r1ze <superdash993@gmail.com>
This commit is contained in:
parent
2172cae779
commit
2e5d27b6b6
31 changed files with 438 additions and 126 deletions
74
scripts/build/buildWeb.mjs
Executable file → Normal file
74
scripts/build/buildWeb.mjs
Executable file → Normal file
|
@ -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),
|
||||
]);
|
||||
|
||||
|
|
|
@ -17,9 +17,9 @@
|
|||
*/
|
||||
|
||||
import { exec, execSync } from "child_process";
|
||||
import { existsSync } from "fs";
|
||||
import { existsSync, readFileSync } from "fs";
|
||||
import { readdir, readFile } from "fs/promises";
|
||||
import { join } from "path";
|
||||
import { join, relative } from "path";
|
||||
import { promisify } from "util";
|
||||
|
||||
export const watch = process.argv.includes("--watch");
|
||||
|
@ -35,7 +35,7 @@ export const banner = {
|
|||
|
||||
// https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
|
||||
/**
|
||||
* @type {esbuild.Plugin}
|
||||
* @type {import("esbuild").Plugin}
|
||||
*/
|
||||
export const makeAllPackagesExternalPlugin = {
|
||||
name: "make-all-packages-external",
|
||||
|
@ -46,7 +46,7 @@ export const makeAllPackagesExternalPlugin = {
|
|||
};
|
||||
|
||||
/**
|
||||
* @type {esbuild.Plugin}
|
||||
* @type {import("esbuild").Plugin}
|
||||
*/
|
||||
export const globPlugins = {
|
||||
name: "glob-plugins",
|
||||
|
@ -87,7 +87,7 @@ export const globPlugins = {
|
|||
};
|
||||
|
||||
/**
|
||||
* @type {esbuild.Plugin}
|
||||
* @type {import("esbuild").Plugin}
|
||||
*/
|
||||
export const gitHashPlugin = {
|
||||
name: "git-hash-plugin",
|
||||
|
@ -103,7 +103,7 @@ export const gitHashPlugin = {
|
|||
};
|
||||
|
||||
/**
|
||||
* @type {esbuild.Plugin}
|
||||
* @type {import("esbuild").Plugin}
|
||||
*/
|
||||
export const gitRemotePlugin = {
|
||||
name: "git-remote-plugin",
|
||||
|
@ -125,7 +125,7 @@ export const gitRemotePlugin = {
|
|||
};
|
||||
|
||||
/**
|
||||
* @type {esbuild.Plugin}
|
||||
* @type {import("esbuild").Plugin}
|
||||
*/
|
||||
export const fileIncludePlugin = {
|
||||
name: "file-include-plugin",
|
||||
|
@ -147,6 +147,31 @@ export const fileIncludePlugin = {
|
|||
}
|
||||
};
|
||||
|
||||
const styleModule = readFileSync("./scripts/build/module/style.js", "utf-8");
|
||||
/**
|
||||
* @type {import("esbuild").Plugin}
|
||||
*/
|
||||
export const stylePlugin = {
|
||||
name: "style-plugin",
|
||||
setup: ({ onResolve, onLoad }) => {
|
||||
onResolve({ filter: /\.css\?managed$/, namespace: "file" }, ({ path, resolveDir }) => ({
|
||||
path: relative(process.cwd(), join(resolveDir, path.replace("?managed", ""))),
|
||||
namespace: "managed-style",
|
||||
}));
|
||||
onLoad({ filter: /\.css$/, namespace: "managed-style" }, async ({ path }) => {
|
||||
const css = await readFile(path, "utf-8");
|
||||
const name = relative(process.cwd(), path).replaceAll("\\", "/");
|
||||
|
||||
return {
|
||||
loader: "js",
|
||||
contents: styleModule
|
||||
.replaceAll("STYLE_SOURCE", JSON.stringify(css))
|
||||
.replaceAll("STYLE_NAME", JSON.stringify(name))
|
||||
};
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @type {import("esbuild").BuildOptions}
|
||||
*/
|
||||
|
@ -158,7 +183,7 @@ export const commonOpts = {
|
|||
sourcemap: watch ? "inline" : "",
|
||||
legalComments: "linked",
|
||||
banner,
|
||||
plugins: [fileIncludePlugin, gitHashPlugin, gitRemotePlugin],
|
||||
plugins: [fileIncludePlugin, gitHashPlugin, gitRemotePlugin, stylePlugin],
|
||||
external: ["~plugins", "~git-hash", "~git-remote"],
|
||||
inject: ["./scripts/build/inject/react.mjs"],
|
||||
jsxFactory: "VencordCreateElement",
|
||||
|
|
26
scripts/build/module/style.js
Normal file
26
scripts/build/module/style.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
(window.VencordStyles ??= new Map()).set(STYLE_NAME, {
|
||||
name: STYLE_NAME,
|
||||
source: STYLE_SOURCE,
|
||||
classNames: {},
|
||||
dom: null,
|
||||
});
|
||||
|
||||
export default STYLE_NAME;
|
Loading…
Add table
Add a link
Reference in a new issue