build: ship each asset once and fix ineffective dynamic imports

This commit is contained in:
Jones Jankovic committed 2026-11-02 18:15:09 +10:30
1 parent 27269ac943
commit 7f42b50893
9 files changed
+15 -88

No files matched your search

-53
View File
@@ -1,53 +0,0 @@
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
View File
@@ -18,6 +18,7 @@ export default function InlineWorkerDevPlugin(): Plugin {
format: "iife",
target: "esnext",
minify: true,
conditions: ["onnxruntime-web-use-extern-wasm"],
alias: {
"@huggingface/transformers": resolve(
"node_modules/@huggingface/transformers/dist/transformers.web.js",
+6 -17
View File
@@ -11,7 +11,7 @@
* or `node lib/publish.js --b firefox`
*/
import { globSync } from "glob";
import { globSync } from "node:fs";
import semver from "semver";
import { execSync } from "node:child_process";
@@ -20,13 +20,8 @@ function getLatestVersion(files) {
const versions = files
.map((file) => {
const match = file.match(/@([\d\.]+)-/);
console.log(
"Matching file:",
file,
"Version found:",
match ? match[1] : "None",
);
const match = file.match(/@([\d.]+)-/);
console.log("Matching file:", file, "Version found:", match ? match[1] : "None");
if (!match) return null;
@@ -61,9 +56,7 @@ function getLatestVersion(files) {
}
// Get the original full version string that matches the identified latest SemVer version
const latestVersionData = versions.find(
(v) => v.semverVersion === latestSemver,
);
const latestVersionData = versions.find((v) => v.semverVersion === latestSemver);
const latestFullVersion = latestVersionData ? latestVersionData.fullVersion : null;
console.log("Final selected latest version:", latestFullVersion);
@@ -121,12 +114,8 @@ function zipSources() {
}
function runPublishCommand(browsers) {
const chromeZip = browsers.includes("chrome")
? getLatestFiles("chrome")
: null;
const firefoxZip = browsers.includes("firefox")
? getLatestFiles("firefox")
: null;
const chromeZip = browsers.includes("chrome") ? getLatestFiles("chrome") : null;
const firefoxZip = browsers.includes("firefox") ? getLatestFiles("firefox") : null;
// Sources are typically only needed for Firefox submissions
const firefoxSourcesZip = browsers.includes("firefox") ? zipSources() : null;