mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-03 23:51:19 +08:00
fix(reading): package PDF.js decoder assets (#1305)
Co-authored-by: XZH <cto@xzh.ai>
This commit is contained in:
@@ -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
|
||||
|
||||
34
web/scripts/copy-pdfjs-assets.mjs
Normal file
34
web/scripts/copy-pdfjs-assets.mjs
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user