-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrollup.config.mjs
169 lines (159 loc) · 4.13 KB
/
rollup.config.mjs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import crypto from "node:crypto";
import fs from "node:fs";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import rollupReplace from "@rollup/plugin-replace";
import typescript from "@rollup/plugin-typescript";
import copyPlugin from "rollup-plugin-copy";
import deletePlugin from "rollup-plugin-delete";
const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8"));
const DIST_FOLDER = "dist";
const EXTERNAL_NODE_PACKAGES = ["fs", "path", "crypto"];
const IS_PROD = process.env.NODE_ENV === "production";
const wasmFile = fs.readFileSync("src/vendor/pdfium.wasm");
function sha256Base64(buffer) {
return crypto.createHash("sha256").update(buffer).digest("base64");
}
function patchEsmPath() {
// replace string like this "pdfium.esm.wasm" with "pdfium.wasm"
return {
name: "patch-esm-path",
renderChunk(code) {
return code.replace(/pdfium\.esm\.wasm/g, "pdfium.wasm");
},
};
}
function injectDebugLog({ message }) {
if (IS_PROD) {
return null;
}
return {
name: "inject-console-log",
renderChunk(code) {
return `${code}\nconsole.log("${message}");`;
},
};
}
async function injectCdnVars() {
if (IS_PROD) {
const wasmSHA256B64 = sha256Base64(wasmFile);
return rollupReplace({
preventAssignment: true,
values: {
// For properly fetching the wasm file from CDN
__PACKAGE_VERSION__: JSON.stringify(pkg.version),
__WASM_SHA265_B64__: JSON.stringify(wasmSHA256B64),
},
});
}
// For dev environent we don't care about integrity
return rollupReplace({
preventAssignment: true,
values: {
__PACKAGE_VERSION__: JSON.stringify(pkg.version),
__WASM_SHA265_B64__: JSON.stringify(null),
},
});
}
function setNotNodeEnvironment() {
return rollupReplace({
preventAssignment: true,
values: {
// This is helpefull for properly tree-shaking the node.js code
ENVIRONMENT_IS_NODE: "false",
},
});
}
export default [
// CommonJS build
{
input: "src/index.cjs.ts",
output: {
name: "@hyzyla/pdfium",
file: `${DIST_FOLDER}/index.cjs`,
format: "cjs",
exports: "named",
},
plugins: [
deletePlugin({ targets: `${DIST_FOLDER}/*` }),
copyPlugin({
targets: [{ src: "src/vendor/pdfium.wasm", dest: DIST_FOLDER }],
}),
injectDebugLog({ message: "PDFium CJS loaded" }),
resolve(),
typescript(),
commonjs({
strictRequires: true,
ignore: EXTERNAL_NODE_PACKAGES,
}),
],
external: EXTERNAL_NODE_PACKAGES,
},
// ES module build
{
input: "src/index.esm.ts",
output: {
name: "@hyzyla/pdfium",
file: `${DIST_FOLDER}/index.esm.js`,
format: "es",
exports: "named",
},
plugins: [injectDebugLog({ message: "PDFium ESM loaded" }), patchEsmPath(), resolve(), typescript()],
},
// ES (browser) build
{
input: "src/index.esm.ts",
output: {
name: "@hyzyla/pdfium",
file: `${DIST_FOLDER}/index.esm.browser.js`,
format: "es",
exports: "named",
},
plugins: [
injectDebugLog({ message: "PDFium ESM browser loaded" }),
patchEsmPath(),
setNotNodeEnvironment(),
resolve(),
typescript(),
],
},
// ES (browser) + CDN build
{
input: "src/index.esm.cdn.ts",
output: {
name: "PDFium",
file: `${DIST_FOLDER}/index.esm.cdn.js`,
format: "es",
},
plugins: [
injectDebugLog({ message: "PDFium with CDN loaded" }),
patchEsmPath(),
setNotNodeEnvironment(),
await injectCdnVars(),
resolve(),
typescript(),
],
},
// ES (browser) + Base64 build
{
input: "src/index.esm.base64.ts",
output: {
name: "PDFium",
dir: DIST_FOLDER,
format: "es",
},
plugins: [
injectDebugLog({ message: "PDFium with Base64 loaded" }),
patchEsmPath(),
setNotNodeEnvironment(),
rollupReplace({
preventAssignment: true,
values: {
__WASM_BINARY_BASE64__: JSON.stringify(wasmFile.toString("base64")),
},
}),
resolve(),
typescript(),
],
},
];