-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathelectron.vite.config.ts
More file actions
108 lines (100 loc) · 2.63 KB
/
electron.vite.config.ts
File metadata and controls
108 lines (100 loc) · 2.63 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
import { execSync } from "node:child_process";
import { resolve } from "node:path";
import react from "@vitejs/plugin-react";
import { defineConfig, externalizeDepsPlugin } from "electron-vite";
import type { Plugin } from "vite";
const devCsp =
"default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: file: local-file: https://i.scdn.co https://*.mzstatic.com; connect-src 'self' ws://localhost:* http://localhost:*; base-uri 'none'; object-src 'none'";
const prodCsp =
"default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: file: local-file: https://i.scdn.co https://*.mzstatic.com; connect-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'; object-src 'none'";
function cspPlugin(csp: string): Plugin {
return {
name: "csp",
transformIndexHtml(html) {
return html.replace('content="__CSP__"', `content="${csp}"`);
},
};
}
function getGitSha(): string | undefined {
try {
return execSync("git rev-parse HEAD", { encoding: "utf8" }).trim();
} catch {
return undefined;
}
}
function getBuildDate(): string {
return new Date().toISOString().split("T")[0];
}
export default defineConfig(({ command }) => {
const csp = command === "serve" ? devCsp : prodCsp;
const isProd = command === "build";
const watchIgnored = [
"**/node_modules/**",
"**/.git/**",
"**/build/**",
"**/dist/**",
"**/screenshots/**",
"**/Library/**",
];
const buildDefines = {
__BUILD_DATE__: isProd ? JSON.stringify(getBuildDate()) : "undefined",
__GIT_SHA__: isProd ? JSON.stringify(getGitSha()) : "undefined",
__RELEASE_CHANNEL__: JSON.stringify(
process.env.RELEASE_CHANNEL || "stable",
),
};
return {
main: {
plugins: [externalizeDepsPlugin()],
define: buildDefines,
build: {
lib: {
entry: resolve(__dirname, "electron/main/index.ts"),
},
watch: {
exclude: watchIgnored,
},
rollupOptions: {
external: ["better-sqlite3", "sharp"],
},
},
},
preload: {
plugins: [externalizeDepsPlugin()],
build: {
lib: {
entry: resolve(__dirname, "electron/preload/index.ts"),
formats: ["cjs"],
},
watch: {
exclude: watchIgnored,
},
rollupOptions: {
output: {
entryFileNames: "[name].cjs",
},
},
},
},
renderer: {
root: ".",
server: {
watch: {
usePolling: false,
ignored: watchIgnored,
},
},
build: {
rollupOptions: {
input: resolve(__dirname, "index.html"),
},
},
resolve: {
alias: {
"@": resolve(__dirname, "src"),
},
},
plugins: [react(), cspPlugin(csp)],
},
};
});