最近在折騰把項目打包成桌面應用程序,發現一個工具electron,能夠講項目打包成一個跨平臺的應用程序,很方便,來學習一下。
一、先安裝electron、electron-packager,安裝方法能夠使用package.json文件配置,而後npm install
也能夠使用cnpm安裝,速度會快點,具體以下:
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm ihtml
package.json以下:web
{ "name": "electron_demo", "version": "1.0.0", "main": "main.js", "scripts": { "start": "electron .", "packager": "electron-packager ./ stockschool --platform=win32 --arch=x64 --icon=./pazq.ico --out=./ElectronApp" }, "devDependencies": { "electron": "~1.7.8", "electron-packager": "^10.1.2" }, "dependencies": {} }
二、安裝完成後,準備好要打包的項目,並增長一個main.js,用來聲明一個相似webview的東西,來加載頁面。npm
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: 1366, height: 727, minWidth: 1366, // Integer (可選) - 窗口的最小寬度, 默認值爲 . minHeight: 727, // Integer (可選) - 窗口的最小高度. 默認值爲 . maxWidth: 1366, // Integer (可選) - 窗口的最大寬度, 默認無限制. maxHeight: 727, // Integer (可選) - 窗口的最大高度, 默認無限制. minimizable: true, // Boolean (可選) - 窗口是否能夠最小化. 在 Linux 中無效. 默認值爲 true. maximizable: false, // Boolean (可選) - 窗口是否能夠最大化動. 在 Linux 中無效. 默認值爲 true. useContentSize: false, // width 和 height 將使用 web 頁面的尺寸 center: true, // Boolean (可選) - 窗口在屏幕居中. }) mainWindow.setMenu(null) // and load the index.html of the app. mainWindow.loadURL(url.format({ pathname: path.join(__dirname, './stockschool/index.html'), protocol: 'file:', slashes: true })) // 加載應用的 index.html // mainWindow.loadURL('file://' + __dirname + '/index.html'); // Open the DevTools. // 打開開發工具 mainWindow.openDevTools(); // 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() } }) // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here.
三、啓動項目:
npm startjson
四、打包,根據平臺打包成一個.exe文件。(導出目錄見 package.json中的out配置項)
打包方法:cnpm run packager
package.json中的打包配置
electron-packager <應用目錄> <應用名稱> <打包平臺> --out <輸出目錄> <架構> <應用版本>windows
electron-packager . HelloWorld --platform=win32 --arch=x64 --icon=./pazq.ico --out=./ElectronApp --version=0.0.1架構