electron将web应用构建跨平台桌面应用

更新日期: 2020-01-16阅读: 1.9k标签: electron

Electron是由Github开发,用htmlcss和JavaScript来构建跨平台桌面应用程序的一个开源库。 Electron通过将Chromium和Node.js合并到同一个运行时环境中,并将其打包为Mac,Windows和Linux系统下的应用来实现这一目的。


本文利用vue-cli快速搭建一个项目说明怎么将一个web应用打包构建成桌面应用的过程


开始

使用npm init初始化项目,配置依赖electron,electron-builder(打包组件),这里如果安装electron或者启动时报错,可以删除node_modules中electron的使用npm i electron@+指定版本即可

{
  "name": "electron-quick-start",
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "build_win": "electron-builder --win --x64",
    "build_mac": "electron-builder --mac --x64"
  },
  "build": {
    "productName": "vuecli"
  },
  "repository": "https://github.com/electron/electron-quick-start",
  "keywords": [
    "Electron",
    "quick",
    "start",
    "tutorial",
    "demo"
  ],
  "author": "GitHub",
  "license": "CC0-1.0",
  "devDependencies": {
    "electron": "^3.1.0",
    "electron-builder": "^21.2.0"
  }
}

package.json需要注意的参数: :项目入口文件,文件用于配置启动所需参数 scripts:启动命令

  • start:开发启动命令
  • build_win:构建win桌面应用
  • build_mac:构建macos桌面应用 build:构建应用参数,所有参数参考electron-build
  • productName:应用名称
  • output:产出目录


main.js

官方例子

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')

// 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,
    webPreferences: {
      webSecurity: false
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  // 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 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', function () {
  // 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 (mainWindow === null) createWindow()
})

BrowserWindow浏览器代理,设置窗口信息等,mainWindow.loadFile('index.html')引入主页文件,webSecurity设置false可忽略浏览器跨域等,该设置可禁止浏览器同源策略,只能在测试环境使用,如需上线正式环境需另外配置网络代理


静态文件准备

http请求

在测试环境下,如在main.js中设置了webSecurity为false,接口请求地址可设置为绝对路径(此设置忽略跨域),生产环境需另配网络转发。

路由

需修改的相关静态文件地址为相对路径,vue.config.js配置如下:

module.exports = {
      assetsDir: 'static',
      publicPath: './'
};

启动npm run build开始打包,打包后的文件在dist中,dist文件夹下的文件全部拷贝到electron的初始化项目的根目录下,运行npm start即可正常访问,npm run build_mac开发打包,完成后在根目录的dist下生成vuecli-1.0.0.dmg的桌面应用安装包

原文:http://zhaoqingweb.com/article/detail/qjvrrfvm30kiu9bc3


链接: https://www.fly63.com/article/detial/7319

使用 Electron 打包 Vue 项目

新建一个 Vue 项目,安装 electron-builder,Electron 的main进程和renderer进程分开进行开发,可以通过ipcMain和ipcRenderer进行通信,数据流清晰,开发简单。

electron阻止应用关闭

这里直接监听onbeforeunload事件,每当页面刷新或关闭时,都会触发这个事件。方法二:我们在main.js中监听close事件。定义一个flag标识是否可以关闭。如果不可以关闭,则阻止该事件。

如何在Electron中调用Dll

客户端有些硬件的接口需要调试,是在电脑上连了一些硬件的设备,比如打印机、扫描仪或者进行串口通信等等。单靠JS是完成不了了,我们决定通过把C++或者C#把这些功能打包成Dll

electron_pcMain模块、ipcRenderer模块

ipcMain模块是EventEmitter类的一个实例。 当在主进程中使用时,它处理从渲染器进程(网页)发送出来的异步和同步信息。 从渲染器进程发送的消息将被发送到该模块。

Electron怎么启动并行的子任务

有些场景下比如要处理一大堆文件内容的查找,字符串的替换,文件的修改等一系列耗时操作的时候,如果放在主进程执行那必然会导致渲染进程的阻塞

Electron Windows增加托盘悬浮框功能

在做Electron Windows 桌面应用时候,做鼠标悬浮到托盘图标上时显示一个悬浮框(例如做消息提醒),但因为Windows没有提供托盘mouse-enter/mouse-leave事件,无法直接做这个功能,考虑到还有mouse-move事件,弄个间接的方式实现。

Electron应用打包、自动升级

使用electron builder打包只需要在vue.config.js中配置即可,这里需要注意的是,默认情况下electron builder打包出来的安装程序是不能修改安装目录的,需要allowToChangeInstallationDirectory这个配置设置为true

基于Electron开发Hosts切换工具的“踩坑”之旅

用过好几个Hosts切换工具,但总是有点这样那样的问题。最讨厌的莫过于切换完后,键盘都快按坏了,浏览器里面的Hosts就是不变,网上找了好多方法,但是感觉都并不完美,于是就有了这篇文章。

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!