mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-08-28 09:11:06 +00:00
refactor: trim PR debloat and fix transformers build
Extract shared helpers for home notices, timetable subtitles, and theme images; dedupe global search, Select, and build scripts while preserving behaviour. Add @huggingface/transformers as a direct dependency and resolve ORT WASM paths via require.resolve so pnpm postinstall and Vite can bundle vector search. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,15 +1,8 @@
|
||||
/**
|
||||
* layerchart ships raw `.svelte` sources in `dist/`. Vite/Svelte compilation is
|
||||
* unreliable for this package on CI (Rollup parses vendor sources as JS). Compile
|
||||
* to plain `.js` at install/build time and rewrite internal imports.
|
||||
* Pre-compile layerchart `.svelte` sources to `.js` so Rollup/Vite CI builds succeed.
|
||||
*/
|
||||
import { compile } from "svelte/compiler";
|
||||
import {
|
||||
readFileSync,
|
||||
readdirSync,
|
||||
statSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
@@ -17,97 +10,71 @@ const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const layerchartRoot = join(root, "node_modules", "layerchart");
|
||||
const layerchartDist = join(layerchartRoot, "dist");
|
||||
const stampPath = join(layerchartDist, ".bsplus-compiled");
|
||||
const COMPILE_ALGO_VERSION = "2";
|
||||
const importSuffixPattern = /\.svelte(?=['"])/g;
|
||||
|
||||
function exists(path) {
|
||||
const exists = (path) => {
|
||||
try {
|
||||
statSync(path);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (!exists(layerchartDist)) {
|
||||
console.log("compile-layerchart-vendor: layerchart not installed, skipping");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const COMPILE_ALGO_VERSION = "2";
|
||||
|
||||
const layerchartVersion = JSON.parse(
|
||||
readFileSync(join(layerchartRoot, "package.json"), "utf8"),
|
||||
).version;
|
||||
|
||||
const stampContent = `${layerchartVersion}\n${COMPILE_ALGO_VERSION}`;
|
||||
|
||||
if (
|
||||
exists(stampPath) &&
|
||||
readFileSync(stampPath, "utf8").trim() === stampContent
|
||||
) {
|
||||
console.log(
|
||||
`compile-layerchart-vendor: layerchart@${layerchartVersion} already compiled, skipping`,
|
||||
);
|
||||
if (exists(stampPath) && readFileSync(stampPath, "utf8").trim() === stampContent) {
|
||||
console.log(`compile-layerchart-vendor: layerchart@${layerchartVersion} already compiled, skipping`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
function walkFiles(dir, files = []) {
|
||||
const walkFiles = (dir, files = []) => {
|
||||
for (const name of readdirSync(dir)) {
|
||||
if (name === "node_modules") continue;
|
||||
const path = join(dir, name);
|
||||
if (statSync(path).isDirectory()) {
|
||||
walkFiles(path, files);
|
||||
} else {
|
||||
files.push(path);
|
||||
}
|
||||
if (statSync(path).isDirectory()) walkFiles(path, files);
|
||||
else files.push(path);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
};
|
||||
|
||||
const importSuffixPattern = /\.svelte(?=['"])/g;
|
||||
|
||||
function patchSvelteImports(content) {
|
||||
return content.replace(importSuffixPattern, ".js");
|
||||
}
|
||||
|
||||
/** Rollup CJS resolver chokes on TS optional params (`name?`) in vendor `.js`. */
|
||||
function stripRollupBreakingSyntax(code) {
|
||||
return code
|
||||
const patchSvelteImports = (content) => content.replace(importSuffixPattern, ".js");
|
||||
const stripRollupBreakingSyntax = (code) =>
|
||||
code
|
||||
.replace(/(\w+)\?(?=\s*[,)\]])/g, "$1")
|
||||
.replace(/(\w+)\?(?=\s*:)/g, "$1");
|
||||
}
|
||||
|
||||
const svelteFiles = walkFiles(layerchartDist).filter((f) => f.endsWith(".svelte"));
|
||||
|
||||
for (const sveltePath of svelteFiles) {
|
||||
const source = readFileSync(sveltePath, "utf8");
|
||||
if (!source.includes("<script")) continue;
|
||||
|
||||
const compiled = compile(source, {
|
||||
filename: sveltePath,
|
||||
generate: "client",
|
||||
css: "injected",
|
||||
});
|
||||
|
||||
const jsPath = sveltePath.replace(/\.svelte$/, ".js");
|
||||
const jsCode = stripRollupBreakingSyntax(patchSvelteImports(compiled.js.code));
|
||||
writeFileSync(jsPath, jsCode);
|
||||
writeFileSync(
|
||||
sveltePath.replace(/\.svelte$/, ".js"),
|
||||
stripRollupBreakingSyntax(patchSvelteImports(compiled.js.code)),
|
||||
);
|
||||
}
|
||||
|
||||
const patchable = walkFiles(layerchartDist).filter((f) =>
|
||||
/\.(js|svelte|ts|mjs)$/.test(f),
|
||||
);
|
||||
|
||||
for (const filePath of patchable) {
|
||||
for (const filePath of walkFiles(layerchartDist).filter((f) => /\.(js|svelte|ts|mjs)$/.test(f))) {
|
||||
const content = readFileSync(filePath, "utf8");
|
||||
if (!content.includes(".svelte")) continue;
|
||||
const patched = patchSvelteImports(content);
|
||||
if (patched !== content) {
|
||||
writeFileSync(filePath, patched);
|
||||
}
|
||||
if (patched !== content) writeFileSync(filePath, patched);
|
||||
}
|
||||
|
||||
writeFileSync(stampPath, stampContent);
|
||||
|
||||
console.log(
|
||||
`compile-layerchart-vendor: compiled ${svelteFiles.length} Svelte files`,
|
||||
);
|
||||
console.log(`compile-layerchart-vendor: compiled ${svelteFiles.length} Svelte files`);
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
import { copyFileSync, mkdirSync } from "node:fs";
|
||||
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 transformersDist = join(
|
||||
root,
|
||||
"node_modules",
|
||||
"@huggingface",
|
||||
"transformers",
|
||||
"dist",
|
||||
);
|
||||
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 });
|
||||
@@ -20,5 +17,12 @@ const ortFiles = [
|
||||
];
|
||||
|
||||
for (const file of ortFiles) {
|
||||
copyFileSync(join(transformersDist, file), join(outDir, file));
|
||||
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,15 +1,8 @@
|
||||
/**
|
||||
* Package Chrome/Firefox build folders into zip files Windows Explorer can open.
|
||||
* Git Bash `tar -a` on CI often produces zips that fail to unzip on Windows.
|
||||
* Package Chrome/Firefox build folders into Windows-friendly zip files.
|
||||
*/
|
||||
import { execFileSync } from "node:child_process";
|
||||
import {
|
||||
appendFileSync,
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
readFileSync,
|
||||
unlinkSync,
|
||||
} from "node:fs";
|
||||
import { appendFileSync, existsSync, mkdirSync, readFileSync, unlinkSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
@@ -18,29 +11,27 @@ const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
function zipDirectory(sourceRel, outRel) {
|
||||
const sourceDir = join(root, sourceRel);
|
||||
const outZip = join(root, outRel);
|
||||
|
||||
if (!existsSync(sourceDir)) {
|
||||
throw new Error(`Missing build output: ${sourceRel}`);
|
||||
}
|
||||
if (!existsSync(sourceDir)) throw new Error(`Missing build output: ${sourceRel}`);
|
||||
|
||||
mkdirSync(dirname(outZip), { recursive: true });
|
||||
if (existsSync(outZip)) {
|
||||
unlinkSync(outZip);
|
||||
}
|
||||
if (existsSync(outZip)) unlinkSync(outZip);
|
||||
|
||||
if (process.platform === "win32") {
|
||||
const ps = [
|
||||
"Add-Type -AssemblyName System.IO.Compression.FileSystem",
|
||||
`[IO.Compression.ZipFile]::CreateFromDirectory('${sourceDir.replace(/'/g, "''")}', '${outZip.replace(/'/g, "''")}')`,
|
||||
].join("; ");
|
||||
execFileSync("powershell", ["-NoProfile", "-Command", ps], {
|
||||
stdio: "inherit",
|
||||
});
|
||||
const esc = (s) => s.replace(/'/g, "''");
|
||||
execFileSync(
|
||||
"powershell",
|
||||
[
|
||||
"-NoProfile",
|
||||
"-Command",
|
||||
[
|
||||
"Add-Type -AssemblyName System.IO.Compression.FileSystem",
|
||||
`[IO.Compression.ZipFile]::CreateFromDirectory('${esc(sourceDir)}', '${esc(outZip)}')`,
|
||||
].join("; "),
|
||||
],
|
||||
{ stdio: "inherit" },
|
||||
);
|
||||
} else {
|
||||
execFileSync("zip", ["-r", "-q", outZip, "."], {
|
||||
cwd: sourceDir,
|
||||
stdio: "inherit",
|
||||
});
|
||||
execFileSync("zip", ["-r", "-q", outZip, "."], { cwd: sourceDir, stdio: "inherit" });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +39,6 @@ const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
||||
const version = process.argv[2] || pkg.version;
|
||||
const updateChannel = process.env.UPDATE_CHANNEL || "stable";
|
||||
const buildLabel = process.env.BUILD_LABEL || "";
|
||||
|
||||
const base =
|
||||
updateChannel === "nightly" && buildLabel
|
||||
? `betterseqtaplus-nightly-${buildLabel}`
|
||||
@@ -59,13 +49,8 @@ const firefoxZip = `dist/${base}-firefox.zip`;
|
||||
|
||||
zipDirectory("dist/chrome", chromeZip);
|
||||
zipDirectory("dist/firefox", firefoxZip);
|
||||
|
||||
console.log(`Packaged ${chromeZip}`);
|
||||
console.log(`Packaged ${firefoxZip}`);
|
||||
console.log(`Packaged ${chromeZip}\nPackaged ${firefoxZip}`);
|
||||
|
||||
if (process.env.GITHUB_OUTPUT) {
|
||||
appendFileSync(
|
||||
process.env.GITHUB_OUTPUT,
|
||||
`chrome_zip=${chromeZip}\nfirefox_zip=${firefoxZip}\n`,
|
||||
);
|
||||
appendFileSync(process.env.GITHUB_OUTPUT, `chrome_zip=${chromeZip}\nfirefox_zip=${firefoxZip}\n`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user