修改 , 通过后置钩子,复制小图标。没有小图标打包后的应用会打不开,很难以发现这个问题,具体如何调试,后面再说。

添加打包配置

修改 package.json ,添加一下配置,它来自于 electron-build 其实基本上没有多少需要配置的,只需要配置个打包的图标即可。 music.icns 是使用 svg 文件(为了保证高精度)转换来的,使用的是 image2icon 。

  1. "name": "reader",
  2. "version": "0.0.1",
  3. "build": {
  4. "appId": "me.yugo",
  5. "mac": {
  6. "category": "me.yugo",
  7. "icon": "./music.icns"
  8. }
  9. },

npm run dist 最终生成在 dist 的 yml 配置文件会像这样

  1. directories:
  2. output: dist
  3. buildResources: build
  4. extraMetadata:
  5. files:
  6. - from: .
  7. filter:
  8. - package.json
  9. - from: dist/renderer
  10. - from: dist/renderer-dll
  11. extraResources:
  12. - from: static
  13. to: static
  14. appId: me.yugo
  15. mac:
  16. category: me.yugo
  17. icon: ./music.icns
  18. extends: electron-webpack/electron-builder.yml
  19. electronVersion: 2.0.4

将 dock 的图标隐藏

ready 里面调用

在 createMainWindow 中,这里我  换了一种范式。

  1. import { format as formatUrl } from 'url'
  2. if (process.env.NODE_ENV != 'production') {
  3. win.webContents.openDevTools()
  4. win.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`)
  5. } else {
  6. formatUrl({
  7. pathname: join(__dirname, 'index.html'),
  8. protocol: 'file',
  9. })
  10. )
  11. }

修复 Music.svelte 图标

  1. function getStaticPath() {
  2. const isDevelopment = process.env.NODE_ENV === 'development'
  3. const staticPath = isDevelopment
  4. ? ''
  5. : 'file://' + __dirname.replace(/app\.asar$/, 'static')
  6. return staticPath
  7. }

添加到 data 里面去。

拼接 url

  1. <div class="flex">
  2. <button on:click="file()"><img src="{staticPath + "plus-circle.png"}" width="25" height="25" alt="add"></button>
  3. <button on:click="play()"><img src="{staticPath + "play-circle.png"}" width="25" height="25" alt="play"></button>
  4. <button on:click="pause()"><img src="{staticPath + "time-out.png"}" width="25" height="25" alt="stop"></button>
  5. <button on:click="prev()"><img src="{staticPath + "left-circle.png"}" width="25" height="25" alt="prev"></button>
  6. <button on:click="next()"><img src="{staticPath + "right-circle.png"}" width="25" height="25" alt="next"></button>
  7. </div>

关于路径

最终打包好的路径会像下图所示,并非如 compile 之后的 dist 目录所示,这是额外要注意的。

打包应用 - 图1

  1. npm install -g asar

如何调试

进入到解压后的 app

这样当有错误的时候,会在终端报出。

开始打包

  1. npm run dist:dir

关于更新的说明

 对于打包,其实我有录制过非常详细的课程在我的个人网站可以寻到,之所以不讲的很细是因为  大多数人更希望知道如何快速吃到螃蟹,而不是烹饪螃蟹 🦀 。

更新在 mac os 需要苹果开发者认证证书签名,所以你需要付费,在 windows 下面可以不用签名。更新会用到一个 的文件,它在 dist 目录下,更新会拉取这个文件判断版本并进行更新,更新相关 API,Electron 内置。更新通常需要一个更新服务器,部署在云端。

打包应用 - 图2