Electron打包H5网页为桌面运行程序

一、安装配置环境

  • Electron(一种桌面应用程序运行时),Electron 把 Chromium 和 Node 合并到一个单独的运行时里面,很适合开发桌面 web 形式的应用程序,通过Node它提供了通常浏览器所不能提供的能力。
  • 首先需要在电脑进行安装配置Node环境,下载Nodejs,安装的过程像安装QQ一样简单
  • 通过npm全局安装electron

    npm install electron -g

    这样你就可以在电脑的任意位置进行你想要的操作了

  • 进入你要打包的H5网页的根目录

二、操作项目

  • 进入项目目录需要先在根目录进行创建两个文件,分别为package.json、main.js,这两个文件与你项目的index.html在同一个文件内
  • package.json内的文件内容

    {
      "name": "app-name",
      "version": "0.1.0",
      "main": "main.js"
    }
  • main.js内的内容

    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.
    
  • 下载需要的打包插件工具 electron-packag

    npm install electron-package -g
  • 最后一步进行打包操作,这块比较代码比较长
    electron-package . (生成exe文件的名字) –win –out (打包完文件夹的名字) -arch=×64 –electronVersion (electron的版本号) –overwrite –ignore=node_modules即可完成打包
    例如:我的项目需要进行打包的操作为:

    electron-packager . miaotong --win --out presenterTool --arch=x64 --electronVersion 3.0.4 --overwrite --ignore=node_modules

    注意:–electronVersion的版本号必须和你第一步安装的electron版本一致,如果不确定版本可以输入命令进行查看
    cmd—–>electron -v 终端会输出你当前全局安装electron的版本号,当然这个操作也是你验证electron有没有安装成功的方法

  • 至此就看自己的操作和运气了,上面的长串指令执行完成之后,在你的项目下会生成一个presenterTool文件夹,一级一级点击进去,会看到一个exe文件,点击试试吧

三、存在的问题

  • 目前打包出来的exe文件比较大,这部分还需要一个精简操作,等我找到靠谱的解决办法,再更新一下这个文章吧!

四、上图

《Electron打包H5网页为桌面运行程序》

《Electron打包H5网页为桌面运行程序》

项目展示:

《Electron打包H5网页为桌面运行程序》

    原文作者:焦政
    原文地址: https://segmentfault.com/a/1190000016674369
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞