vue3+vite2+electron19环境搭建

yarn create vite
# project name: vite-demo1
# choose vue->vue-ts
cd vite-demo1
yarn 
yarn dev

# install depedency for electron app
yarn add -D concurrently cross-env electron electron-builder electron-packager wait-on

修改package.json

{
  "name": "vite-demo1",
  "private": true,
  "version": "0.0.0",
  "main": "electron/electron.js",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "serve": "vite preview",
    "start": "electron .",
    "packager": "electron-packager ./dist/ --platform=win32 --arch=x64 --overwrite",
    "electron": "wait-on tcp:3000 && cross-env IS_DEV=true electron .",
    "electron:dev": "concurrently -k \"cross-env BROWSER=none yarn dev\" \"yarn electron\"",
    "electron:build.win": "yarn build && electron-builder --win --dir",
    "electron:build.linux": "yarn build && electron-builder --linux appImage",
    "electron:build.test": "yarn build && electron-builder --dir",
    "electron:build.exe": "yarn build && electron-builder --win"
  },
  "dependencies": {
    "vue": "^3.2.25"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^2.3.3",
    "concurrently": "^7.2.2",
    "cross-env": "^7.0.3",
    "electron": "^19.0.6",
    "electron-builder": "^23.1.0",
    "electron-packager": "^15.5.1",
    "typescript": "^4.5.4",
    "vite": "^2.9.9",
    "vue-tsc": "^0.34.7",
    "wait-on": "^6.0.1"
  },
  "build": {
    "appId": "com.damaho.my-app",
    "productName": "MyApp",
    "copyright": "Copyright © 2022 vrix.yan",
    "mac": {
      "category": "public.app-category.utilities"
    },
    "nsis": {
      "oneClick": false,
      "allowToChangeInstallationDirectory": true
    },
    "files": [
      "dist/**/*",
      "electron/**/*"
    ],
    "directories": {
      "buildResources": "assets",
      "output": "dist_electron"
    },
    "electronDownload": {
      "mirror": "https://npm.taobao.org/mirrors/electron/"
    }
  }
}

修改vite.config.ts

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

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

新增 electron/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();
  }
});

新增electron/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])
    }
  })

运行如下的命令打包:

yarn dev
yarn build
yarn start
#windows
yarn electron:build.exe
#linux
yarn electron:build.linux

亲测有效,放心使用。

注意:所有环境搭建的文章都忽略了个非常重要的细节。那就是electron的版本是要有对应的node版本配合的。否则会出莫名其妙的问题。

vue3+vite2+electron19环境搭建_第1张图片

你可能感兴趣的:(Electron,大数据)