Vite + Vue 3 + electron 環(huán)境搭建

參考:Vite + Vue 3 + electron + TypeScript - DEV Community

第1步,建立一個新的vite項(xiàng)目

yarn create vite

第2步,安裝項(xiàng)目依賴

yarn add -D concurrently cross-env electron electron-builder wait-on

第3部,修改package.json文件

添加build節(jié)點(diǎn),更多的信息可以參閱electron.build

"build": {
  "appId": "com.my-website.my-app",
  "productName": "MyApp",
  "copyright": "Copyright ? 2019 ${author}",
  "mac": {
    "category": "public.app-category.utilities"
  },
  "nsis": {
    "oneClick": false,
    "allowToChangeInstallationDirectory": true
  },
  "files": [
    "dist/**/*",
    "electron/**/*"
  ],
  "directories": {
    "buildResources": "assets",
    "output": "dist_electron"
  }
}

修改scripts節(jié)點(diǎn)

"scripts": {
  "dev": "vite",
  "build": "vite build",
  "serve": "vite preview",
  "electron": "wait-on tcp:3000 && cross-env IS_DEV=true electron .",
  "electron:pack": "electron-builder --dir",
  "electron:dev": "concurrently -k \"cross-env BROWSER=none yarn dev\" \"yarn electron\"",
  "electron:builder": "electron-builder",
  "build:for:electron": "cross-env ELECTRON=true vite build",
  "app:pack": "yarn build:for:electron && electron-builder --dir",
  "app:build": "yarn build:for:electron && yarn electron:builder"
},

添加main節(jié)點(diǎn)

{
  "main": "electron/electron.js",
}

第 4 步:編輯vite.config.js

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  base: process.env.ELECTRON=="true" ? './' : "",
  plugins: [vue()]
})

因?yàn)?code>base的設(shè)置在ELECTRON環(huán)境下與網(wǎng)站不同。

第 5 步:

在項(xiàng)目根文件夾創(chuàng)建一個新的文件夾electron,并在其中創(chuàng)建文件electron.js

// electron/electron.js
const path = require('path');
const { app, BrowserWindow } = require('electron');

const isDev = process.env.IS_DEV == "true" ? true : false;

function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true,
    },
  });

  // and load the index.html of the app.
  // win.loadFile("index.html");
  mainWindow.loadURL(
    isDev
      ? 'http://localhost:3000'
      : `file://${path.join(__dirname, '../dist/index.html')}`
  );
  // Open the DevTools.
  if (isDev) {
    mainWindow.webContents.openDevTools();
  }
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  createWindow()
  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

再創(chuàng)建一個文件preload.js

// electron/preload.js
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener('DOMContentLoaded', () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector)
    if (element) element.innerText = text
  }

  for (const dependency of ['chrome', 'node', 'electron']) {
    replaceText(`${dependency}-version`, process.versions[dependency])
  }
})

完成

到此環(huán)境已安裝完成,請使用以下命令

yarn dev //瀏覽器開發(fā)
yarn electron:dev //electron開發(fā)
yarn app:pack //發(fā)布,只合并,不產(chǎn)生壓縮包
yarn app:build //發(fā)布應(yīng)用
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容