怎么用 Electron 将网页打包成 exe 可执行文件!


转载自: http://blog.csdn.net/a727911438/article/details/70834467?utm_source=itdadao&utm_medium=referral

 

在 HTML5的崛起、JavaScript要一统天下之际,有一个名为【跨平台】的技术越来越火。为什么会这么火?因为软件开发者只需一次编写程序,即可在 Windows、Linux、Mac、iOSAndroid 等平台运行,大大降低了程序员的工作量,也使公司的产品可以快读迭代。曾经跨平台技术的不被看好,如今随着手机、电脑硬件的发展而快速发展。这一切,几乎由html5技术推动,当然,javascript 这个语言,是最大的功臣。

 

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

 

其实,以上都是废话,现在进入主题:怎么用 Electron 将网页打包成 exe 可执行文件!

 

假设:

1、你已经安装并配置好了 node.js (全局安装)

2、你已经用 npm 安装了 electron (全局安装)

3、你已经写好了前端网页(html、css、javascript 这些,或者基于这些的前端框架写好的网页)

4、以上三点看不懂的,赶紧去百度。。。

 

你如果具备了以上的假设,请继续往下看:

 

1、找到你的前端网页项目文件夹,新建 package.json、main.js、index.html 三个文件(注:其中的 index.html 是你的网页首页)

 

  1.  
    你的项目目录/
  2.  
    ├──  package.json
  3.  
    ├── main.js
  4.  
    └── index.html

 

 

2、在 package.json 中添加如下内容

 

  1.  
    {
  2.  
    "name" : "app-name",
  3.  
    "version" : "0.1.0",
  4.  
    "main" : "main.js"
  5.  
    }

 

 

3、在 main.js 中添加下面的内容,这个 main.js 文件就是上面 package.json 中的 "main"键 的值,所以可根据需要修改

 

  1.  
    const {appBrowserWindowrequire('electron')
  2.  
    const path require('path')
  3.  
    const url require('url')
  4.  
     
  5.  
    // Keep a global reference of the window object, if you don't, the window will
  6.  
    // be closed automatically when the JavaScript object is garbage collected.
  7.  
    let win
  8.  
     
  9.  
    function createWindow () {
  10.  
    // Create the browser window.
  11.  
    win new BrowserWindow({width800height600})
  12.  
     
  13.  
    // and load the index.html of the app.
  14.  
    win.loadURL(url.format({
  15.  
    pathnamepath.join(__dirname'index.html'),
  16.  
    protocol'file:',
  17.  
    slashestrue
  18.  
    }))
  19.  
     
  20.  
    // Open the DevTools.
  21.  
    // win.webContents.openDevTools()
  22.  
     
  23.  
    // Emitted when the window is closed.
  24.  
    win.on('closed'() => {
  25.  
    // Dereference the window object, usually you would store windows
  26.  
    // in an array if your app supports multi windows, this is the time
  27.  
    // when you should delete the corresponding element.
  28.  
    win null
  29.  
    })
  30.  
    }
  31.  
     
  32.  
    // This method will be called when Electron has finished
  33.  
    // initialization and is ready to create browser windows.
  34.  
    // Some APIs can only be used after this event occurs.
  35.  
    app.on('ready'createWindow)
  36.  
     
  37.  
    // Quit when all windows are closed.
  38.  
    app.on('window-all-closed'() => {
  39.  
    // On macOS it is common for applications and their menu bar
  40.  
    // to stay active until the user quits explicitly with Cmd + Q
  41.  
    if (process.platform !== 'darwin'{
  42.  
    app.quit()
  43.  
    }
  44.  
    })
  45.  
     
  46.  
    app.on('activate'() => {
  47.  
    // On macOS it's common to re-create a window in the app when the
  48.  
    // dock icon is clicked and there are no other windows open.
  49.  
    if (win === null{
  50.  
    createWindow()
  51.  
    }
  52.  
    })
  53.  
     
  54.  
    // In this file you can include the rest of your app's specific main process
  55.  
    // code. You can also put them in separate files and require them here.

 

 

4、如果你的网页首页的文件名不是 “index.html”,那么请在 main.js 中将其中的 'index.html' 修改为你的网页首页名

 

5、打开 DOS,cd 到你的项目目录(或直接在你的项目目录下空白的地方 shift+鼠标右键,然后点击在此处打开命令窗口,这里看不懂的,唉,百度吧少年)

 

6、在上一步的 DOS 下,输入 npm install electron-packager -g全局安装我们的打包神器

npm install electron-packager -g

 

7、安装好打包神器后,还是在上一步的 DOS 下,输入 electron-packager . app --win --out presenterTool --arch=x64 --version 1.4.14 --overwrite --ignore=node_modules 即可开始打包

electron-packager . app --win --out presenterTool --arch=x64 --version 1.4.14 --overwrite --ignore=node_modules

 

这个命令什么意思?蓝色部分可自行修改:

electron-packager . 可执行文件的文件名 --win --out 打包成的文件夹名 --arch=x64位还是32位 --version版本号 --overwrite --ignore=node_modules

 

 

8、打包成功后,会生成一个新的文件夹,点进去,找到 exe 文件,双击就可以看到网页变成了一个桌面应用啦!

 

 

 

 

以上是最简单的打包方式,至于怎么修改窗口大小、菜单栏怎么加、怎么调用系统API这些,就给你慢慢去研究Electron了。

 

 

如果你打包总是不成功,觉得很烦,同时对扩展功能没什么要求的话,

 

点击进入我的Coding代码仓库:https://coding.net/u/linhongbijkm/p/Electron-packager-build-project/git

 

里面有我已将内容为 hello,world 的 index.html 网页通过 Electron 框架打包为 windows 环境下的桌面应用。

 

现只需将你的网页前端项目复制到 /resources/app/project 目录下,双击 exe 文件即可以桌面应用的方式运行你的网页。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM