通过 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应用已经生成

猜你喜欢

转载自www.cnblogs.com/zhouy-77253569/p/10118658.html