Electron
Electron官网
Electron内置了Chromium(谷歌浏览器)和Node.js
其中Chromium是渲染进程,主要渲染和解析HTML;Node.js作为主进程,管道用IPC通信
Vite构建Electron项目
需要安装两个依赖:
1 2
| npm install electron -D npm install vite-plugin-electron -D
|
配置vite.config.ts:
1 2 3 4 5 6 7
| import electron from "vite-plugin-electron"
export default defineConfig({ plugins: [electron({ entry: "electron/index.js" })], })
|
配置package.json:
1 2 3 4
| { "main": "dist-electron/index.js", }
|
编写electron/index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import {app,BrowserWindow} from "electron"
const createWindow = ()=> { const win = new BrowserWindow({ webPreferences: { nodeIntegration: true, contextIsolation: false, } }) console.log(process.env)
win.loadURL(`${process.env['VITE_DEV_SERVER_URL']}`) }
app.whenReady().then(createWindow)
|
执行npm run dev
,程序启动了。
Electron项目打包
安装依赖:
1 2 3
| npm install electron-builder -D // 能够使用环境变量 npm install cross-env
|
修改electron/index.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import {app,BrowserWindow} from "electron" import path from "path"
const createWindow = ()=> { const win = new BrowserWindow({ webPreferences: { nodeIntegration: true, contextIsolation: false, } })
if(process.env.NODE_ENV != "development") { win.loadFile(path.join(__dirname,"../index.html")) } else { win.loadURL(`${process.env['VITE_DEV_SERVER_URL']}`) } }
app.whenReady().then(createWindow)
|
package.json中添加打包配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| { "scripts": { "dev": "chcp 65001 && cross-env NODE_ENV=development vite", "build": "vue-tsc && vite build && electron-builder", "preview": "vite preview" }, "build": { "appId": "com.electron.desktop", "productName": "electron", "asar": true, "copyright": "Copyright © 2022 electron", "directories": { "output": "release/" }, "files": [ "dist", "dist-electron" ], "mac": { "artifactName": "${productName}_${version}.${ext}", "target": [ "dmg" ] }, "win": { "target": [ { "target": "nsis", "arch": [ "x64" ] } ], "artifactName": "${productName}_${version}.${ext}" }, "nsis": { "oneClick": false, "perMachine": false, "allowToChangeInstallationDirectory": true, "deleteAppDataOnUninstall": false }, "publish": [ { "provider": "generic", "url": "http://127.0.0.1:8080" } ], "releaseInfo": { "releaseNotes": "版本更新的具体内容" } } }
|
nsis的配置项:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| {"oneClick": false, "allowElevation": true, "allowToChangeInstallationDirectory": true, "installerIcon": "public/timg.ico", "uninstallerIcon": "public/timg.ico", "installerHeader": "public/timg.ico", "installerHeaderIcon": "public/timg.ico", "installerSidebar": "public/installerSiddebar.bmp", "uninstallerSidebar": "public/uninstallerSiddebar.bmp", "uninstallDisplayName": "${productName}${version}", "createDesktopShortcut": true, "createStartMenuShortcut": true, "shortcutName": "SHom", "include": "script/installer.nsi", "script": "script/installer.nsi", "deleteAppDataOnUninstall": false, "runAfterFinish": true, "menuCategory": false, }
|
Electron debug工具Debugtron
进程间通信
进程间通信其实就是一种发布-订阅模式。
安装依赖:
1
| pnpm add vite-plugin-electron-renderer
|
修改vite.config.ts:
1 2 3 4 5 6 7 8 9 10 11 12
| import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue'
import electron from "vite-plugin-electron" import electronRender from "vite-plugin-electron-renderer"
export default defineConfig({ plugins: [vue(),electron({ entry: "electron/index.js" }),electronRenderer()], })
|
App.vue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <template> <button @click="send">发送信息</button> </template>
<script setup lang="ts"> import {ipcRenderer} from "electron"
const send = ()=> { // 渲染进程给主进程发送消息 // 参数1:自定义事件名称 其他参数:携带的信息 ipcRenderer.send("message",114514) }
// 主进程使用ipcRenderer侦听来自渲染进程的事件 ipcRenderer.on("load",(_,message)=> { console.log(message, "牛逼") }) </script>
|
electron/index.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| import {app,BrowserWindow,ipcMain} from "electron" import path from "path"
const createWindow = ()=> { const win = new BrowserWindow({ webPreferences: { nodeIntegration: true, contextIsolation: false, } })
console.log(process.env)
if(process.env.NODE_ENV != "development") { win.loadFile(path.join(__dirname,"../dist/index.html")) } else { win.loadURL(`${process.env['VITE_DEV_SERVER_URL']}`) }
win.webContents.openDevTools()
ipcMain.on("message",(_,msg)=> { console.log(msg,"我趣") })
setTimeout(() => { win.webContents.send("load",{message:1919810}) }, 3000) }
app.whenReady().then(createWindow)
|