|
1 | 1 | // Copies the pdf.js worker into public/ at build/dev time so the app can serve |
2 | 2 | // it from its own origin instead of pulling it from a CDN at runtime. |
| 3 | +// |
| 4 | +// pdfjs-dist's layout has shifted across major versions, so we discover |
| 5 | +// whatever pdf.worker.* file exists rather than hardcoding one name. |
3 | 6 | const fs = require('fs'); |
4 | 7 | const path = require('path'); |
5 | 8 |
|
6 | | -const candidates = [ |
7 | | - 'pdf.worker.min.mjs', |
8 | | - 'pdf.worker.mjs', |
9 | | - 'pdf.worker.min.js', |
10 | | - 'pdf.worker.js', |
| 9 | +const root = path.join(__dirname, '..'); |
| 10 | +const searchDirs = [ |
| 11 | + path.join(root, 'node_modules', 'pdfjs-dist', 'build'), |
| 12 | + path.join(root, 'node_modules', 'pdfjs-dist', 'legacy', 'build'), |
11 | 13 | ]; |
12 | 14 |
|
13 | | -const fromDir = path.join(__dirname, '..', 'node_modules', 'pdfjs-dist', 'build'); |
14 | | -const toDir = path.join(__dirname, '..', 'public'); |
| 15 | +// Higher = better. We prefer the minified .mjs, then any .mjs, then .min.js, |
| 16 | +// then .js. Only files starting with `pdf.worker` are considered. |
| 17 | +function score(name) { |
| 18 | + if (!name.startsWith('pdf.worker')) return -1; |
| 19 | + if (name.endsWith('.min.mjs')) return 4; |
| 20 | + if (name.endsWith('.mjs')) return 3; |
| 21 | + if (name.endsWith('.min.js')) return 2; |
| 22 | + if (name.endsWith('.js')) return 1; |
| 23 | + return 0; |
| 24 | +} |
| 25 | + |
| 26 | +let best = null; |
| 27 | +for (const dir of searchDirs) { |
| 28 | + if (!fs.existsSync(dir)) continue; |
| 29 | + for (const name of fs.readdirSync(dir)) { |
| 30 | + const s = score(name); |
| 31 | + if (s <= 0) continue; |
| 32 | + if (!best || s > best.score) { |
| 33 | + best = { dir, name, score: s }; |
| 34 | + } |
| 35 | + } |
| 36 | +} |
15 | 37 |
|
16 | | -const found = candidates.find((name) => fs.existsSync(path.join(fromDir, name))); |
17 | | -if (!found) { |
18 | | - console.error('[copy-pdf-worker] no pdf.worker.* found in', fromDir); |
| 38 | +if (!best) { |
| 39 | + console.error( |
| 40 | + '[copy-pdf-worker] could not find pdf.worker.* in any of:\n ' + |
| 41 | + searchDirs.join('\n ') + |
| 42 | + '\nIs pdfjs-dist installed?' |
| 43 | + ); |
19 | 44 | process.exit(1); |
20 | 45 | } |
21 | 46 |
|
22 | | -const src = path.join(fromDir, found); |
23 | | -const dst = path.join(toDir, found); |
24 | | -fs.mkdirSync(toDir, { recursive: true }); |
| 47 | +const dstDir = path.join(root, 'public'); |
| 48 | +// Always write to a deterministic name; runtime references this exact path. |
| 49 | +const dstName = 'pdf.worker.mjs'; |
| 50 | +const src = path.join(best.dir, best.name); |
| 51 | +const dst = path.join(dstDir, dstName); |
| 52 | +fs.mkdirSync(dstDir, { recursive: true }); |
25 | 53 | fs.copyFileSync(src, dst); |
26 | | -console.log(`[copy-pdf-worker] ${src} -> ${dst}`); |
| 54 | +console.log(`[copy-pdf-worker] ${path.relative(root, src)} -> ${path.relative(root, dst)}`); |
0 commit comments