Files
zn-ai/vite.config.ts
2026-04-14 17:02:20 +08:00

165 lines
5.5 KiB
TypeScript

import { defineConfig } from 'vite';
import { resolve } from 'path';
import electron from 'vite-plugin-electron';
import renderer from 'vite-plugin-electron-renderer';
import vue from '@vitejs/plugin-vue';
import tailwindcss from '@tailwindcss/vite';
import autoImport from 'unplugin-auto-import/vite';
import electronBytecode from './plugins/bytenode/vite-plugin-electron-encrypt';
function isMainProcessExternal(id: string): boolean {
if (!id || id.startsWith('\0')) return false;
if (id.startsWith('.') || id.startsWith('/') || /^[A-Za-z]:[\\/]/.test(id)) return false;
// Project-specific aliases that should be bundled (not external)
const internalAliases = [
'@lib/', '@electron/', '@src/', '@locales/', '@service/', '@utils/',
'@assets/', '@api/', '@constant/', '@components/', '@hooks/', '@store/', '@shared/'
];
// Check if the ID starts with any internal alias
for (const alias of internalAliases) {
if (id.startsWith(alias)) return false;
}
// Externalize @electron/ packages and other @scoped packages
if (id.startsWith('@/') || id.startsWith('@electron/')) return false;
// Externalize all other modules (npm packages)
return true;
}
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
// Determine if we're in development mode
const isDev = mode === 'development';
// Development server URL - in dev mode, this will be the Vite dev server URL
// In production, this should be undefined
// Prefer VITE_DEV_SERVER_URL environment variable if set (e.g., when port is auto-selected)
const devServerUrl = isDev
? (process.env.VITE_DEV_SERVER_URL || 'http://localhost:5173')
: undefined;
return {
// Required for Electron: all asset URLs must be relative because the renderer
// loads via file:// in production. vite-plugin-electron-renderer sets this
// automatically, but we declare it explicitly so the intent is clear.
base: './',
plugins: [
vue(),
tailwindcss(),
autoImport({
imports: ['vue', 'vue-router', 'pinia', 'vue-i18n', '@vueuse/core'],
dts: 'src/auto-imports.d.ts'
}),
electron([
{
// Main process entry file
entry: 'electron/main.ts',
onstart(options) {
options.startup();
},
vite: {
plugins: [electronBytecode()],
define: {
// Inject the dev server URL for use in main process
MAIN_WINDOW_VITE_DEV_SERVER_URL: JSON.stringify(devServerUrl),
'process.env.VITE_SERVICE_URL': JSON.stringify(process.env.VITE_SERVICE_URL || 'http://8.138.234.141/ingress'),
},
build: {
outDir: 'dist-electron/main',
rollupOptions: {
external: isMainProcessExternal,
},
watch: {
exclude: ['**/electron/scripts/**', '**/scripts.meta.json'],
},
},
resolve: {
alias: {
"@electron": resolve(__dirname, "./electron"),
"@lib": resolve(__dirname, "./src/lib"),
"@src": resolve(__dirname, "./src"),
"@locales": resolve(__dirname, "./src/i18n/locales"),
"@service": resolve(__dirname, "./electron/service"),
"@utils": resolve(__dirname, "./electron/utils"),
},
},
},
},
{
// Preload scripts entry file
entry: 'electron/preload/index.ts',
onstart(options) {
options.reload();
},
vite: {
build: {
outDir: 'dist-electron/preload',
rollupOptions: {
external: ['electron'],
output: {
entryFileNames: 'preload.js',
},
},
watch: {
exclude: ['**/electron/scripts/**', '**/scripts.meta.json'],
},
},
resolve: {
alias: {
"@lib": resolve(__dirname, "./src/lib"),
"@electron": resolve(__dirname, "./electron"),
"@src": resolve(__dirname, "./src"),
"@locales": resolve(__dirname, "./src/i18n/locales"),
},
},
},
},
]),
renderer(),
],
css: {
transformer: 'lightningcss',
},
resolve: {
alias: {
"@src": resolve(__dirname, "./src"),
"@api": resolve(__dirname, "./src/api"),
"@assets": resolve(__dirname, "./src/assets"),
"@lib": resolve(__dirname, "./src/lib"),
"@constant": resolve(__dirname, "./src/constant"),
"@components": resolve(__dirname, "./src/components"),
"@hooks": resolve(__dirname, "./src/hooks"),
"@store": resolve(__dirname, "./src/store"),
"@utils": resolve(__dirname, "./src/utils"),
"@shared": resolve(__dirname, "./src/shared"),
"@locales": resolve(__dirname, "./src/i18n/locales"),
"@electron": resolve(__dirname, "./electron"),
"@service": resolve(__dirname, "./electron/service"),
},
},
server: {
port: 5173,
watch: {
ignored: ['**/electron/scripts/**', '**/scripts.meta.json'],
},
proxy: {
'/ingress': {
target: 'http://8.138.234.141',
changeOrigin: true,
},
},
},
build: {
outDir: 'dist',
emptyOutDir: true,
},
}
});