forked from unjs/unpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfjs.rollup.config.ts
78 lines (75 loc) · 2.42 KB
/
pdfjs.rollup.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// This rollup config is used to build PDF.js for serverless environments
import { fileURLToPath } from "node:url";
import { join } from "node:path";
import { defineConfig } from "rollup";
import alias from "@rollup/plugin-alias";
import replace from "@rollup/plugin-replace";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import inject from "@rollup/plugin-inject";
import terser from "@rollup/plugin-terser";
import * as unenv from "unenv";
import { resolveAliases } from "./src/pdfjs-serverless/rollup/utils";
import { pdfjsTypes } from "./src/pdfjs-serverless/rollup/plugins";
const mockDir = fileURLToPath(
new URL("src/pdfjs-serverless/mocks", import.meta.url),
);
const env = unenv.env(unenv.nodeless);
export default defineConfig({
// @ts-expect-error: Rollup 4 type compatibility not yet available
input: "src/pdfjs-serverless/index.mjs",
output: {
file: "dist/pdfjs.mjs",
format: "esm",
exports: "auto",
inlineDynamicImports: true,
generatedCode: {
constBindings: true,
},
sourcemap: false,
},
external: env.external,
plugins: [
replace({
delimiters: ["", ""],
preventAssignment: true,
values: {
// Disable the `window` check (for requestAnimationFrame).
"typeof window": '"undefined"',
// Imitate the Node.js environment for all serverless environments, unenv will
// take care of the remaining Node.js polyfills. Keep support for browsers.
"const isNodeJS = typeof":
'const isNodeJS = typeof document === "undefined" // typeof',
// Force inlining the PDF.js worker.
"await import(/* webpackIgnore: true */ this.workerSrc)":
"__pdfjsWorker__",
// Tree-shake client worker initialization logic.
"!PDFWorkerUtil.isWorkerDisabled && !PDFWorker.#mainThreadWorkerMessageHandler":
"false",
},
}),
alias({
entries: resolveAliases({
canvas: join(mockDir, "canvas.mjs"),
"path2d-polyfill": join(mockDir, "path2d-polyfill.mjs"),
...env.alias,
}),
}),
nodeResolve(),
commonjs({
esmExternals: (id) => !id.startsWith("unenv/"),
requireReturnsDefault: "auto",
}),
inject(env.inject),
pdfjsTypes(),
terser({
mangle: {
keep_fnames: true,
keep_classnames: true,
},
format: {
comments: false,
},
}),
],
});