From a760a152dc68667242aecf1e524fabf62af2af57 Mon Sep 17 00:00:00 2001 From: SethBurkart123 Date: Wed, 2 Sep 2026 11:37:47 +1000 Subject: [PATCH] perf: reduce extension package size --- build-log.txt | Bin 37094 -> 0 bytes lib/closePlugin.ts | 20 ----------- lib/dedupePublicAssets.ts | 53 +++++++++++++++++++++++++++++ lib/inlineWorker.ts | 7 ++++ package.json | 4 +-- scripts/copy-ort-wasm-assets.mjs | 28 --------------- scripts/copy-pdfjs-assets.mjs | 17 --------- scripts/copy-runtime-assets.mjs | 24 +++++++++++++ scripts/package-extension-zips.mjs | 2 +- src/lib/pdfjsExtension.ts | 2 +- src/manifests/manifest.json | 2 +- vite.config.ts | 14 ++++++-- 12 files changed, 101 insertions(+), 72 deletions(-) delete mode 100644 build-log.txt delete mode 100644 lib/closePlugin.ts create mode 100644 lib/dedupePublicAssets.ts delete mode 100644 scripts/copy-ort-wasm-assets.mjs delete mode 100644 scripts/copy-pdfjs-assets.mjs create mode 100644 scripts/copy-runtime-assets.mjs diff --git a/build-log.txt b/build-log.txt deleted file mode 100644 index 3e0cfb75494c2f311676d2765851bbd7f7e664cf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 37094 zcmeI5`EMJ?7037I0{tHdwTK)fB6azQ>LiwBxiMPDj%7CmY{!u(N}{c+Bz3r{`=_`4 ze&%sBv&$tZxssM#3IxgJ9y9Nncg>sOfB*CQ^qLi?c7Aj6k-~hcH=ra#a`Zg%q?rC6J*BidH zbOzeeCUCZOZ$no%lkXi?%kJ4-cdg;*QW2&*x})WOf%#W@_6xnIk`@``BJUE zPTnS8>-&ZJf%ZVVs&S0`BkQIQvUojq9Aaerq$6ZKz`CmT(QYkai_95b(81(zMc4N9 zLZ&y>m%rQk;B!a!cGP=Y&(`#cbwgFpG$egBeR2hj3(W7-PImlm>g=w1W8CiQ_0h@u zg7gwF(bIx-6UV%eI7SgIZs_}4z5f{Xa9`4omIrF2`d@l;#vnlBPsu9W>b8r93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i=Buvu?56QtSNzI5Je=bA(hr+wg3rfN*mD1S zS>wd=CD?SEWg9OHepbyeb3tv5b#^2geO|z$IL>7!8FrP4{}H=r+ul!(wgfxtP5rJ* zwzkVhDx(T@mg1u$BeBQFzd4a9AaC_TR1WJR;+5h>(>N~z%+j|vm-C{lW$?d?)h~W$ z2Xdq2m3&sF=-nK$E8G2SP54KzkVQg+huslYoS)$5!;7YGo_ui5;@_&GfMR#e$b%0b zY+rI|vF@6)DD3=kB55rO-s0A6oA-PqdqkEZKR;2``KtC5wSQ&gpWxVu0e_W$NQ07l v;xo^)^XS`Xw_A3F+jCyWu6EI9f4glr295rk+Gm^nTA8&vQY*CZ$$kD0`sd`M diff --git a/lib/closePlugin.ts b/lib/closePlugin.ts deleted file mode 100644 index 9494f298..00000000 --- a/lib/closePlugin.ts +++ /dev/null @@ -1,20 +0,0 @@ -// ref: https://stackoverflow.com/a/76920975 -import type { Plugin } from "vite"; - -/** Exit with code 1 on build failure; do not exit on success (multi-target builds). */ -export default function ClosePlugin(): Plugin { - return { - name: "ClosePlugin", - buildEnd(error) { - if (error) { - console.error("Error bundling", error); - process.exit(1); - } else { - console.log("Build ended"); - } - }, - closeBundle() { - console.log("Bundle closed"); - }, - }; -} diff --git a/lib/dedupePublicAssets.ts b/lib/dedupePublicAssets.ts new file mode 100644 index 00000000..d3c5c9dd --- /dev/null +++ b/lib/dedupePublicAssets.ts @@ -0,0 +1,53 @@ +import { createHash } from "node:crypto"; +import { readFileSync, unlinkSync, writeFileSync } from "node:fs"; +import { resolve } from "node:path"; +import { dirname, relative } from "node:path/posix"; +import { globSync } from "glob"; +import type { Plugin } from "rollup"; + +const hash = (path: string) => + createHash("sha256").update(readFileSync(path)).digest("hex"); + +/** Reuse identical files already copied to stable extension paths. */ +export default (): Plugin => ({ + name: "dedupe-public-assets", + writeBundle({ dir }) { + if (!dir) return; + const files = globSync("**/*", { cwd: dir, nodir: true }); + const stable = new Map(); + const duplicates = new Map(); + for (const file of files) { + if (file.startsWith("resources/")) + stable.set(hash(resolve(dir, file)), file); + } + for (const file of files) { + const match = file.startsWith("assets/") + ? stable.get(hash(resolve(dir, file))) + : undefined; + if (match) duplicates.set(file, match); + } + for (const file of files.filter((file) => + /\.(?:css|html|js)$/.test(file), + )) { + const path = resolve(dir, file); + let output = readFileSync(path, "utf8"); + for (const [asset, canonical] of duplicates) { + output = output.replaceAll( + relative(dirname(file), asset), + relative(dirname(file), canonical), + ); + } + writeFileSync(path, output); + } + for (const file of duplicates.keys()) unlinkSync(resolve(dir, file)); + + const manifestPath = resolve(dir, "manifest.json"); + const manifest = JSON.parse(readFileSync(manifestPath, "utf8")); + for (const entry of manifest.web_accessible_resources ?? []) { + entry.resources = entry.resources.filter( + (file: string) => !duplicates.has(file), + ); + } + writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`); + }, +}); diff --git a/lib/inlineWorker.ts b/lib/inlineWorker.ts index cfd846b5..91c9d96b 100644 --- a/lib/inlineWorker.ts +++ b/lib/inlineWorker.ts @@ -1,5 +1,6 @@ import { Plugin } from "vite"; import { build } from "esbuild"; +import { resolve } from "node:path"; /** Bundle worker entry points imported with `?inlineWorker` as Blob-backed Workers in dev. */ export default function InlineWorkerDevPlugin(): Plugin { @@ -16,6 +17,12 @@ export default function InlineWorkerDevPlugin(): Plugin { platform: "browser", format: "iife", target: "esnext", + minify: true, + alias: { + "@huggingface/transformers": resolve( + "node_modules/@huggingface/transformers/dist/transformers.web.js", + ), + }, external: ["webextension-polyfill"], }); diff --git a/package.json b/package.json index 4bf8cc04..ab671dd7 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "browserslist": "> 0.5%, last 2 versions, not dead", "scripts": { "compile:layerchart": "node scripts/compile-layerchart-vendor.mjs", - "postinstall": "node scripts/copy-pdfjs-assets.mjs && node scripts/copy-ort-wasm-assets.mjs && npm run compile:layerchart", + "postinstall": "node scripts/copy-runtime-assets.mjs && npm run compile:layerchart", "autoaudit": "npm audit && npm audit fix && npm run build", "dev": "cross-env MODE=chrome vite dev", "dev:firefox": "cross-env MODE=firefox vite build --watch", @@ -25,7 +25,7 @@ "test:ci": "npm run test:unit && npm run build && npm run test:smoke", "release": "gh release create $npm_package_version --repo BetterSEQTA/BetterSEQTA-Plus ./dist/*.zip --generate-notes", "publish": "bun lib/publish.js --b", - "zip": "bedframe zip" + "zip": "node scripts/package-extension-zips.mjs" }, "keywords": [], "author": { diff --git a/scripts/copy-ort-wasm-assets.mjs b/scripts/copy-ort-wasm-assets.mjs deleted file mode 100644 index 9fc172b9..00000000 --- a/scripts/copy-ort-wasm-assets.mjs +++ /dev/null @@ -1,28 +0,0 @@ -import { copyFileSync, existsSync, mkdirSync } from "node:fs"; -import { createRequire } from "node:module"; -import { dirname, join } from "node:path"; -import { fileURLToPath } from "node:url"; - -const root = join(dirname(fileURLToPath(import.meta.url)), ".."); -const require = createRequire(import.meta.url); - -const transformersDist = dirname(require.resolve("@huggingface/transformers")); -const outDir = join(root, "src", "public", "resources", "ort"); - -mkdirSync(outDir, { recursive: true }); - -const ortFiles = [ - "ort-wasm-simd-threaded.jsep.mjs", - "ort-wasm-simd-threaded.jsep.wasm", -]; - -for (const file of ortFiles) { - const src = join(transformersDist, file); - if (!existsSync(src)) { - throw new Error( - `Missing ONNX Runtime WASM asset: ${src}\n` + - "Ensure @huggingface/transformers is installed (direct dependency).", - ); - } - copyFileSync(src, join(outDir, file)); -} diff --git a/scripts/copy-pdfjs-assets.mjs b/scripts/copy-pdfjs-assets.mjs deleted file mode 100644 index 2ed5cc77..00000000 --- a/scripts/copy-pdfjs-assets.mjs +++ /dev/null @@ -1,17 +0,0 @@ -import { copyFileSync, mkdirSync } from "node:fs"; -import { dirname, join } from "node:path"; -import { fileURLToPath } from "node:url"; - -const root = join(dirname(fileURLToPath(import.meta.url)), ".."); -const pdfjsRoot = join(root, "node_modules", "pdfjs-dist"); -const outDir = join(root, "src", "public", "resources", "pdfjs"); - -mkdirSync(outDir, { recursive: true }); -copyFileSync( - join(pdfjsRoot, "build", "pdf.worker.min.mjs"), - join(outDir, "pdf.worker.min.mjs"), -); -copyFileSync( - join(pdfjsRoot, "legacy", "build", "pdf.min.mjs"), - join(outDir, "pdf.legacy.min.mjs"), -); diff --git a/scripts/copy-runtime-assets.mjs b/scripts/copy-runtime-assets.mjs new file mode 100644 index 00000000..bcd5c262 --- /dev/null +++ b/scripts/copy-runtime-assets.mjs @@ -0,0 +1,24 @@ +import { copyFileSync, mkdirSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const resources = join(root, "src", "public", "resources"); +const files = [ + ["pdfjs-dist/build/pdf.worker.min.mjs", "pdfjs/pdf.worker.min.mjs"], + ["pdfjs-dist/legacy/build/pdf.min.mjs", "pdfjs/pdf.legacy.min.mjs"], + [ + "@huggingface/transformers/dist/ort-wasm-simd-threaded.jsep.mjs", + "ort/ort-wasm-simd-threaded.jsep.mjs", + ], + [ + "@huggingface/transformers/dist/ort-wasm-simd-threaded.jsep.wasm", + "ort/ort-wasm-simd-threaded.jsep.wasm", + ], +]; + +for (const [source, target] of files) { + const output = join(resources, target); + mkdirSync(dirname(output), { recursive: true }); + copyFileSync(join(root, "node_modules", source), output); +} diff --git a/scripts/package-extension-zips.mjs b/scripts/package-extension-zips.mjs index 015d96fc..1a2f70ab 100644 --- a/scripts/package-extension-zips.mjs +++ b/scripts/package-extension-zips.mjs @@ -42,7 +42,7 @@ const buildLabel = process.env.BUILD_LABEL || ""; const base = updateChannel === "nightly" && buildLabel ? `betterseqtaplus-nightly-${buildLabel}` - : `betterseqtaplus-${version}`; + : `${pkg.name}@${version}`; const chromeZip = `dist/${base}-chrome.zip`; const firefoxZip = `dist/${base}-firefox.zip`; diff --git a/src/lib/pdfjsExtension.ts b/src/lib/pdfjsExtension.ts index 77646be2..ab658d08 100644 --- a/src/lib/pdfjsExtension.ts +++ b/src/lib/pdfjsExtension.ts @@ -1,7 +1,7 @@ import * as pdfjs from "pdfjs-dist"; import browser from "webextension-polyfill"; -/** Static copies in `src/public` (see `scripts/copy-pdfjs-assets.mjs`, manifest web_accessible_resources). */ +/** Static copies in `src/public` (see `scripts/copy-runtime-assets.mjs`, manifest web_accessible_resources). */ const PDF_WORKER_RESOURCE = "resources/pdfjs/pdf.worker.min.mjs"; const PDF_LEGACY_RESOURCE = "resources/pdfjs/pdf.legacy.min.mjs"; diff --git a/src/manifests/manifest.json b/src/manifests/manifest.json index 095e37fb..77da051f 100644 --- a/src/manifests/manifest.json +++ b/src/manifests/manifest.json @@ -15,7 +15,7 @@ "64": "resources/icons/icon-64.png" } }, - "permissions": ["tabs", "notifications", "storage", "identity", "alarms"], + "permissions": ["tabs", "notifications", "storage", "alarms"], "host_permissions": [ "https://newsapi.org/", "https://betterseqta.org/", diff --git a/vite.config.ts b/vite.config.ts index 2f49b5d0..b8ca5423 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -5,11 +5,11 @@ import touchGlobalCSSPlugin from "./lib/touchGlobalCSS"; import InlineWorkerPlugin from "./lib/inlineWorker"; import { base64Loader } from "./lib/base64loader"; import type { BuildTarget, Manifest } from "./lib/types"; -import ClosePlugin from "./lib/closePlugin"; import fixCrxWorkerLiveReload from "./lib/fixCrxWorkerLiveReload"; import stabilizeCrxDevHmr from "./lib/stabilizeCrxDevHmr"; import { firefoxStripFunctionProbe } from "./lib/firefoxStripFunctionProbe"; import { extensionChunkUrls } from "./lib/extensionChunkUrls"; +import dedupePublicAssets from "./lib/dedupePublicAssets"; import { svelte } from "@sveltejs/vite-plugin-svelte"; @@ -96,13 +96,23 @@ export default defineConfig(({ command, mode: viteMode }) => { fixCrxWorkerLiveReload(), stabilizeCrxDevHmr(), touchGlobalCSSPlugin(), - ...(command === "build" ? [ClosePlugin(), firefoxStripFunctionProbe()] : []), + ...(command === "build" + ? [ + dedupePublicAssets(), + firefoxStripFunctionProbe(), + ] + : []), ], root: resolve(__dirname, "./src"), resolve: { alias: { "@": resolve(__dirname, "./src"), }, + dedupe: [ + "@huggingface/transformers", + "onnxruntime-common", + "onnxruntime-web", + ], }, server: { port: DEV_SERVER_PORT,