安裝smart-npm
npm install --global smart-npm --registry=https://registry.npm.taobao.org/
1、安裝electron-builder(注意全局安裝)
cnpm install -g electron-builder
cnpm install -g electron-package
2、在項目目錄(my-project)執行打包命令
electron-builder
3、由於網絡原因,各種包下載不下來,導致出錯
electron-builder 在打包時會檢測cache中是否有electron 包,如果沒有的話會從github上拉去,在國內網絡環境中拉取的過程大概率會失敗,所以你可以自己去下載一個包放到cache目錄裏
例如在macos平臺打包electron應用,執行 electron-builder --mac --x64
➜ clipboard git:(master) ✗ npm run dist
> clipboard@1.0.0 dist /Users/xx/workspace/electron/clipboard
> electron-builder --mac --x64
• electron-builder version=22.3.2 os=18.7.0
• loaded configuration file=package.json ("build" field)
• writing effective config file=dist/builder-effective-config.yaml
• packaging platform=darwin arch=x64 electron=8.0.0 appOutDir=dist/mac
• downloading url=https://github.com/electron/electron/releases/download/v8.0.0/electron-v8.0.0-darwin-x64.zip size=66 MB parts=8
可以單獨下載這個包 https://github.com/electron/electron/releases/download/v8.0.0/electron-v8.0.0-darwin-x64.zip, 放到~/Library/Caches/electron/ 目錄下
各個平臺的目錄地址
Linux: $XDG_CACHE_HOME or ~/.cache/electron/
MacOS: ~/Library/Caches/electron/
Windows: %LOCALAPPDATA%/electron/Cache or ~/AppData/Local/electron/Cache/
常用參數
electron-builder配置文件寫在package.json中的build字段中
"build": {
"appId": "com.example.app", // 應用程序id
"productName": "測試", // 應用名稱
// 設置爲 true 可以把自己的代碼合併並加密
"asar": true,
"directories": {
"buildResources": "build", // 構建資源路徑默認爲build
"output": "dist" // 輸出目錄 默認爲dist
},
"mac": {
"category": "public.app-category.developer-tools", // 應用程序類別
"target": ["dmg", "zip"], // 目標包類型
"icon": "build/icon.icns" // 圖標的路徑
},
"dmg": {
"background": "build/background.tiff or build/background.png", // 背景圖像的路徑
"title": "標題",
"icon": "build/icon.icns" // 圖標路徑
},
"win": {
// 打包成一個獨立的 exe 安裝程序
// 'target': 'nsis',
// 這個意思是打出來32 bit + 64 bit的包,但是要注意:這樣打包出來的安裝包體積比較大,所以建議直接打32的安裝包。
// 'arch': [
// 'x64',
// 'ia32'
// ]
"target": ["nsis","zip"] // 目標包類型
},
"nsis": {
// 是否一鍵安裝,建議爲 false,可以讓用戶點擊下一步、下一步、下一步的形式安裝程序,如果爲true,當用戶雙擊構建好的程序,自動安裝程序並打開,即:一鍵安裝(one-click installer)
"oneClick": false,
// 允許請求提升。 如果爲false,則用戶必須使用提升的權限重新啓動安裝程序。
"allowElevation": true,
// 允許修改安裝目錄,建議爲 true,是否允許用戶改變安裝目錄,默認是不允許
"allowToChangeInstallationDirectory": true,
// 安裝圖標
"installerIcon": "build/installerIcon_120.ico",
// 卸載圖標
"uninstallerIcon": "build/uninstallerIcon_120.ico",
// 安裝時頭部圖標
"installerHeaderIcon": "build/installerHeaderIcon_120.ico",
// 創建桌面圖標
"createDesktopShortcut": true,
// 創建開始菜單圖標
"createStartMenuShortcut": true,
// electron中LICENSE.txt所需要的格式,並非是GBK,或者UTF-8,LICENSE.txt寫好之後,需要進行轉化,轉化爲ANSI
"license": "LICENSE.txt"
},