Electron打包exe的使用

来源:简书 分类: 文章浏览史 发布时间:2020-06-06 22:13:00 最后更新:2020-06-06 浏览:766
转载声明:
本文为摘录自“简书”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-06-06 22:13:00
关于Electron

Electron是用html,css,JavaScript来构建跨平台桌面应用的一个开源库。Electron通过将Chromium和node合并在同一个运行时环境中,并将其打包为Mac,Windows和linux系统下的应用来实现这一目的。

基于HTML5的跨平台技术比较出名的有 PhoneGap、Cordova,常常用于开发webapp,还有Egret、Cocos-creator、Unity等,常用于开发游戏;还有基于node的nw.js,用于开发桌面应用,以及Electron,一款比nw.js还强大的用网页技术来开发桌面应用的神器。

1. 安装Electron
  1. 安装node
  2. npm全局安装electron
2. 创建目录

找到你前端页面的文件夹,package.json、main.js、index.html三个文件(当然也可以npm init创建),这个index.html就是你的网页首页。

项目结构
├── package.json
├── main.js
└── index.html
3. 添加package内容
{
  "name"    : "app-name",
  "version" : "0.1.0",
  "main"    : "main.js"
}

在main中添加以下内容,这个main.js文件就是上面package.json中的main键的值。

const {app, BrowserWindow} = require('electron')
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 win
 
function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({width: 800, height: 600})
 
  // and load the index.html of the app.
  win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))
 
  // Open the DevTools.
  // win.webContents.openDevTools()
 
  // Emitted when the window is closed.
  win.on('closed', () => {
    // 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.
    win = 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', () => {
  // On macOS 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', () => {
  // On macOS 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 (win === 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.
4. 打开当前目录命令行

再全局安装 npm install electron-packager -g

5. 打包

还在这个目录里直接执行 electron-packager . app --win --out presenterTool --arch=x64 --electron-version 1.0.0 --overwrite --ignore=node_modules
它的意思是
electron-packager . 可执行文件的文件名 --win --out 打包成的文件夹名 --arch=x64位还是32位 --electron-version版本号 --overwrite --ignore=node_modules(网上很多写法是--version 1.0.0 在高版本中的electron中,这个好像不行啦,必须要--electron-version这样写)

6. 打包成功

会生成一个目录,然后打开这个目录,找到exe文件,打开即可。


image.png
7. 其他

或许你会发现几个问题

  1. 打开APP之后发现白屏,有可能报错啦,但是有没有办法调试。调试的方法有很多种,具一种最简单的,就是在main.js中添加这个代码,这个一定要添加在ready这个方法之前。它会监听8315这个端口,然后打开可以看到调试器信息。
const app = require('electron').app;
app.commandLine.appendSwitch('remote-debugging-port', '8315');
app.commandLine.appendSwitch('host-rules', 'MAP * 127.0.0.1');

app.on('ready', function() {
  // Your code here
});
  1. 采用通用方法引入jQuery并不报错,但是不起任何作用,具体表现为"$"符号无法选中dom,原因是node环境中某些变量与jQuery中产生冲突。解决方法
<!-- Insert this line above script imports  -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>

<!-- normal script imports etc  -->
<script src="scripts/jquery.min.js"></script>    
<script src="scripts/vendor.js"></script>    

<!-- Insert this line after script imports -->
<script>if (window.module) module = window.module;</script>

8. 打包成安装包

借鉴https://www.cnblogs.com/kakayang/p/9559777.html

php技术微信