Preview mode allows you to test production builds locally before distribution, providing a quick way to verify that your Electron app works correctly in production mode.
Start Electron app to preview production build with optional build step.
/**
* Start electron app to preview production build
* @param inlineConfig - Optional inline configuration overrides
* @param options - Preview options
* @returns Promise that resolves when preview starts
*/
function preview(
inlineConfig?: InlineConfig,
options?: { skipBuild?: boolean }
): 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 { preview } from "electron-vite";
// Preview with automatic build
await preview();
// Skip build and preview existing build
await preview({}, { skipBuild: true });
// Preview with custom configuration
await preview({
configFile: 'electron.vite.production.config.js',
mode: 'production'
});Build Phase (unless skipBuild: true):
Launch Phase:
When using skipBuild: true:
Preview mode sets production environment:
// Available in all processes
process.env.NODE_ENV // Set to 'production'
process.env.NODE_ENV_ELECTRON_VITE // Set to 'production'
// No development server URL
process.env.ELECTRON_RENDERER_URL // undefinedPreview mode is commonly used via CLI:
# Build and preview
electron-vite preview
# Preview existing build
electron-vite preview --skipBuild
# Preview with custom options
electron-vite preview --noSandbox --entry=custom-main.jsAvailable CLI options for preview:
--skipBuild: Skip the build step--noSandbox: Force renderer process to run un-sandboxed--entry <file>: Specify custom electron entry file--config <file>: Use specific config file--mode <mode>: Set environment mode--logLevel <level>: Set logging levelPreview mode is ideal for testing:
Common debugging scenarios:
// Test asset loading in production
const assetPath = process.env.NODE_ENV === 'development'
? './assets/icon.png'
: path.join(__dirname, '../renderer/assets/icon.png');
// Test different behavior between dev and production
if (process.env.NODE_ENV === 'development') {
// Development-only code
} else {
// Production code path
}Verify production build integrity:
Configure behavior specifically for preview mode:
// In electron.vite.config.js
export default defineConfig(({ command, mode }) => {
const isPreview = process.env.NODE_ENV_ELECTRON_VITE === 'production';
return {
main: {
build: {
sourcemap: isPreview ? 'inline' : false, // Source maps for preview debugging
minify: !isPreview // Disable minification for preview debugging
}
},
renderer: {
base: './', // Relative base for filesystem loading
build: {
assetsDir: 'assets',
rollupOptions: {
output: {
format: 'es'
}
}
}
}
};
});Ensure proper structure for preview mode:
out/
├── main/
│ └── index.js # Main process entry
├── preload/
│ └── index.js # Preload script
└── renderer/
├── index.html # Renderer entry point
└── assets/ # Static assets
├── main.js
├── main.css
└── images/Preview mode provides specific error handling: