1. 程式人生 > >通過 electron 和 electron-packager 生成mac桌面圖示

通過 electron 和 electron-packager 生成mac桌面圖示

最近公司研發了一套內部使用的辦公系統包括移動端+PC端,系統是基於javaWeb開發的,PC端要求製作一個可安裝檔案以方便員工使用(url路徑記著有些麻煩),

於是想到了electron。由於之前用過所以window的exe包很容易就生成了,到了mac時候出現了一些問題........所以記錄下過程.

簡單介紹下:

Electron是由Github開發,用HTML,CSS和JavaScript來構建跨平臺桌面應用程式的一個開源庫。 Electron通過將Chromium和Node.js合併到同一個執行時環境中,並將其打包為Mac,Windows和Linux系統下的應用來實現這一目的。

Electron於2013年作為構建Github上可程式設計的文字編輯器Atom的框架而被開發出來。這兩個專案在2014春季開源。

 

 

 

 因為是基於node的,所以預先要安裝node環境,具體安裝就不說了........

1. 快速生成新專案

 npm init   這時候在目錄下會生成.json檔案,我的檔案package.json

{
"name": "zhiliangeoffice",
"version": "1.0.0",
"description": "www.znzlkj.com",
"main": "main.js",
"scripts": {
"start": "electron .",
"package": "electron-packager . 'Hosts' --platform=darwin --arch=x64 --icon=Icon.icns --out=./dist --asar --app-version=1.0.0",
"packageDarwin": "electron-packager . 'Hosts' --platform=darwin --arch=x64 --icon=Icon.icns --out=./dist --asar --app-version=1.0.0"
},
"author": "zhouy",
"license": "ISC",
"devDependencies": {
"electron": "^3.0.11",
"electron-packager": "^13.0.1"
}
}

 

 

 

 

 

 

 

 

 

 

 2.安裝electron

 npm install --save-dev electron 

 3.建立main.js

const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow

const path = require('path')
const url = require('url')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})

// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))

// Open the DevTools.
// mainWindow.webContents.openDevTools()

// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.最後直接執行命令

npm run-script package

ok,當前路徑下會生成一個dist的資料夾.app應用已經生成