mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-02 08:50:04 +08:00
81 lines
3.1 KiB
JavaScript
81 lines
3.1 KiB
JavaScript
// Reproducible, offline WebView engines from the desktop's existing lockfile.
|
|
// No native dependency or runtime fingerprint change is needed.
|
|
import { readFile, readdir, writeFile } from "node:fs/promises";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { build } from "esbuild";
|
|
|
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "../../..");
|
|
const pdfRoot = join(root, "node_modules/pdfjs-dist");
|
|
const modelRoot = join(root, "node_modules/@google/model-viewer");
|
|
const output = join(
|
|
root,
|
|
"apps/mobile/src/session/documentPreviewAssets.generated.ts",
|
|
);
|
|
const pdfVersion = JSON.parse(
|
|
await readFile(join(pdfRoot, "package.json"), "utf8"),
|
|
).version;
|
|
const modelVersion = JSON.parse(
|
|
await readFile(join(modelRoot, "package.json"), "utf8"),
|
|
).version;
|
|
const pdfBundle = await build({
|
|
stdin: {
|
|
contents: `import * as pdf from 'pdfjs-dist/legacy/build/pdf.mjs'; import * as worker from 'pdfjs-dist/legacy/build/pdf.worker.mjs'; globalThis.pdfjsLib = pdf; globalThis.pdfjsWorker = worker;`,
|
|
resolveDir: root,
|
|
},
|
|
bundle: true,
|
|
write: false,
|
|
format: "iife",
|
|
platform: "browser",
|
|
minify: true,
|
|
external: ["node:*"],
|
|
target: ["safari16", "chrome105"],
|
|
});
|
|
const binaryAssets = {};
|
|
for (const [kind, directory, extensions] of [
|
|
["cMapUrl", "cmaps", [".bcmap"]],
|
|
["standardFontDataUrl", "standard_fonts", [".pfb", ".ttf"]],
|
|
]) {
|
|
binaryAssets[kind] = {};
|
|
for (const name of await readdir(join(pdfRoot, directory))) {
|
|
if (!extensions.some((ext) => name.endsWith(ext))) continue;
|
|
binaryAssets[kind][name] = (
|
|
await readFile(join(pdfRoot, directory, name))
|
|
).toString("base64");
|
|
}
|
|
}
|
|
const modelBundle = await build({
|
|
entryPoints: [join(modelRoot, "dist/model-viewer.min.js")],
|
|
bundle: true,
|
|
write: false,
|
|
format: "iife",
|
|
platform: "browser",
|
|
minify: true,
|
|
target: ["safari16", "chrome105"],
|
|
});
|
|
const modelJs = modelBundle.outputFiles[0].text;
|
|
const drawioJs = await readFile(
|
|
join(root, "apps/desktop/src/renderer/vendor/drawio/viewer-static.min.js"),
|
|
"utf8",
|
|
);
|
|
const pdfCss = await readFile(join(pdfRoot, "web/pdf_viewer.css"), "utf8");
|
|
await writeFile(
|
|
output,
|
|
[
|
|
"// Generated by apps/mobile/scripts/sync-document-preview-assets.mjs; do not hand edit.",
|
|
"// PDF.js (Apache-2.0) and model-viewer (Apache-2.0) retain their bundled license notices.",
|
|
`export const MOBILE_PDF_VERSION = ${JSON.stringify(pdfVersion)};`,
|
|
`export const MOBILE_PDF_JS = ${JSON.stringify(pdfBundle.outputFiles[0].text)};`,
|
|
`export const MOBILE_PDF_CSS = ${JSON.stringify(pdfCss)};`,
|
|
`export const MOBILE_PDF_BINARY_ASSETS = ${JSON.stringify(binaryAssets)};`,
|
|
`export const MOBILE_MODEL_VERSION = ${JSON.stringify(modelVersion)};`,
|
|
`export const MOBILE_MODEL_JS = ${JSON.stringify(modelJs)};`,
|
|
"// draw.io viewer: identical to the desktop vendor distribution, including its notices.",
|
|
`export const MOBILE_DRAWIO_JS = ${JSON.stringify(drawioJs)};`,
|
|
"",
|
|
].join("\n"),
|
|
);
|
|
console.log(
|
|
`Generated offline PDF ${pdfVersion} and model-viewer ${modelVersion} engines.`,
|
|
);
|