經過 electron 和 electron-packager 生成mac桌面圖標

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

因而想到了electron。因爲以前用過因此window的exe包很容易就生成了,到了mac時候出現了一些問題........因此記錄下過程.java

簡單介紹下:node

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

Electron於2013年做爲構建Github上可編程的文本編輯器Atom的框架而被開發出來。這兩個項目在2014春季開源。npm

 

 

 

 由於是基於node的,因此預先要安裝node環境,具體安裝就不說了........編程

1. 快速生成新項目json

 npm init   這時候在目錄下會生成.json文件,個人文件package.jsonwindows

{
"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"
}
}app

 

 

 

 

 

 

 

 

 

 

 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應用已經生成

相關文章
相關標籤/搜索