diff options
author | V <vendicated@riseup.net> | 2023-09-19 04:07:24 +0200 |
---|---|---|
committer | V <vendicated@riseup.net> | 2023-09-19 04:11:27 +0200 |
commit | 41f5d71e38f785786656e111cf2ea1200e45886d (patch) | |
tree | 564a0300485de18a0b8d396118f09c6f756105ed /scripts | |
parent | efb88a4df8037fc1394a9e2053c49e75d340f401 (diff) | |
download | Vencord-41f5d71e38f785786656e111cf2ea1200e45886d.tar.gz Vencord-41f5d71e38f785786656e111cf2ea1200e45886d.tar.bz2 Vencord-41f5d71e38f785786656e111cf2ea1200e45886d.zip |
Bundle dependencies with extensions for webstore rule compliance (#1740)
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/build/build.mjs | 2 | ||||
-rw-r--r-- | scripts/build/buildWeb.mjs | 118 |
2 files changed, 83 insertions, 37 deletions
diff --git a/scripts/build/build.mjs b/scripts/build/build.mjs index ce84fed..97e50b9 100755 --- a/scripts/build/build.mjs +++ b/scripts/build/build.mjs @@ -78,6 +78,7 @@ await Promise.all([ define: { ...defines, IS_WEB: false, + IS_EXTENSION: false, IS_DISCORD_DESKTOP: true, IS_VESKTOP: false } @@ -124,6 +125,7 @@ await Promise.all([ define: { ...defines, IS_WEB: false, + IS_EXTENSION: false, IS_DISCORD_DESKTOP: false, IS_VESKTOP: true } diff --git a/scripts/build/buildWeb.mjs b/scripts/build/buildWeb.mjs index aab82d0..8cf1ff3 100644 --- a/scripts/build/buildWeb.mjs +++ b/scripts/build/buildWeb.mjs @@ -17,12 +17,11 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ - import esbuild from "esbuild"; -import { zip } from "fflate"; import { readFileSync } from "fs"; -import { appendFile, mkdir, readFile, rm, writeFile } from "fs/promises"; +import { appendFile, mkdir, readdir, readFile, rm, writeFile } from "fs/promises"; import { join } from "path"; +import Zip from "zip-local"; import { BUILD_TIMESTAMP, commonOpts, globPlugins, VERSION, watch } from "./common.mjs"; @@ -42,6 +41,7 @@ const commonOptions = { target: ["esnext"], define: { IS_WEB: "true", + IS_EXTENSION: "false", IS_STANDALONE: "true", IS_DEV: JSON.stringify(watch), IS_DISCORD_DESKTOP: "false", @@ -52,19 +52,51 @@ const commonOptions = { } }; +const MonacoWorkerEntryPoints = [ + "vs/language/css/css.worker.js", + "vs/editor/editor.worker.js" +]; + await Promise.all( [ esbuild.build({ + entryPoints: MonacoWorkerEntryPoints.map(entry => `node_modules/monaco-editor/esm/${entry}`), + bundle: true, + minify: true, + format: "iife", + outbase: "node_modules/monaco-editor/esm/", + outdir: "dist/monaco" + }), + esbuild.build({ + entryPoints: ["browser/monaco.ts"], + bundle: true, + minify: true, + format: "iife", + outfile: "dist/monaco/index.js", + loader: { + ".ttf": "file" + } + }), + esbuild.build({ ...commonOptions, outfile: "dist/browser.js", footer: { js: "//# sourceURL=VencordWeb" }, }), esbuild.build({ ...commonOptions, + outfile: "dist/extension.js", + define: { + ...commonOptions?.define, + IS_EXTENSION: "true", + }, + footer: { js: "//# sourceURL=VencordWeb" }, + }), + esbuild.build({ + ...commonOptions, inject: ["browser/GMPolyfill.js", ...(commonOptions?.inject || [])], define: { - "window": "unsafeWindow", - ...(commonOptions?.define) + ...(commonOptions?.define), + window: "unsafeWindow", }, outfile: "dist/Vencord.user.js", banner: { @@ -79,12 +111,39 @@ await Promise.all( ); /** - * @type {(target: string, files: string[], shouldZip: boolean) => Promise<void>} + * @type {(dir: string) => Promise<string[]>} */ -async function buildPluginZip(target, files, shouldZip) { +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; +} + +/** + * @type {(dir: string, basePath?: string) => Promise<Record<string, string>>} + */ +async function loadDir(dir, basePath = "") { + const files = await globDir(dir); + return Object.fromEntries(await Promise.all(files.map(async f => [f.slice(basePath.length), await readFile(f)]))); +} + +/** + * @type {(target: string, files: string[]) => Promise<void>} + */ +async function buildExtension(target, files) { const entries = { - "dist/Vencord.js": await readFile("dist/browser.js"), - "dist/Vencord.css": await readFile("dist/browser.css"), + "dist/Vencord.js": await readFile("dist/extension.js"), + "dist/Vencord.css": await readFile("dist/extension.css"), + ...await loadDir("dist/monaco"), + ...await loadDir("browser/third-party", "browser/"), ...Object.fromEntries(await Promise.all(files.map(async f => { let content = await readFile(join("browser", f)); if (f.startsWith("manifest")) { @@ -100,31 +159,15 @@ async function buildPluginZip(target, files, shouldZip) { }))), }; - if (shouldZip) { - 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 { - 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 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); } const appendCssRuntime = readFile("dist/Vencord.user.css", "utf-8").then(content => { @@ -142,8 +185,9 @@ const appendCssRuntime = readFile("dist/Vencord.user.css", "utf-8").then(content await Promise.all([ appendCssRuntime, - buildPluginZip("extension.zip", ["modifyResponseHeaders.json", "content.js", "manifest.json", "icon.png"], true), - buildPluginZip("chromium-unpacked", ["modifyResponseHeaders.json", "content.js", "manifest.json", "icon.png"], false), - buildPluginZip("firefox-unpacked", ["background.js", "content.js", "manifestv2.json", "icon.png"], false), + buildExtension("chromium-unpacked", ["modifyResponseHeaders.json", "content.js", "manifest.json", "icon.png"]), + buildExtension("firefox-unpacked", ["background.js", "content.js", "manifestv2.json", "icon.png"]), ]); +Zip.sync.zip("dist/chromium-unpacked").compress().save("dist/extension.zip"); +console.info("Packed Chromium Extension written to dist/extension.zip"); |