An Electron CLI integrated with Vite #7570
alex8088
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
electron-vite
Features
Usage
Install
Development & Build
In a project where
electron-vite
is installed, you can useelectron-vite
binary directly withnpx electron-vite
or add the npm scripts to yourpackage.json
file like this:In order to use the renderer process HMR, you need to use the
environment variables
to determine whether the window browser loads a local html file or a remote URL.Note: For development, the renderer process
index.html
file needs to reference your script code via<script type="module">
.Recommended project directory
Get started
Clone the electron-vite-boilerplate or use the create-electron tool to scaffold your project.
Configure
Config file
When running
electron-vite
from the command line, electron-vite will automatically try to resolve a config file namedelectron.vite.config.js
inside project root. The most basic config file looks like this:You can also explicitly specify a config file to use with the
--config
CLI option (resolved relative tocwd
):Tips:
electron-vite
also supportsts
ormjs
config file.Config intellisense
Since
electron-vite
ships with TypeScript typings, you can leverage your IDE's intellisense with jsdoc type hints:Alternatively, you can use the
defineConfig
anddefineViteConfig
helper which should provide intellisense without the need for jsdoc annotations:Tips: The
defineViteConfig
exports fromVite
.Config reference
See vitejs.dev
Config presets
Build options for
main
:out\main
(relative to project root)node*
, automatically match node target ofElectron
. For example, the node target of Electron 17 isnode16.13
src\main\{index|main}.{js|ts|mjs|cjs}
(relative to project root), empty string if not foundcjs
electron
and all builtin modulesBuild options for
preload
:out\preload
(relative to project root)main
src\preload\{index|preload}.{js|ts|mjs|cjs}
(relative to project root), empty string if not foundcjs
main
Build options for
renderer
:src\renderer
(relative to project root)out\renderer
(relative to project root)chrome*
, automatically match chrome target ofElectron
. For example, the chrome target of Electron 17 ischrome98
src\renderer\index.html
(relative to project root), empty string if not foundfalse
, there is no need to polyfillModule Preload
for the Electron renderermain
Define option for
main
andpreload
In web development, Vite will transform
'process.env.'
to'({}).'
. This is reasonable and correct. But in nodejs development, we sometimes need to useprocess.env
, soelectron-vite
will automatically add config define field to redefine global variable replacements like this:Note: If you want to use these configurations in an existing project, please see the Vite plugin vite-plugin-electron-config
Config FAQs
How do I configure when the Electron app has multiple windows?
When your electron app has multiple windows, it means there are multiple html files or preload files. You can modify your config file like this:
CLI options
For the full list of CLI options, you can run
npx electron-vite -h
in your project. The flags listed below are only available via the command line interface:--ignoreConfigWarning
: boolean, allow you ignore warning when config missing--outDir
: string, output directory (default: out)API
build
Type Signature:
Example Usage:
createServer
Type Signature:
Example Usage:
preview
Type Signature:
Example Usage:
InlineConfig
The InlineConfig interface extends Vite UserConfig with additional properties:
ignoreConfigWarning
: set tofalse
to ignore warning when config missingAnd omit
base
property because it is not necessary to set the base public path in Electron.resolveConfig
Type Signature:
Beta Was this translation helpful? Give feedback.
All reactions