前面用了electron-packager
为实现electron打包,后面发现更多人使用electron-builder
,发现electron-builder
相对electron-packager
更有优势
安装相对比较顺利,一次成功。electron-packager
安装了三次
npm install electron-builder --save-dev
D:\project\electron>npm install electron-builder --save-dev
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.
+ electron-builder@20.38.2
added 151 packages in 192.032s
打包成文件夹及绿色免安装:
//依赖winCodeSign
electron-builder --dir
打包成exe的安装包
//依赖winCodeSign和nsis
electron-builder
electron-builder
参数很多,都必须要packjson里设置,具体可参考https://www.electron.build/configuration/configuration
"scripts": {
"build": "electron-builder",
"build:dir": "electron-builder --dir"
}
执行打包:
如果执行报错,请看最后的问题解决
D:\project\electron>npm run build
> electron-test@1.0.0 build D:\project\electron
> electron-builder
• electron-builder version=20.38.2
• loaded configuration file=package.json ("build" field)
• loaded parent configuration file=D:\project\electron\node_modules\electron-webpack\out\electron-b
• description is missed in the package.json appPackageFile=D:\project\electron\package.json
• author is missed in the package.json appPackageFile=D:\project\electron\package.json
• writing effective config file=dist\builder-effective-config.yaml
• no native production dependencies
• packaging platform=win32 arch=ia32 electron=2.0.7 appOutDir=dist\win-ia32-unpacked
• file source doesn't exist from=D:\project\electron\static
• building target=nsis file=dist\electron-test Setup 1.0.0.exe archs=ia32 oneClick=false perMachine=false
• building block map blockMapFile=dist\electron-test Setup 1.0.0.exe.blockmap
D:\project\electron>
打包后的文件可以执行,但是显示空白。参考(https://www.imqianduan.com/electron/159.html)
下载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
报错代码:
• electron-builder version=20.38.2
• loaded configuration file=package.json ("build" field)
• loaded parent configuration file=D:\project\electron\node_modules\electron-webpack\out\electron-builder.js
• description is missed in the package.json appPackageFile=D:\project\electron\package.json
• author is missed in the package.json appPackageFile=D:\project\electron\package.json
• writing effective config file=dist\builder-effective-config.yaml
• no native production dependencies
• packaging platform=win32 arch=ia32 electron=2.0.7 appOutDir=dist\win-ia32-unpacked
• file source doesn't exist from=D:\project\electron\static
[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
[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
[31m ⨯ [0m exit status 2
github.com/develar/app-builder/pkg/download.DownloadArtifact
/Volumes/data/Documents/app-builder/pkg/download/artifactDownloader.go:121
github.com/develar/app-builder/pkg/download.downloadFromGithub
/Volumes/data/Documents/app-builder/pkg/download/tool.go:62
github.com/develar/app-builder/pkg/download.DownloadWinCodeSign
/Volumes/data/Documents/app-builder/pkg/download/tool.go:57
github.com/develar/app-builder/pkg/download.DownloadArtifact
/Volumes/data/Documents/app-builder/pkg/download/artifactDownloader.go:69
github.com/develar/app-builder/pkg/download.ConfigureArtifactCommand.func1
/Volumes/data/Documents/app-builder/pkg/download/artifactDownloader.go:26
github.com/alecthomas/kingpin.(*actionMixin).applyActions
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/actions.go:28
github.com/alecthomas/kingpin.(*Application).applyActions
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:557
github.com/alecthomas/kingpin.(*Application).execute
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:390
github.com/alecthomas/kingpin.(*Application).Parse
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin@v2.2.6+incompatible/app.go:222
main.main
/Volumes/data/Documents/app-builder/main.go:73
runtime.main
/usr/local/Cellar/go/1.11.2/libexec/src/runtime/proc.go:201
runtime.goexit
/usr/local/Cellar/go/1.11.2/libexec/src/runtime/asm_amd64.s:1333
Error: D:\project\electron\node_modules\app-builder-bin\win\x64\app-builder.exe exited with code 1
at ChildProcess.childProcess.once.code (D:\project\electron\node_modules\builder-util\src\util.ts:244:14)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:925:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
From previous event:
at runCallback (timers.js:794:20)
at tryOnImmediate (timers.js:752:5)
at processImmediate [as _immediateCallback] (timers.js:729:5)
From previous event:
at WinPackager.signAndEditResources (D:\project\electron\node_modules\app-builder-lib\src\winPackager.ts:260:158)
at _bluebirdLst.default.map.file (D:\project\electron\node_modules\app-builder-lib\src\winPackager.ts:363:21)
at go$readdir$cb (D:\project\electron\node_modules\graceful-fs\graceful-fs.js:162:14)
at FSReqWrap.oncomplete (fs.js:135:15)
From previous event:
at D:\project\electron\node_modules\app-builder-lib\src\winPackager.ts:361:27
at Generator.next (<anonymous>)
From previous event:
at WinPackager.signApp (D:\project\electron\node_modules\app-builder-lib\src\winPackager.ts:355:73)
at D:\project\electron\node_modules\app-builder-lib\src\platformPackager.ts:250:16
at Generator.next (<anonymous>)
at runCallback (timers.js:794:20)
at tryOnImmediate (timers.js:752:5)
at processImmediate [as _immediateCallback] (timers.js:729:5)
From previous event:
at WinPackager.doPack (D:\project\electron\node_modules\app-builder-lib\src\platformPackager.ts:167:165)
at D:\project\electron\node_modules\app-builder-lib\src\platformPackager.ts:113:16
at Generator.next (<anonymous>)
From previous event:
at WinPackager.pack (D:\project\electron\node_modules\app-builder-lib\src\platformPackager.ts:111:95)
at D:\project\electron\node_modules\app-builder-lib\src\packager.ts:430:24
at Generator.next (<anonymous>)
at xfs.stat (D:\project\electron\node_modules\fs-extra-p\node_modules\fs-extra\lib\mkdirs\mkdirs.js:56:16)
at D:\project\electron\node_modules\graceful-fs\polyfills.js:285:20
at FSReqWrap.oncomplete (fs.js:153:5)
From previous event:
at Packager.doBuild (D:\project\electron\node_modules\app-builder-lib\src\packager.ts:396:24)
at D:\project\electron\node_modules\app-builder-lib\src\packager.ts:366:57
at Generator.next (<anonymous>)
at D:\project\electron\node_modules\graceful-fs\graceful-fs.js:111:16
at D:\project\electron\node_modules\graceful-fs\graceful-fs.js:45:10
at FSReqWrap.oncomplete (fs.js:135:15)
From previous event:
at Packager._build (D:\project\electron\node_modules\app-builder-lib\src\packager.ts:335:133)
at D:\project\electron\node_modules\app-builder-lib\src\packager.ts:331:23
at Generator.next (<anonymous>)
at runCallback (timers.js:794:20)
at tryOnImmediate (timers.js:752:5)
at processImmediate [as _immediateCallback] (timers.js:729:5)
From previous event:
at Packager.build (D:\project\electron\node_modules\app-builder-lib\src\packager.ts:288:14)
at build (D:\project\electron\node_modules\app-builder-lib\src\index.ts:59:28)
at build (D:\project\electron\node_modules\electron-builder\src\builder.ts:227:10)
at then (D:\project\electron\node_modules\electron-builder\src\cli\cli.ts:46:19)
at <anonymous>
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! electron-builder-start@1.0.0 build: `yarn compile && electron-builder`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the electron-builder-start@1.0.0 build script.
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
electron通过reactjs自定义最大化、最小化、关闭功能
无边框窗口是不带外壳(包括窗口边框、工具栏等),只含有网页内容的窗口。 这些是 BrowserWindow 类上的选项。透明窗口是没有阴影部分。利用css设置自定义区域可拖动。
nsis中文手册及安装
对electron目录调整后,打包白屏?而使用electron-packager打包没问题,什么原因呢?
electron .命令是什么?能换成别的目录吗?当然可以~
electron-builder相对electron-packager有更丰富的的功能,支持更多的平台,同时也支持了自动更新,electron-builder打出的包更为轻量,并且可以打包出不暴露源码的setup安装程序
window下使用 electron-packager将electron打包成exe可执行文件
electron快速开始入门