:electron: Electron + Vite + React + Sass boilerplate.
Go to file
草鞋没号 f8677670ef
fix: `index.cjs` -> `index.js`
2022-06-07 07:23:08 +08:00
.vscode fix: `index.cjs` -> `index.js` 2022-06-07 07:23:08 +08:00
electron refactor: minor changes 2022-06-06 21:23:25 +03:00
public 📦 refactor: package, static, editorconfig 2022-06-06 12:42:38 +03:00
src refactor: minor changes 2022-06-06 21:23:25 +03:00
.editorconfig 📦 refactor: package, static, editorconfig 2022-06-06 12:42:38 +03:00
.gitignore update 2022-06-05 08:02:20 +08:00
LICENSE Initial commit 2021-11-01 01:54:59 +00:00
README.md Update README.md 2022-06-07 07:22:07 +08:00
README.zh-CN.md docs: update 2022-04-26 08:28:53 +08:00
electron-builder.json5 feat: improve json5 2022-06-06 21:21:10 +03:00
index.html template-react-ts/index.html 2022-06-05 08:30:46 +08:00
package.json bump vite-plugin-electron from 0.4.6 to 0.4.7 2022-06-06 20:21:21 +08:00
tsconfig.json feat: use path aliases, improve TS 2022-06-06 21:23:07 +03:00
tsconfig.node.json template-react-ts/tsconfig.json 2022-06-05 08:08:12 +08:00
types.d.ts refactor: minor changes 2022-06-06 21:23:25 +03:00
vite.config.ts feat: use path aliases, improve TS 2022-06-06 21:23:07 +03:00

README.md

electron-vite-react

GitHub stars GitHub issues GitHub license Required Node.JS >= v14.17.0

English | 简体中文

Overview

This is a Vite-integrated Electron template built with simplification in mind.

The repo contains only the most basic files, dependencies and functionalities to ensure flexibility for various scenarios.

You need a basic understanding of Electron and Vite to get started. But that's not mandatory - you can learn almost all the details by reading through the source code. Trust me, this repo is not that complex. 😋

Quick start

npm create electron-vite

electron-vite-react.gif

Debug

electron-vite-react-debug.gif

Directory structure

Once dev or build npm-script is executed, the dist folder will be generated. It has the same structure as the project, the purpose of this design is to ensure the correct path calculation.

├── electron                  Electron-related code
|   ├── main                  Main-process source code
|   ├── preload               Preload-script source code
|   └── resources             Resources for the production build
|       ├── icon.icns             Icon for the application on macOS
|       ├── icon.ico              Icon for the application
|       ├── installerIcon.ico     Icon for the application installer
|       └── uninstallerIcon.ico   Icon for the application uninstaller
|
├── release                   Generated after production build, contains executables
|   └──{version}
|       ├── {os}-unpacked     Contains unpacked application executable
|       └── Setup.{ext}       Installer for the application
|
├── public                    Static assets
└── src                       Renderer source code, your React application

Use Electron and NodeJS API

🚧 By default, Electron doesn't support the use of API related to Electron and NodeJS in the Renderer process, but someone might need to use it. If so, you can see the template 👉 electron-vite-boilerplate

Invoke Electron and NodeJS API in Preload-script

  • electron/preload/index.ts

    import fs from "fs";
    import { contextBridge, ipcRenderer } from "electron";
    
    // --------- Expose some API to Renderer-process. ---------
    contextBridge.exposeInMainWorld("fs", fs);
    contextBridge.exposeInMainWorld("ipcRenderer", ipcRenderer);
    
  • src/global.d.ts

    // Defined in the window
    interface Window {
      fs: typeof import("fs");
      ipcRenderer: import("electron").IpcRenderer;
    }
    
  • src/main.ts

    // Use Electron and NodeJS API in the Renderer-process
    console.log("fs", window.fs);
    console.log("ipcRenderer", window.ipcRenderer);
    

Use SerialPort, SQLite3, or other node-native addons in the Main-process

  • First, you need to make sure that the dependencies in the package.json are NOT in the "devDependencies". Because the project will need them after packaged.

  • Main-process, Preload-script are also built with Vite, and they're built as build.lib.
    So they just need to configure Rollup.

export default {
  build: {
    // built lib for Main-process, Preload-script
    lib: {
      entry: "index.ts",
      formats: ["cjs"],
      fileName: () => "[name].js",
    },
    rollupOptions: {
      // configuration here
      external: ["serialport", "sqlite3"],
    },
  },
};

dependencies vs devDependencies

  • First, you need to know if your dependencies are needed after the application is packaged.

  • Like serialport, sqlite3 they are node-native modules and should be placed in dependencies. In addition, Vite will not build them, but treat them as external modules.

  • Dependencies like Vue and React, which are pure javascript modules that can be built with Vite, can be placed in devDependencies. This reduces the size of the application.