electron-vite-vue/vite.config.ts

123 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-07-24 07:26:24 +08:00
import { rmSync } from 'fs'
2022-08-06 10:58:11 +08:00
import path from 'path'
import {
type Plugin,
type UserConfig,
defineConfig,
} 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
2022-08-06 10:58:11 +08:00
index: path.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
2022-08-11 09:25:19 +08:00
// https://github.com/electron-vite/vite-plugin-electron/tree/main/packages/electron-renderer#electron-renderervite-serve
renderer: {},
}),
2022-08-06 10:58:11 +08:00
renderBuiltUrl(),
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-24 12:34:15 +08:00
if (!config.build) config.build = {}
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
}
2022-08-06 10:58:11 +08:00
// Only worked Vite@3.x #212
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) }
}
},
}
},
}
}