electron-vite-vue/vite.config.ts

60 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-07-24 07:26:24 +08:00
import { rmSync } from 'fs'
import { join } from 'path'
2022-07-23 17:21:53 +08:00
import { defineConfig, Plugin, UserConfig } from 'vite'
2022-06-13 22:10:18 +08:00
import vue from '@vitejs/plugin-vue'
2022-06-14 20:53:01 +08:00
import electron from 'vite-plugin-electron'
import pkg from './package.json'
2022-06-13 22:10:18 +08:00
2022-06-15 21:47:33 +08:00
rmSync('dist', { recursive: true, force: true }) // v14.14.0
2022-06-13 22:10:18 +08:00
// https://vitejs.dev/config/
export default defineConfig({
2022-06-14 20:53:01 +08:00
plugins: [
vue(),
electron({
main: {
entry: 'electron/main/index.ts',
2022-07-23 17:21:53 +08:00
vite: withDebug({
build: {
outDir: 'dist/electron/main',
},
2022-07-23 17:21:53 +08:00
}),
},
preload: {
input: {
// You can configure multiple preload here
index: join(__dirname, 'electron/preload/index.ts'),
},
vite: {
build: {
2022-07-23 17:21:53 +08:00
// For Debug
sourcemap: 'inline',
outDir: 'dist/electron/preload',
2022-07-18 13:46:50 +08:00
},
},
},
// Enables use of Node.js API in the Renderer-process
renderer: {},
}),
2022-06-14 20:53:01 +08:00
],
server: {
host: pkg.env.VITE_DEV_SERVER_HOST,
port: pkg.env.VITE_DEV_SERVER_PORT,
},
2022-06-13 22:10:18 +08:00
})
2022-07-23 17:21:53 +08:00
function withDebug(config: UserConfig): UserConfig {
2022-07-24 07:26:24 +08:00
if (process.env.VSCODE_DEBUG) {
2022-07-23 17:21:53 +08:00
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');
2022-07-24 07:26:24 +08:00
// At present, Vite can only modify plugins in configResolved hook.
2022-07-23 17:21:53 +08:00
(config.plugins as Plugin[]).splice(index, 1)
},
})
}
return config
}