The build system provides production build functionality that compiles all Electron processes (main, preload, renderer) for distribution with optimizations and asset handling.
Main build function that orchestrates the production build process for all configured Electron processes.
/**
* Bundles the electron app for production
* @param inlineConfig - Optional inline configuration overrides
* @returns Promise that resolves when build completes
*/
function build(inlineConfig?: InlineConfig): Promise<void>;
type InlineConfig = Omit<ViteConfig, 'base'> & {
/** Path to config file or false to disable */
configFile?: string | false;
/** Disable .env file loading */
envFile?: false;
/** Ignore configuration warnings */
ignoreConfigWarning?: boolean;
};Usage Examples:
import { build } from "electron-vite";
// Basic build
await build();
// Build with inline configuration
await build({
mode: 'production',
build: {
outDir: 'custom-dist',
sourcemap: true
}
});
// Build with custom config file
await build({
configFile: 'electron.vite.production.config.js'
});The build system follows this sequence:
Default output structure:
out/
├── main/ # Main process output
│ └── index.js
├── preload/ # Preload scripts output
│ └── index.js
└── renderer/ # Renderer process output
├── index.html
└── assets/Common build configuration options:
// In electron.vite.config.js
export default defineConfig({
main: {
build: {
outDir: 'dist/main',
minify: true,
sourcemap: false,
rollupOptions: {
external: ['electron', 'node:*']
}
}
},
preload: {
build: {
outDir: 'dist/preload',
lib: {
entry: 'src/preload/index.ts',
formats: ['cjs']
}
}
},
renderer: {
build: {
outDir: 'dist/renderer',
rollupOptions: {
input: {
main: 'src/renderer/index.html'
}
}
}
}
});Native Node.js modules are automatically handled:
// Automatically resolved with ?asset suffix
import addonPath from './native-addon.node?asset';WebAssembly modules with loader support:
// WASM with loader
import loadWasm from './module.wasm?loader';
const wasmInstance = await loadWasm();Node.js worker thread support:
// Worker thread module
import WorkerConstructor from './worker.js?nodeWorker';
const worker = new WorkerConstructor({
workerData: { /* data */ }
});Build-time environment variable handling:
// In your code
const isDev = process.env.NODE_ENV === 'development';
const apiUrl = process.env.VITE_API_URL;
// Electron-specific environment variables
const rendererUrl = process.env.ELECTRON_RENDERER_URL; // Dev server URLAutomatic code splitting for renderer process:
// Lazy loading in renderer
const Component = lazy(() => import('./components/HeavyComponent'));Dead code elimination is enabled by default for all processes.
Production builds are minified using esbuild by default:
{
build: {
minify: 'esbuild', // or 'terser' for more aggressive minification
terserOptions: {
// Terser-specific options
}
}
}Build process includes comprehensive error handling:
Build errors are logged with detailed context information and stack traces for debugging.