-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrollup.config.ts
More file actions
64 lines (61 loc) · 2.07 KB
/
rollup.config.ts
File metadata and controls
64 lines (61 loc) · 2.07 KB
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
// This rollup config builds a PDF.js bundle for serverless environments
import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import { defineConfig } from 'rollup'
import esbuild from 'rollup-plugin-esbuild'
import { pdfjsTypes } from './src/rollup/plugins'
const canvasMock = `
new Proxy({}, {
get(target, prop) {
return () => {
throw new Error("@napi-rs/canvas is not available in this environment")
}
},
})
`
.replaceAll('\n', '')
.trim()
export default defineConfig({
input: 'src/index.mjs',
output: {
file: 'dist/index.mjs',
format: 'esm',
exports: 'auto',
inlineDynamicImports: true,
generatedCode: {
constBindings: true,
},
sourcemap: false,
},
plugins: [
replace({
delimiters: ['', ''],
preventAssignment: true,
values: {
// Mimick Node.js environment.
'const isNodeJS = typeof': 'const isNodeJS = typeof window === "undefined" // typeof',
// Force inlining the PDF.js worker.
'await import(\n /*webpackIgnore: true*/\n /*@vite-ignore*/\n this.workerSrc)': '__pdfjsWorker__',
// Force setting up fake PDF.js worker.
'#isWorkerDisabled = false': '#isWorkerDisabled = true',
// Remove WASM code from the worker.
'wasmExports = await createWasm': 'wasmExports = {}',
'if (!this.#modulePromise)': 'if (false)',
'#instantiateWasm(fallbackCallback, imports, successCallback) {': '#instantiateWasm(fallbackCallback, imports, successCallback) { return;',
'#getJsModule(fallbackCallback) {': '#getJsModule(fallbackCallback) { return;',
// Mock the `@napi-rs/canvas` module import from the unused `NodeCanvasFactory` class.
'require("@napi-rs/canvas")': canvasMock,
// Remove the legacy build warning.
'warn("Please use the `legacy` build in Node.js environments.")': '',
},
}),
nodeResolve(),
pdfjsTypes(),
esbuild({
target: 'es2021',
minify: true,
keepNames: true,
legalComments: 'none',
}),
],
})