文章目录
首页
Javascript
Html
Css
Node.js
Electron
移动开发
工具类
服务端
浏览器相关
前端收藏
其他

利用electron-builder打包electron成exe

2018年12月10日 发布 阅读(768) 作者:懒炖

前面用了electron-packager为实现electron打包,后面发现更多人使用electron-builder,发现electron-builder相对electron-packager更有优势

特点

  • electron-builder 可以打包成msi、exe、dmg文件,macOS系统,只能打包dmg文件,window系统才能打包exe,msi文件;
  • 几乎支持了所有平台的所有格式;
  • 可以将prepackage目录(手动或使用electron-packager生成的目录)打包成安装包;
  • 支持Auto Update;
  • 非常丰富的选项;
  • 支持CLI和JS API两种使用方式;

安装

安装相对比较顺利,一次成功。electron-packager安装了三次

  1. npm install electron-builder --save-dev
  1. D:\project\electron>npm install electron-builder --save-dev
  2. npm WARN ajv-keywords@3.2.0 requires a peer of ajv@^6.0.0 but none is installed. You must install peer dependencies yourself.
  3. + electron-builder@20.38.2
  4. added 151 packages in 192.032s

electron-builder运行方式

  1. 打包成文件夹及绿色免安装:

    1. //依赖winCodeSign
    2. electron-builder --dir
  2. 打包成exe的安装包

    1. //依赖winCodeSign和nsis
    2. electron-builder

参数

electron-builder参数很多,都必须要packjson里设置,具体可参考https://www.electron.build/configuration/configuration

设置packjson

  1. "scripts": {
  2. "build": "electron-builder",
  3. "build:dir": "electron-builder --dir"
  4. }

执行打包:
如果执行报错,请看最后的问题解决

  1. D:\project\electron>npm run build
  2. > electron-test@1.0.0 build D:\project\electron
  3. > electron-builder
  4. electron-builder version=20.38.2
  5. loaded configuration file=package.json ("build" field)
  6. loaded parent configuration file=D:\project\electron\node_modules\electron-webpack\out\electron-b
  7. description is missed in the package.json appPackageFile=D:\project\electron\package.json
  8. author is missed in the package.json appPackageFile=D:\project\electron\package.json
  9. writing effective config file=dist\builder-effective-config.yaml
  10. no native production dependencies
  11. packaging platform=win32 arch=ia32 electron=2.0.7 appOutDir=dist\win-ia32-unpacked
  12. file source doesn't exist from=D:\project\electron\static
  13. • building target=nsis file=dist\electron-test Setup 1.0.0.exe archs=ia32 oneClick=false perMachine=false
  14. • building block map blockMapFile=dist\electron-test Setup 1.0.0.exe.blockmap
  15. D:\project\electron>

打包后的文件可以执行,但是显示空白。参考(https://www.imqianduan.com/electron/159.html)


问题:打包时,依赖winCodeSign和nsis下载报错

下载https://github.com/electron-userland/electron-builder-binaries/releases/download/winCodeSign-2.4.0/winCodeSign-2.4.0.7z状态还是downloading就报错了,说明winCodeSign下载失败

偿试换了几个npm代理,好像都不行。没办法只能手动下载了~

解决办法:
手动下载winCodeSign,并放置到C:\Users\Administrator\AppData\electron-builder\Cache\winCodeSign目录,如果是公司的域帐号,那Administrator要换成域帐号了,如C:\Users\xxx001000\AppData\Local\electron-builder\Cache
如果你已经安装了electron-builder,那么Cache目录,应该是已经存在的,如果没有就创建一个,注意Cache第一个字母大写

步骤:
(1)、winCodeSign下载:https://github.com/electron-userland/electron-builder-binaries/releases ,如下载winCodeSign-2.4.0
(2)、打开C:\Users\Administrator\AppData\electron-builder\Cache\,新建winCodeSign目录,把下载的解压到里面

如下图,除了winCodeSign-2.4.0,还有其他很多文件夹,说明偿试了很多次下载依赖都失败了,是因为我切换了多个npm代理都不行。不得已才使用这种手动下载的办法

nsis一样的处理,只是解压到C:\Users\xn001829\AppData\Local\electron-builder\Cache\nsis目录,如果我们下载nsis-3.0.3.2,那就是解压到C:\Users\xn001829\AppData\Local\electron-builder\Cache\nsis\nsis-3.0.3.2

报错代码:

  1. electron-builder version=20.38.2
  2. loaded configuration file=package.json ("build" field)
  3. loaded parent configuration file=D:\project\electron\node_modules\electron-webpack\out\electron-builder.js
  4. description is missed in the package.json appPackageFile=D:\project\electron\package.json
  5. author is missed in the package.json appPackageFile=D:\project\electron\package.json
  6. writing effective config file=dist\builder-effective-config.yaml
  7. no native production dependencies
  8. packaging platform=win32 arch=ia32 electron=2.0.7 appOutDir=dist\win-ia32-unpacked
  9. file source doesn't exist from=D:\project\electron\static
  10. [34m • [0m downloading [34mparts [0m=1 [34msize [0m=5.6 MB [34murl [0m=https://github.com/electron-userland/electron-builder-binaries/releases/download/winCodeSign-2.4.0/winCodeSign-2.4.0.7z
  11. [34m • [0m downloaded [34mduration [0m=5.889s [34murl [0m=https://github.com/electron-userland/electron-builder-binaries/releases/download/winCodeSign-2.4.0/winCodeSign-2.4.0.7z
  12. [31m ⨯ [0m exit status 2
  13. github.com/develar/app-builder/pkg/download.DownloadArtifact
  14. /Volumes/data/Documents/app-builder/pkg/download/artifactDownloader.go:121
  15. github.com/develar/app-builder/pkg/download.downloadFromGithub
  16. /Volumes/data/Documents/app-builder/pkg/download/tool.go:62
  17. github.com/develar/app-builder/pkg/download.DownloadWinCodeSign
  18. /Volumes/data/Documents/app-builder/pkg/download/tool.go:57
  19. github.com/develar/app-builder/pkg/download.DownloadArtifact
  20. /Volumes/data/Documents/app-builder/pkg/download/artifactDownloader.go:69
  21. github.com/develar/app-builder/pkg/download.ConfigureArtifactCommand.func1
  22. /Volumes/data/Documents/app-builder/pkg/download/artifactDownloader.go:26
  23. github.com/alecthomas/kingpin.(*actionMixin).applyActions
  24. /Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/actions.go:28
  25. github.com/alecthomas/kingpin.(*Application).applyActions
  26. /Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:557
  27. github.com/alecthomas/kingpin.(*Application).execute
  28. /Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:390
  29. github.com/alecthomas/kingpin.(*Application).Parse
  30. /Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:222
  31. main.main
  32. /Volumes/data/Documents/app-builder/main.go:73
  33. runtime.main
  34. /usr/local/Cellar/go/1.11.2/libexec/src/runtime/proc.go:201
  35. runtime.goexit
  36. /usr/local/Cellar/go/1.11.2/libexec/src/runtime/asm_amd64.s:1333
  37. Error: D:\project\electron\node_modules\app-builder-bin\win\x64\app-builder.exe exited with code 1
  38. at ChildProcess.childProcess.once.code (D:\project\electron\node_modules\builder-util\src\util.ts:244:14)
  39. at Object.onceWrapper (events.js:317:30)
  40. at emitTwo (events.js:126:13)
  41. at ChildProcess.emit (events.js:214:7)
  42. at maybeClose (internal/child_process.js:925:16)
  43. at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
  44. From previous event:
  45. at runCallback (timers.js:794:20)
  46. at tryOnImmediate (timers.js:752:5)
  47. at processImmediate [as _immediateCallback] (timers.js:729:5)
  48. From previous event:
  49. at WinPackager.signAndEditResources (D:\project\electron\node_modules\app-builder-lib\src\winPackager.ts:260:158)
  50. at _bluebirdLst.default.map.file (D:\project\electron\node_modules\app-builder-lib\src\winPackager.ts:363:21)
  51. at go$readdir$cb (D:\project\electron\node_modules\graceful-fs\graceful-fs.js:162:14)
  52. at FSReqWrap.oncomplete (fs.js:135:15)
  53. From previous event:
  54. at D:\project\electron\node_modules\app-builder-lib\src\winPackager.ts:361:27
  55. at Generator.next (<anonymous>)
  56. From previous event:
  57. at WinPackager.signApp (D:\project\electron\node_modules\app-builder-lib\src\winPackager.ts:355:73)
  58. at D:\project\electron\node_modules\app-builder-lib\src\platformPackager.ts:250:16
  59. at Generator.next (<anonymous>)
  60. at runCallback (timers.js:794:20)
  61. at tryOnImmediate (timers.js:752:5)
  62. at processImmediate [as _immediateCallback] (timers.js:729:5)
  63. From previous event:
  64. at WinPackager.doPack (D:\project\electron\node_modules\app-builder-lib\src\platformPackager.ts:167:165)
  65. at D:\project\electron\node_modules\app-builder-lib\src\platformPackager.ts:113:16
  66. at Generator.next (<anonymous>)
  67. From previous event:
  68. at WinPackager.pack (D:\project\electron\node_modules\app-builder-lib\src\platformPackager.ts:111:95)
  69. at D:\project\electron\node_modules\app-builder-lib\src\packager.ts:430:24
  70. at Generator.next (<anonymous>)
  71. at xfs.stat (D:\project\electron\node_modules\fs-extra-p\node_modules\fs-extra\lib\mkdirs\mkdirs.js:56:16)
  72. at D:\project\electron\node_modules\graceful-fs\polyfills.js:285:20
  73. at FSReqWrap.oncomplete (fs.js:153:5)
  74. From previous event:
  75. at Packager.doBuild (D:\project\electron\node_modules\app-builder-lib\src\packager.ts:396:24)
  76. at D:\project\electron\node_modules\app-builder-lib\src\packager.ts:366:57
  77. at Generator.next (<anonymous>)
  78. at D:\project\electron\node_modules\graceful-fs\graceful-fs.js:111:16
  79. at D:\project\electron\node_modules\graceful-fs\graceful-fs.js:45:10
  80. at FSReqWrap.oncomplete (fs.js:135:15)
  81. From previous event:
  82. at Packager._build (D:\project\electron\node_modules\app-builder-lib\src\packager.ts:335:133)
  83. at D:\project\electron\node_modules\app-builder-lib\src\packager.ts:331:23
  84. at Generator.next (<anonymous>)
  85. at runCallback (timers.js:794:20)
  86. at tryOnImmediate (timers.js:752:5)
  87. at processImmediate [as _immediateCallback] (timers.js:729:5)
  88. From previous event:
  89. at Packager.build (D:\project\electron\node_modules\app-builder-lib\src\packager.ts:288:14)
  90. at build (D:\project\electron\node_modules\app-builder-lib\src\index.ts:59:28)
  91. at build (D:\project\electron\node_modules\electron-builder\src\builder.ts:227:10)
  92. at then (D:\project\electron\node_modules\electron-builder\src\cli\cli.ts:46:19)
  93. at <anonymous>
  94. npm ERR! code ELIFECYCLE
  95. npm ERR! errno 1
  96. npm ERR! electron-builder-start@1.0.0 build: `yarn compile && electron-builder`
  97. npm ERR! Exit status 1
  98. npm ERR!
  99. npm ERR! Failed at the electron-builder-start@1.0.0 build script.
  100. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

github: https://github.com/electron-userland/electron-builder
参考: https://www.electron.build/
依赖下载: https://github.com/electron-userland/electron-builder-binaries/releases

版权声明:本站文章除特别声明外,均采用署名-非商业性使用-禁止演绎 4.0 国际 许可协议,如需转载,请注明出处

评论

  •