美文网首页
Electron:Win环境下-Vue项目打包

Electron:Win环境下-Vue项目打包

作者: CodeMT | 来源:发表于2023-05-29 14:37 被阅读0次

准备工作

  • 已有的一个正常的Vue项目(Vue3 + Vite + Ts)
  • Node版本14.18.2

注意:Electron不能跨架构跨平台打包,windows下只能打包windows包,如需要打包,MacOS、Linux等需要去对应平台

建议先设置国内镜像

npm config edit

执行后会弹出npm的配置文档,将以下类容复制到文件末尾(格式跟文档中保持一致)

electron_mirror=https://npm.taobao.org/mirrors/electron/
    
electron-builder-binaries_mirror=https://npm.taobao.org/mirrors/electron-builder-binaries/

一、安装依赖

项目根目录下安装如下依赖包

npm install electron -g  // 全局安装Electron,加g是全局安装,自动添加到环境变量中
vue add electron-builder  // 安装Electron-builder打包工具  
  • vite中支持Electron的插件也改为vite-electron-plugin
  • Electron-packager同样可以打包,官方更推荐使用Electron-builder

可使用输入electron命令验证

如果出现安装出错?

  • 检查下Node版本是否与Electron版本匹配,如果不匹配需要降级/升级
  • 检查vue-cli是否匹配,如果不匹配卸载重装
  • 检查网络情况国内镜像是否配置成功

二、electron配置文件

2.1 新建electron文件夹

在项目根目录下新建一个electron文件夹,与package.json平级

2.2 新建main.js文件

electron文件夹下新建一个main.js文件,并复制以下内容

// Modules to control application life and create native browser window
const { app, BrowserWindow, Menu } = require('electron');
const path = require('path');
const mode = process.env.NODE_ENV;

// 隐藏electron创听的菜单栏
Menu.setApplicationMenu(null);

function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800, // 窗口打开的宽度
    height: 600,  // 窗口打开的高度
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      //  渲染进程 开启node模块,使得JS中可以使用node的model
      nodeIntegration: true,
    }
  });

  // and load the index.html of the app.
  mainWindow.loadURL(
    mode === 'development' ? 'http://localhost:2103' : `file://${path.join(__dirname, '../dist/index.html')}`
  );

  // Open the DevTools.
  if (mode === 'development') {
    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', function () {
  if (process.platform !== 'darwin') app.quit()
  BrowserWindow.addDevToolsExtension('node_modules/vue-devtools/vender')
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

2.3 新建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 type of ['chrome', 'node', 'electron']) {
    replaceText(`${type}-version`, process.versions[type])
  }
})

备注

mainpreload文件来自Electron快速启动示例代码,可以直接将项目拉取下来查看

git clone https://github.com/electron/electron-quick-start

三、package.json配置

在package.json文件中添加一下内容

"main": "electron/main.js",
"scripts": {
  "electron:build": "npm run build && electron-builder"
},

注意:如果package.json中有"type": "module",字段则需要去掉,避免报错

  • windows系统中打包,只需在项目根目录输入electron-builder即可,不需要加任何参数,就可以打包出一个与当前系统相匹配的安装包。
  • electron-builder --win --x64,其中,--win可简写为-w--64表示64位系统软件;如果要打包一个32位系统的软件,可以添加参数--ia32

四、打包完成

npm run electron:build 
// 构建打包客户端-会在根目录生成dist_electron文件夹,其中的XXX Setup XXX.exe就是安装包

五、其他配置

package.json文件中,有一个build属性,可以配置打包软件的参数

"build": {
  "appId": "xxxxxxxx",
  "copyright": "xxx",
  "productName": "xxx",
  "win": {
    "files":{
      "filter": ["!doc/*"]
    },
  "icon": "res/icon.ico"
  },
  "mac": {
    "files":{
      "filter": ["!doc/*"]
    },
  "icon": "res/icon.icns"
  }
},
  • windows打包时,icon其格式必须是.icowindows图标。如果这么写:"icon": "res/icon",将自动补全为"icon": "res/icon.ico"
  • windows平台的ico文件大小可设置为256×256
  • MacOS平台的ico文件大小可设置为128×128

相关文章

网友评论

      本文标题:Electron:Win环境下-Vue项目打包

      本文链接:https://www.haomeiwen.com/subject/fywxsdtx.html