-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
71 lines (65 loc) · 2.24 KB
/
vite.config.ts
File metadata and controls
71 lines (65 loc) · 2.24 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
import { defineConfig } from 'vite';
import { copyFileSync, existsSync, mkdirSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* Single entry per build (Vite 5 does not accept config array; Rollup does not allow IIFE + code-splitting).
* Run twice: vite build (pdf-signable) and VITE_ENTRY=acroform-editor vite build.
* CSS is built separately via `pnpm run build:css` (Sass).
* The pdf-signable build copies the PDF.js worker from node_modules to outDir so it is served next to pdf-signable.js (default asset).
*/
const entry = process.env.VITE_ENTRY || 'pdf-signable';
const isPdf = entry === 'pdf-signable';
const WORKER_CANDIDATES = [
'pdf.worker.min.mjs',
'pdf.worker.mjs',
'pdf.worker.min.js',
'pdf.worker.js',
];
const WORKER_OUT_NAME = 'pdf.worker.min.js';
function copyPdfWorker(outDir: string): void {
const buildDir = join(__dirname, 'node_modules', 'pdfjs-dist', 'build');
const outFile = join(outDir, WORKER_OUT_NAME);
for (const name of WORKER_CANDIDATES) {
const src = join(buildDir, name);
if (existsSync(src)) {
mkdirSync(outDir, { recursive: true });
copyFileSync(src, outFile);
console.log('[vite] Copied PDF.js worker:', name, '->', outFile);
return;
}
}
console.warn('[vite] PDF.js worker not found in', buildDir, '- run pnpm install');
}
export default defineConfig({
define: {
__PDF_SIGNABLE_BUILD_TIME__: JSON.stringify(new Date().toISOString()),
},
build: {
outDir: 'src/Resources/public/js',
emptyOutDir: isPdf,
rollupOptions: {
input: isPdf
? { 'pdf-signable': 'src/Resources/assets/signable-editor.ts' }
: { 'acroform-editor': 'src/Resources/assets/acroform-editor.ts' },
output: {
format: 'iife',
...(isPdf ? { inlineDynamicImports: true } : {}),
entryFileNames: '[name].js',
assetFileNames: '[name][extname]',
},
},
},
plugins: [
isPdf
? {
name: 'copy-pdf-worker',
closeBundle() {
const outDir = join(__dirname, 'src', 'Resources', 'public', 'js');
copyPdfWorker(outDir);
},
}
: undefined,
].filter(Boolean),
});