uni-app通过配置Electron打包桌面应用

393 阅读2分钟

uni-app通过Electron打包桌面应用

修改manifest.json应用配置

  • 运行的基础路径:./
  • 去掉启用https协议

image.png

前端打包

  • 点击发行,选择网站-PC Web或手机H5(仅适用于uni-app)(H)
  • 包路径unpackage/dist/build/web

Electron配置

  • web包目录下新建main.js
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
 
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
 
function createWindow () {
  // 从命令行获取参数
  const args = process.argv.slice(1);
  let deviceId = '';
  let screenId = '';

  // 解析 deviceid 和 screenid 参数
  args.forEach((arg, index) => {
    if (arg === '--deviceid') {
      deviceId = args[index + 1];
    }
    if (arg === '--screenid') {
      screenId = args[index + 1];
    }
  });
  
  // Create the browser window.
  win = new BrowserWindow({
    width: 800,
    height: 600,
    fullscreen: true,  // 启动时全屏
    frame: false,  // 无边框
    autoHideMenuBar: true,  // 隐藏菜单栏
  })
 
  // and load the index.html of the app.
  // 构建带参数的本地文件路径
  const localUrl = url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true,
    hash: `/?deviceid=${deviceId}&screenid=${screenId}`  // 使用 hash
  });

  // 加载本地 HTML 文件
  win.loadURL(localUrl);
 
  // Open the DevTools.
  // win.webContents.openDevTools()
 
  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })
}
 
// 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.on('ready', createWindow)
 
// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
 
app.on('activate', () => {
  // 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 (win === null) {
    createWindow()
  }
})
 
// 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.
  • web包目录下新建package.json
{
    "name": "xx应用",
    "version": "2.0",
    "main": "main.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
      "electron": "electron .",
      "start": "electron .",
      "build:win": "electron-packager ./ xx应用 --plantform=win32 --arch=x64 --out xx应用 --overwrite --icon=icon.ico",
      "build:mac-x64": "electron-packager ./ xx应用 --platform=darwin --arch=x64 --out xx应用 --overwrite --icon=icon.icns",
      "build:mac-universal": "electron-packager ./ xx应用 --platform=darwin --arch=universal --out xx应用 --overwrite --icon=icon.icns",
      "build:linux-x64": "electron-packager ./ xx应用 --platform=linux --arch=x64 --out xx应用  --overwrite --no-prune --ignore=/node_modules --icon=icon.png",
      "build:linux-arm64": "electron-packager ./ xx应用 --platform=linux --arch=arm64 --out zhs  --overwrite --no-prune --ignore=/node_modules --icon=icon.png"
    },
    "author": "",
    "license": "ISC"
}
  • 安装依赖
npm install electron
npm install electron-packager
  • 查看package.json是否成功安装依赖
"devDependencies": {
  "electron": "^23.3.13", // 根据当前node版本对应的版本
  "electron-packager": "^12.2.0" // 根据当前node版本对应的版本
}

Electron运行和打包

  • 运行
npm run start
  • 打包
# win系统
npm run build:win
# mac Intel版本
npm run build:mac-x64
# mac M1/M2版本
npm run build:mac-universal
# linux x64版本
npm run build:linux-x64
# linux arm版本
npm run build:linux-arm64