diff --git a/build-log.txt b/build-log.txt deleted file mode 100644 index 3e0cfb75..00000000 Binary files a/build-log.txt and /dev/null differ 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,