mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-12-06 19:56:45 +10:30
perf: reduce extension package size
This commit is contained in:
1 parent
72cce2cc55
commit
a760a152dc
12 files changed
+101
-72
No files matched your search
Binary file not shown.
@@ -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");
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -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<string, string>();
|
||||
const duplicates = new Map<string, string>();
|
||||
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`);
|
||||
},
|
||||
});
|
||||
@@ -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"],
|
||||
});
|
||||
|
||||
|
||||
+2
-2
@@ -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": {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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"),
|
||||
);
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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`;
|
||||
|
||||
@@ -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";
|
||||
|
||||
|
||||
@@ -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/",
|
||||
|
||||
+12
-2
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user