40 lines
1.6 KiB
JavaScript
40 lines
1.6 KiB
JavaScript
// Copies the pdf.js runtime assets out of node_modules into public/pdfjs/ so the
|
|
// standalone viewer (public/viewer/) can load them as plain, unbundled ES modules.
|
|
// Runs via predev/prebuild, i.e. also inside the Docker web build stage.
|
|
//
|
|
// The target layout is dictated by the URLs the ported viewer uses:
|
|
// /pdfjs/legacy/pdf.min.mjs (import + GlobalWorkerOptions.workerSrc)
|
|
// /pdfjs/wasm/ (wasmUrl — CCITT-G4/JBIG2, JPEG2000, ICC decoders)
|
|
// /pdfjs/standard_fonts/ (standardFontDataUrl)
|
|
// /pdfjs/iccs/ (iccUrl)
|
|
import { cp, mkdir, rm, stat } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const webRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
const src = path.join(webRoot, 'node_modules', 'pdfjs-dist');
|
|
const dest = path.join(webRoot, 'public', 'pdfjs');
|
|
|
|
try {
|
|
await stat(src);
|
|
} catch {
|
|
console.error(`[copy-pdfjs] ${src} not found — run npm install first.`);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Full rebuild, so a pdfjs-dist upgrade never leaves stale files behind.
|
|
await rm(dest, { recursive: true, force: true });
|
|
await mkdir(path.join(dest, 'legacy'), { recursive: true });
|
|
|
|
const files = ['pdf.min.mjs', 'pdf.worker.min.mjs'];
|
|
for (const file of files) {
|
|
await cp(path.join(src, 'legacy', 'build', file), path.join(dest, 'legacy', file));
|
|
}
|
|
|
|
const dirs = ['wasm', 'standard_fonts', 'iccs'];
|
|
for (const dir of dirs) {
|
|
await cp(path.join(src, dir), path.join(dest, dir), { recursive: true });
|
|
}
|
|
|
|
console.log(`[copy-pdfjs] ${files.join(', ')} + ${dirs.join('/, ')}/ -> public/pdfjs/`);
|