-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathvite.config.ts
146 lines (133 loc) · 4.05 KB
/
vite.config.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import fs from 'fs';
import { join, resolve } from 'path';
import { svelte, vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import { defineConfig } from 'vite';
import electron from 'vite-plugin-electron';
import renderer from 'vite-plugin-electron-renderer';
import pkg from './package.json';
import installExiftool from './scripts/install-exiftool';
const electronOutDir = join(__dirname, 'dist-electron');
const electronPkg = join(electronOutDir, 'package.json');
const electronAlias = {
'@': __dirname,
'@root': resolve(__dirname, 'electron'),
'@src': resolve(__dirname, 'electron/src'),
'@config': resolve(__dirname, 'electron/src/config.ts'),
'@modules': resolve(__dirname, 'electron/src/modules'),
'@utils': resolve(__dirname, 'electron/src/utils'),
'@router': resolve(__dirname, 'electron/src/router'),
};
function objToEnvStr(obj: Record<string, any>) {
const arr: string[] = [];
for (const k in obj) {
arr.push(`VITE_${k}=${obj[k]}`);
}
return arr.join('\n');
}
export default defineConfig(async ({ command }) => {
const port = 5173;
const isServe = command === 'serve';
const isBuild = command === 'build';
const sourcemap = isServe || !!process.env.VSCODE_DEBUG;
const env: Record<string, any> = {
VERSION: pkg.version,
DIST_ELECTRON: electronOutDir,
WEB: join(electronOutDir, 'web'),
URL: isServe ? `http://localhost:${port}` : '',
};
env.PUBLIC = isServe ? join(__dirname, 'web/public') : env.WEB;
env.EXIFTOOL = env.DIST_ELECTRON;
fs.writeFileSync(join(__dirname, '.env.local'), objToEnvStr(env));
if (!fs.existsSync(electronOutDir)) {
fs.mkdirSync(electronOutDir);
}
// electron输出目录添加package文件
fs.writeFileSync(electronPkg, JSON.stringify({
...pkg,
main: 'main/index.js',
type: 'commonjs',
}, null, 2));
// exiftool工具打包进来
await installExiftool(env.DIST_ELECTRON);
return {
root: 'web',
envDir: join(__dirname, './'),
plugins: [
electron([
{
entry: 'electron/main/index.ts',
onstart(options) {
if (process.env.VSCODE_DEBUG) {
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App');
} else {
options.startup();
}
},
vite: {
resolve: {
alias: electronAlias,
extensions: ['.ts', '.js', '.mjs'],
},
build: {
sourcemap: sourcemap ? 'inline' : undefined,
minify: isBuild,
outDir: join(electronOutDir, 'main'),
rollupOptions: {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
},
},
},
},
{
entry: 'electron/preload/index.ts',
onstart(options) {
options.reload();
},
vite: {
resolve: {
alias: electronAlias,
extensions: ['.ts', '.js', '.mjs'],
},
build: {
sourcemap: sourcemap ? 'inline' : undefined,
minify: isBuild,
outDir: join(electronOutDir, 'preload'),
rollupOptions: {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
},
},
},
},
]),
// Use Node.js API in the Renderer-process
renderer(),
svelte({
preprocess: vitePreprocess(),
}),
],
clearScreen: false,
server: {
open: false,
host: '0.0.0.0',
port,
},
build: {
outDir: env.WEB,
rollupOptions: {
input: {
main: join(__dirname, './web/main/index.html'),
},
},
},
resolve: {
alias: {
'@web': resolve(__dirname, 'web'),
'@components': resolve(__dirname, 'web/components'),
'@common': resolve(__dirname, 'common'),
'@web-utils': resolve(__dirname, 'web/util'),
...electronAlias,
},
extensions: ['.js', '.mjs', '.svelte', '.ts'],
},
};
});