electron-builder打包配置(应用名、安装包、图标、快捷方式、自定义文件关联启动等)

使用的是electron-vite + electron-builder

在electron-builder.yml中配置,主要使用的是windows,其他配置请自行验证

yml 复制代码
appId: cn.kingray.www
# 只能string 不能用${productName}
productName: xx软件
copyright: Copyright © Powered by xx公司
directories:
  buildResources: build
  # output: release
files:
  - '!**/.vscode/*'
  - '!src/*'
  - '!electron.vite.config.{js,ts,mjs,cjs}'
  - '!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}'
  - '!{.env,.env.*,.npmrc,pnpm-lock.yaml}'
  - '!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}'
asarUnpack:
  - resources/**
  - out/main/chunks/*.node
asar:
  smartUnpack: true
# 设置自定义文件关联启动
fileAssociations:
  description: kingrayFile
  # 自定义文件后缀
  ext: krzj
  # 自定义文件图标
  icon: build/icons/win/icon.ico
win:
  # 只能string 不能用${productName}
  executableName: xx软件
  target: nsis
  # 必须为true 并且需要管理员运行
  signAndEditExecutable: true
  icon: build/icons/win/icon.ico
nsis:
  # 安装包名称
  artifactName: ${productName}-${version}-setup.${ext}
  # 快捷方式名称
  shortcutName: ${productName}
  # 卸载程序显示名称
  uninstallDisplayName: ${productName}
  # 是否创建桌面快捷方式
  createDesktopShortcut: always
  # 是否创建开始菜单快捷方式
  createStartMenuShortcut: true
  # 是否为单机版
  oneClick: false
  # 是否为机器级别安装
  perMachine: true
  # 是否允许更改安装目录
  allowToChangeInstallationDirectory: true
  # 是否在卸载时删除应用程序数据
  deleteAppDataOnUninstall: false
  # 安装程序图标
  installerIcon: build/icons/win/icon.ico
  # 卸载程序图标
  uninstallerIcon: build/icons/win/icon.ico
  # 安装程序头部图标
  installerHeaderIcon: build/icons/win/icon.ico
  # 安装程序侧边栏图标
  installerSidebar: build/icons/win/icon.ico
  # 许可证文件
  # license: build/license.txt
mac:
  entitlementsInherit: build/entitlements.mac.plist
  extendInfo:
    - NSCameraUsageDescription: Application requests access to the device's camera.
    - NSMicrophoneUsageDescription: Application requests access to the device's microphone.
    - NSDocumentsFolderUsageDescription: Application requests access to the user's Documents folder.
    - NSDownloadsFolderUsageDescription: Application requests access to the user's Downloads folder.
  notarize: false
dmg:
  artifactName: ${productName}-${version}.${ext}
linux:
  target:
    - AppImage
    - snap
    - deb
  maintainer: electronjs.org
  category: Utility
appImage:
  artifactName: ${productName}-${version}.${ext}
npmRebuild: false
publish:
  provider: generic
  url: http://192.168.2.32:8904/

其中${productName}、${version}对应的是package.json中的参数

json 复制代码
  "name": "app",
  "productName": "xx软件",
  "version": "2.0.2",

将png图标转化为ico

执行package.json中的脚本,使用electron-icon-maker,将resources/icon.png输出到build下

json 复制代码
"build:icons": "electron-icon-maker --input=./resources/icon.png --output=./build",

目录

系统托盘图标

需要使用png,ico无法展示

ts 复制代码
    // ---- 窗口收纳于托盘 ---
    const trayIconPath = join(__dirname, '../../resources/icon.png')
    const trayIcon = nativeImage.createFromPath(trayIconPath)
    const tray = new Tray(trayIcon)

    // 设置托盘工具提示(鼠标悬停时的名称)
    tray.setToolTip('xx软件')

    // 创建上下文菜单
    const contextMenu = Menu.buildFromTemplate([
      { label: '显示应用', click: () => mainWindow?.show() },
      { label: '退出', click: () => app.quit() }
    ])
    tray.setContextMenu(contextMenu)

    // (可选)点击托盘图标显示/隐藏窗口
    tray.on('click', () => {
      if (mainWindow?.isVisible()) {
        mainWindow?.hide()
      } else {
        mainWindow?.show()
      }
    })
相关推荐
不可能片场15 小时前
Electron 发布 cookie 库先查明文再写解密
前端·electron
又过二十1 天前
用 Electron 做的 AI 视频切片工作台:VClip 的技术选型与踩坑实录
ai·electron·aigc·funasr·视频切片
不可能片场3 天前
工程实践 日志文件名是北京日期 内容却是 UTC
前端·electron
不可能片场7 天前
工程实践 台账备份从全量复制改成增量日志
前端·electron
林恒smileZAZ7 天前
用 Vue 3 + Electron 做桌宠:从启动退出到拖拽、单击与鼠标穿透
vue.js·electron·计算机外设
非常好吃的糖7 天前
Windows 桌面端的语音唤醒:从「识别出文字」到「识别出唤醒词」
electron
Suprman9 天前
从 B/S 到 Electron:平面排版套料系统桌面版开发实战
javascript·平面·electron·排序算法
不可能片场11 天前
工程实践 一个缓存把选片从十分钟压到一分钟
前端·electron
lemon_sjdk12 天前
从一段真实的 Electron IM 前端代码,看高级前端的工程化修养
前端·electron·状态模式·im
优选资源分享12 天前
PicGo v3.0.3 新版 | 开源免费图床工具
javascript·笔记·electron