-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
126 lines (118 loc) · 2.84 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
import vue from '@vitejs/plugin-vue';
import fs from 'fs';
import { fileURLToPath, URL } from 'node:url';
import path from 'path';
import { defineConfig, type Plugin, type UserConfig } from 'vite';
import electron from 'vite-plugin-electron';
import pkg from './package.json';
fs.rmSync('dist', { recursive: true, force: true });
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
plugins: [
vue(),
electron({
main: {
entry: 'electron/main.ts',
vite: withDebug({
build: {
sourcemap: 'inline',
outDir: 'dist/electron',
},
}),
},
preload: {
input: {
preload: path.join(__dirname, 'electron/preload.ts'),
},
vite: {
build: {
sourcemap: 'inline',
outDir: 'dist/electron',
},
},
},
}),
renderBuiltUrl(),
],
server: {
host: pkg.env.VITE_DEV_SERVER_HOST,
port: pkg.env.VITE_DEV_SERVER_PORT,
},
build: {
target: 'esnext',
sourcemap: true,
rollupOptions: {
input: {
main: path.resolve(__dirname, 'main.html'),
display: path.resolve(__dirname, 'display.html'),
},
},
},
});
function withDebug(config: UserConfig): UserConfig {
if (process.env.VSCODE_DEBUG) {
if (!config.build) config.build = {};
config.build.sourcemap = true;
config.plugins = (config.plugins || []).concat({
name: 'electron-vite-debug',
configResolved(config) {
const index = config.plugins.findIndex(p => p.name === 'electron-main-watcher');
// At present, Vite can only modify plugins in configResolved hook.
(config.plugins as Plugin[]).splice(index, 1);
},
});
}
return config;
}
function renderBuiltUrl(): Plugin {
// https://github.com/vitejs/vite/blob/main/packages/vite/src/node/constants.ts#L84-L124
const KNOWN_ASSET_TYPES = [
// images
'png',
'jpe?g',
'jfif',
'pjpeg',
'pjp',
'gif',
'svg',
'ico',
'webp',
'avif',
// media
'mp4',
'webm',
'ogg',
'mp3',
'wav',
'flac',
'aac',
// fonts
'woff2?',
'eot',
'ttf',
'otf',
// other
'webmanifest',
'pdf',
'txt',
];
return {
name: 'render-built-url',
config(config) {
config.experimental = {
renderBuiltUrl(filename, type) {
if (KNOWN_ASSET_TYPES.includes(path.extname(filename).slice(1)) && type.hostType === 'js') {
// Avoid Vite relative-path assets handling
// https://github.com/vitejs/vite/blob/89dd31cfe228caee358f4032b31fdf943599c842/packages/vite/src/node/build.ts#L838-L875
return { runtime: JSON.stringify(filename) };
}
},
};
},
};
}