Electron

这是一个可以将HTML打包成桌面程序的工具

打包的方式有多种

  • electron build
  • electron packger
  • electron forge

这边先说比较简单的,关于已知问题及解决方案在下文中有提到

使用electron forge

首先需要去git拉取electron-quick-start

git clone https://github.com/electron/electron-quick-start

将需要打包成exe的项目拷贝至/electron-quick-start下后修改 main.js 中的 mainWindow.loadFile

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

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  // 此处指向入口html
  mainWindow.loadFile('./dist/index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
}

// 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.whenReady().then(() => {
  createWindow()

  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 (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

// 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.

修改package.json,这里主要是依赖和electron-forge的配置

{
  "name": "electron-quick-start",
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "package": "electron-forge package"
  },
  "repository": "https://github.com/electron/electron-quick-start",
  "keywords": [
    "Electron",
    "quick",
    "start",
    "tutorial",
    "demo"
  ],
  "author": "GitHub",
  "license": "CC0-1.0",
  "devDependencies": {
    "@electron-forge/cli": "^6.0.0-beta.61",
    "@electron-forge/maker-deb": "^6.0.0-beta.61",
    "@electron-forge/maker-rpm": "^6.0.0-beta.61",
    "@electron-forge/maker-squirrel": "^6.0.0-beta.61",
    "@electron-forge/maker-zip": "^6.0.0-beta.61",
    "electron": "15.3.0"
  },
  "dependencies": {
    "electron-compile": "^6.4.3",
    "electron-squirrel-startup": "^1.0.0"
  },
  "config": {
    "forge": {
      "packagerConfig": {},
      "makers": [
        {
          "name": "@electron-forge/maker-squirrel",
          "config": {
            "name": "my_electron_app024906"
          }
        },
        {
          "name": "@electron-forge/maker-zip",
          "platforms": [
            "darwin"
          ]
        },
        {
          "name": "@electron-forge/maker-deb",
          "config": {}
        },
        {
          "name": "@electron-forge/maker-rpm",
          "config": {}
        }
      ]
    }
  }
}
// 安装依赖
cnpm i
// 预览
npm run start
// 打包
npm run package

problem

当然,使用期间碰到了很多问题,诸如 read ECONNRESET,连接超时等等

连接超时

连接超时、下载失败及类似的问题可以通过配置环境变量以及修改npm配置文件解决

// 命令行中执行
set ELECTRON_MIRROR="https://npm.taobao.org/mirrors/electron/"
npm配置文件(.npmrc)

该文件一般位于C盘User文件夹下

// 添加如下配置
ELECTRON_MIRROR=https://npm.taobao.org/mirrors/electron/

 


Ex - ploooosion!