-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
98 lines (89 loc) · 3.29 KB
/
Copy pathvite.config.ts
File metadata and controls
98 lines (89 loc) · 3.29 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
import react from '@vitejs/plugin-react';
import { defineConfig, Plugin } from 'vite';
import { rmSync, statSync, mkdirSync, cpSync, readdirSync } from 'node:fs';
import { join, resolve } from 'node:path';
import electron from 'vite-plugin-electron/simple';
import renderer from 'vite-plugin-electron-renderer';
const SELECTED_ALGORITHM = process.env.SELECTED_ALGORITHM;
if (!SELECTED_ALGORITHM) throw new Error("No algorithm has been selected, specify the algorithm via the SELECTED_ALGORITHM env var.");
export default defineConfig(({ command }) => {
rmSync('dist-electron', { recursive: true, force: true });
const isServe = command === 'serve';
return {
resolve: {
alias: {
'@': join(import.meta.dirname, 'src'),
'common': join(import.meta.dirname, 'common'),
'@selected-algo': `@wfp/common-identifier-algorithms/${SELECTED_ALGORITHM}`
}
},
optimizeDeps: {
exclude: [ 'node_modules/.vite/deps' ]
},
plugins: [
react(),
electron({
main: {
entry: 'electron/main/index.ts',
vite: {
resolve: {
alias: {
'@selected-algo': `@wfp/common-identifier-algorithms/${SELECTED_ALGORITHM}`
}
},
build: {
sourcemap: isServe ? 'inline' : false,
minify: true,
rolldownOptions: {
input: {
index: resolve('electron/main/index.ts'),
validateFileWorker: resolve('electron/main/ipc-handlers/workers/validateFileWorker.ts'),
processFileWorker: resolve('electron/main/ipc-handlers/workers/processFileWorker.ts'),
encryptFileWorker: resolve('electron/main/ipc-handlers/workers/encryptFileWorker.ts'),
// TODO: add other workers here
},
output: {
format: "cjs",
entryFileNames: (chunkInfo) => chunkInfo.name.endsWith("Worker") ? "workers/[name].js" : "[name].js",
}
}
},
plugins: [ copyAssetsToDistElectron() ]
}
},
preload: {
input: 'electron/preload.mts',
},
}),
renderer()
],
};
});
function copyAssetsToDistElectron(): Plugin {
const SRC_ASSETS = resolve(process.cwd(), 'assets');
const DEST_ASSETS = resolve(process.cwd(), 'dist-electron', 'assets');
let ranOnce = false;
return {
name: 'copy-assets-to-dist-electron (main)',
apply: 'build',
buildStart() {
try { mkdirSync(DEST_ASSETS, { recursive: true }) } catch {}
},
closeBundle() {
if (ranOnce) return;
ranOnce = true;
try {
const st = statSync(SRC_ASSETS);
if (!st.isDirectory()) return; // no assets dir; skip
} catch (err: any) {
if (err?.code === 'ENOENT') return; // missing assets dir; skip
console.warn('[copy-assets-to-dist-electron] warning:', err?.message);
return;
}
mkdirSync(DEST_ASSETS, { recursive: true });
cpSync(SRC_ASSETS, DEST_ASSETS, {recursive: true});
console.log(`[copy-assets-to-dist-electron] Copied assets -> ${DEST_ASSETS}`);
console.log(`[copy-assets-to-dist-electron] Assets directory contains: ${readdirSync(DEST_ASSETS)}`);
},
};
}