Giter Site home page Giter Site logo

vite-solid-electron's Introduction

vite-solid-electron

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

Overview

  • Very simple Vite, SolidJS, Electron integration template.

  • Contains only the basic dependencies.

  • The extension is very flexible.

Installation

# clone the project
git clone https://github.com/ch99q/vite-solid-electron.git

# open the project directory
cd vite-solid-electron

# install dependencies
npm install

# start the application
npm run dev

# make a production build
npm run build

Directory structure

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

├
├── build                     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
├
├── dist                      Generated after build according to the "packages" directory
├   ├── main
├   ├── preload
├   ├── renderer
├
├── release                   Generated after production build, contains executables
├   ├── {version}
├       ├── win-unpacked      Contains unpacked application executable
├       ├── Setup.exe         Installer for the application
├
├── scripts
├   ├── build.mjs             Develop script -> npm run build
├   ├── watch.mjs             Develop script -> npm run dev
├
├── packages
├   ├── main                  Main-process source code
├       ├── vite.config.ts
├   ├── preload               Preload-script source code
├       ├── vite.config.ts
├   ├── renderer              Renderer-process source code
├       ├── vite.config.ts
├

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

  • packages/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)
  • packages/renderer/src/global.d.ts

    // Defined in the window
    interface Window {
      fs: typeof import("fs")
      ipcRenderer: import("electron").IpcRenderer
    }
  • packages/renderer/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.

Click to see more 👉 packages/main/vite.config.ts

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 SolidJS, which are pure javascript modules that can be built with Vite, can be placed in devDependencies. This reduces the size of the application.

Result

Special thanks

Special thanks to caoxiemeihao for almost the entire code base, i just modified a small part to make it work with SolidJS. Original code can be found here

vite-solid-electron's People

Contributors

ch99q avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.