-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
132 lines (125 loc) · 3.86 KB
/
Copy pathrollup.config.mjs
File metadata and controls
132 lines (125 loc) · 3.86 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
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
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import { rollup } from 'rollup';
import { readFileSync } from 'node:fs';
import path from 'node:path';
const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
const banner = `/*!
* ${pkg.name} ${pkg.version} <${pkg.homepage}>
* Copyright (c) ${new Date().getFullYear()} ${pkg.author.name}
* Released under ${pkg.license} License
*/`;
const sharedPlugins = (tsOptions = {}) => [
resolve({ exportConditions: ['browser', 'module', 'import', 'default'] }),
commonjs({ include: 'node_modules/**' }),
typescript({ sourceMap: true, inlineSources: false, declaration: false, ...tsOptions }),
];
/**
* Inline Web Worker plugin.
*
* Replaces Vite's `?worker&inline` semantics for rollup: when a module imports
* `./worker?worker&inline`, the referenced file is bundled separately as an
* IIFE, wrapped in a Blob URL, and a default-exported factory is returned so
* `new Dom2pdfWorker()` works exactly as before.
*/
function inlineWorker() {
return {
name: 'inline-worker',
resolveId(source, importer) {
if (!source.includes('?worker&inline')) return null;
if (!importer) return source;
const workerFile = source.replace(/\?worker&inline.*$/, '');
const resolved = path.resolve(path.dirname(importer), workerFile);
const withExtension = path.extname(resolved) ? resolved : `${resolved}.ts`;
return `${withExtension}?worker&inline`;
},
async load(id) {
if (!id.includes('?worker&inline')) return null;
const workerFile = id.replace(/\?worker&inline.*$/, '');
const bundle = await rollup({
input: workerFile,
plugins: [
resolve({ exportConditions: ['browser', 'module', 'import', 'default'] }),
commonjs({ include: 'node_modules/**' }),
typescript({ sourceMap: false, inlineSources: false, declaration: false }),
],
});
const { output } = await bundle.generate({ format: 'iife' });
await bundle.close();
const workerCode = output[0].code;
const blob = JSON.stringify(workerCode);
return {
code: `var src = ${blob};
var blob = new Blob([src], { type: 'application/javascript' });
var url = URL.createObjectURL(blob);
function Dom2pdfWorker() { return new Worker(url); }
export default Dom2pdfWorker;`,
moduleSideEffects: true,
};
},
};
}
// UMD build: single-file, inline dynamic imports (UMD has no code-splitting)
const umdConfig = {
input: 'src/index.ts',
output: [
{
file: pkg.main,
name: 'dompdf',
format: 'umd',
exports: 'named',
banner,
sourcemap: true,
inlineDynamicImports: true,
},
{
file: 'dist/dompdf.min.js',
name: 'dompdf',
format: 'umd',
exports: 'named',
banner,
sourcemap: true,
inlineDynamicImports: true,
plugins: [
terser({
compress: { drop_console: true, passes: 2 },
format: { comments: /^!/ },
}),
],
},
],
plugins: [...sharedPlugins(), inlineWorker()],
};
// ESM build: code-splitting allowed
const esmConfig = {
input: 'src/index.ts',
output: [
{
dir: 'dist/esm',
format: 'esm',
entryFileNames: 'dompdf.esm.js',
banner,
sourcemap: true,
},
{
dir: 'dist/esm',
format: 'esm',
entryFileNames: 'dompdf.esm.min.js',
banner,
sourcemap: true,
plugins: [
terser({
compress: { drop_console: true, passes: 2 },
format: { comments: /^!/ },
}),
],
},
],
plugins: [
...sharedPlugins({ outDir: 'dist/esm', declaration: false, declarationDir: undefined }),
inlineWorker(),
],
};
export default [umdConfig, esmConfig];