fix(reading): package PDF.js decoder assets (#1305)

Co-authored-by: XZH <cto@xzh.ai>
This commit is contained in:
evan188199-tech
2026-09-10 23:41:36 +08:00
committed by GitHub
parent d88ccee585
commit 791c8d28db
10 changed files with 97 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { configureTypeIncludes } from "./next-type-includes.mjs";
import { copyPdfjsAssets } from "./copy-pdfjs-assets.mjs";
const webRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
@@ -66,6 +67,7 @@ if (isEntry) {
const buildTsconfigPath = prepareBuildTsconfig(snapshots, distDir);
let result;
try {
copyPdfjsAssets();
result = spawnSync(
process.execPath,
// Next.js 16 defaults to Turbopack, which does not emit the standalone

View File

@@ -0,0 +1,34 @@
import { cpSync, existsSync, mkdirSync, rmSync } from "node:fs";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const webRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"..",
);
const sourceDir = path.join(webRoot, "node_modules", "pdfjs-dist", "wasm");
const targetDir = path.join(webRoot, "public", "pdfjs", "wasm");
/**
* Copy PDF.js decoder resources into Next's public asset tree.
*
* PDF.js keeps the OpenJPEG/JBIG2/QCMS WebAssembly modules outside its JS
* bundles and resolves them at runtime from `wasmUrl`. Next's standalone
* tracing does not include those package files automatically, so released
* builds otherwise render PDFs that use JPEG2000 as blank pages.
*/
export function copyPdfjsAssets() {
if (!existsSync(sourceDir)) {
throw new Error(
`Missing PDF.js decoder assets at ${sourceDir}. Run npm install first.`,
);
}
rmSync(targetDir, { recursive: true, force: true });
mkdirSync(targetDir, { recursive: true });
cpSync(sourceDir, targetDir, { recursive: true });
}
if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
copyPdfjsAssets();
}