Files
zn-ai/vite.config.ts
DEV_DSW 79bea4f107 Refactor UUID generation, remove unused logger and encryption utilities, and clean up request handling
- Updated `generateUUID` function for improved readability and performance.
- Deleted `logger.ts`, `other.ts`, `request.ts`, `storage.ts`, `tansParams.ts`, and `validate.ts` as they were no longer needed.
- Simplified TypeScript configuration by removing unnecessary paths and aliases.
- Enhanced Vite configuration for better project structure and maintainability.
2026-04-17 15:38:08 +08:00

145 lines
4.2 KiB
TypeScript

import { defineConfig } from 'vite';
import { resolve } from 'path';
import electron from 'vite-plugin-electron';
import renderer from 'vite-plugin-electron-renderer';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';
import electronBytecode from './plugins/bytenode/vite-plugin-electron-encrypt';
const projectAliases = {
"@src": resolve(__dirname, "./src"),
"@runtime": resolve(__dirname, "./runtime-shared"),
"@electron": resolve(__dirname, "./electron"),
"@service": resolve(__dirname, "./electron/service"),
};
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 = [
'@src/', '@electron/', '@runtime/', '@service/',
];
// 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 }) => {
// 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: [
react(),
tailwindcss(),
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'),
},
resolve: {
alias: projectAliases,
},
build: {
outDir: 'dist-electron/main',
rollupOptions: {
external: isMainProcessExternal,
},
watch: isDev
? {
exclude: ['**/electron/scripts/**', '**/scripts.meta.json'],
}
: undefined,
},
},
},
{
// 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: isDev
? {
exclude: ['**/electron/scripts/**', '**/scripts.meta.json'],
}
: undefined,
},
resolve: {
alias: projectAliases,
},
},
},
]),
renderer(),
],
css: {
transformer: 'lightningcss',
},
resolve: {
alias: projectAliases,
},
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,
},
}
});