使用Electron-builder将web项目封装客户端安装包 发布

使用,electron,builder,web,项目,封装,客户端,安装包,发布 · 浏览次数 : 494

小编点评

**步骤 1:配置 npm代理** 在你的 `package.json` 中添加以下两行: ```json "scripts": { "view": "electron .", "build": "electron-builder" } ``` **步骤 2:创建 `main.js` 文件** 创建一个名为 `main.js` 的新文件,并添加以下代码: ```javascript const { app, BrowserWindow, Menu } = require('electron') const path = require('path') const mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { preload: path.join(__dirname, 'preload.js') } }) mainWindow.loadFile('dist/index.html') httpServer.createServer({ root: "./dist" }).listen(80) app.whenReady().then(() => { createWindow() app.on('activate', function () { if (BrowserWindow.getAllWindows().length === 0) createWindow() }) }) app.on('window-all-closed', function () { if (process.platform !== 'darwin') app.quit() }) ``` **步骤 3:创建 `package.json` 文件** 创建一个名为 `package.json` 的新文件,并添加以下内容: ```json { "name": "apiadmin", "version": "4.0.0", "author": "zhaoxiang <zhaoxiang051405@gmail.com>", "private": false, "scripts": { "view": "electron .", "build": "electron-builder" }, "main": "main.js", "build": { "productName": "nullmax", "appId": "com.wss.app", "directories": { "output": "builder" }, "nsis": { "oneClick": false, "allowElevation": true, "allowToChangeInstallationDirectory": true, "installerIcon": "./logo.ico", "uninstallerIcon": "./logo.ico", "installerHeaderIcon": "./logo.ico", "createDesktopShortcut": true, "createStartMenuShortcut": true, "shortcutName": "nullmax" }, "win": { "target": [ "nsis", "zip" ] }, "files": [ "dist/**/*", "main.js" ], "dmg": { "contents": [ { "x": 410, "y": 150, "type": "link", "path": "/Applications" }, { "x": 130, "y": 150, "type": "file" } ] }, "mac": { "icon": "build/icons/icon.icns" }, "win": { "icon": "build/icons/aims.ico", "target": [ { "target": "nsis", "arch": [ "ia32" ] } ] }, "linux": { "icon": "build/icons" } } } ``` **步骤 4:打包并发送** 使用以下命令打包你的项目: ``` npm run build ``` 然后,使用以下命令发送你的软件包: ``` apiadmin Setup 4.0.0.exe ``` 这个命令将创建一个名为 `4.0.0.exe` 的软件包,这是你软件的最终发布版本。

正文

背景:之前用electron-packager将web项目打包成客户端时,exe文件只能在当前文件夹下运行,如果发送给别人使用 极不方便。所以我们可以用electron-builder将web项目封装成安装包给别人使用。

1、配置npm代理

npm set electron_mirror=https://npm.taobao.org/mirrors/electron/
npm set electron-builder-binaries_mirror=https://npm.taobao.org/mirrors/electron-builder-binaries/

2、新建main.js

// Modules to control application life and create native browser window
const {app, BrowserWindow,Menu} = require('electron')
const path = require('path')
//因为项目使用server,添加了这个库,添加前,别忘了使用 npm i http-server 安装库。
const httpServer = require('http-server');


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.
  // mainWindow.loadFile('index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
  //注意 这里是我添加的,去掉electron自带菜单
  Menu.setApplicationMenu(null)
  mainWindow.loadFile('dist/index.html')
  httpServer.createServer({root:"./dist"}).listen(80);

}

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

3、新建package.json

{
  "name": "apiadmin",
  "version": "4.0.0",
  "author": "zhaoxiang <zhaoxiang051405@gmail.com>",
  "private": false,
  "scripts": {
    "view": "electron .",
    "build": "electron-builder"
  },
  "main": "main.js",
  "build": {
    "productName":"nullmax",
    "appId": "com.wss.app",
    "directories": {
      "output": "builder"
    },
    "nsis": {
      "oneClick": false,
      "allowElevation": true,
      "allowToChangeInstallationDirectory": true,
      "installerIcon": "./logo.ico",
      "uninstallerIcon": "./logo.ico",
      "installerHeaderIcon": "./logo.ico",
      "createDesktopShortcut": true,
      "createStartMenuShortcut": true,
      "shortcutName": "nullmax"
    },
    "win": {
      "target": [
        "nsis",
        "zip"
      ]
    },
    "files": [
      "dist/**/*",
      "main.js"
    ]
  },
  "dependencies": {
    "http-server": "^14.1.1"
  }
}

package.json解释: http://www.45fan.com/article.php?aid=1CODrX8UJM4TUTnc

"build": {
    "productName":"xxxx",//项目名 这也是生成的exe文件的前缀名
    "appId": "com.leon.xxxxx",//包名  
    "copyright":"xxxx",//版权  信息
    "directories": { // 输出文件夹
      "output": "build"
    }, 
    "nsis": {
      "oneClick": false, // 是否一键安装
      "allowElevation": true, // 允许请求提升。 如果为false,则用户必须使用提升的权限重新启动安装程序。
      "allowToChangeInstallationDirectory": true, // 允许修改安装目录
      "installerIcon": "./build/icons/aaa.ico",// 安装图标
      "uninstallerIcon": "./build/icons/bbb.ico",//卸载图标
      "installerHeaderIcon": "./build/icons/aaa.ico", // 安装时头部图标
      "createDesktopShortcut": true, // 创建桌面图标
      "createStartMenuShortcut": true,// 创建开始菜单图标
      "shortcutName": "xxxx", // 图标名称
      "include": "build/script/installer.nsh", // 包含的自定义nsis脚本
    },
    "publish": [
      {
        "provider": "generic", // 服务器提供商 也可以是GitHub等等
        "url": "http://xxxxx/" // 服务器地址
      }
    ],
    "files": [
      "dist/electron/**/*"
    ],
    "dmg": {
      "contents": [
        {
          "x": 410,
          "y": 150,
          "type": "link",
          "path": "/Applications"
        },
        {
          "x": 130,
          "y": 150,
          "type": "file"
        }
      ]
    },
    "mac": {
      "icon": "build/icons/icon.icns"
    },
    "win": {
      "icon": "build/icons/aims.ico",
      "target": [
        {
          "target": "nsis",
          "arch": [
            "ia32"
          ]
        }
      ]
    },
    "linux": {
      "icon": "build/icons"
    }
  }

4、打包

npm run build

发送 apiadmin Setup 4.0.0.exe给他人装即可

与使用Electron-builder将web项目封装客户端安装包 发布相似的内容:

使用Electron-builder将web项目封装客户端安装包 发布

背景:之前用electron-packager将web项目打包成客户端时,exe文件只能在当前文件夹下运行,如果发送给别人使用 极不方便。所以我们可以用electron-builder将web项目封装成安装包给别人使用。 1、配置npm代理 npm set electron_mirror=https

Electron Mac 打包报 Error: Exit code: ENOENT. spawn /usr/bin/python ENOENT 解决方法

Electron 项目使用vue-cli-electron-builder创建,原来我的 Mac 上编译都很正常 自从 Mac 升级到 mac OS ventura version 13.0.1 后打包报错,electron-builder 编译 dmg 安装包编译不出来 报如下错误 • build

使用Electron-packager打包已有的web项目,发布客户端

1、先拉electron代码 git clone https://github.com/electron/electron-quick-start 2、将web项目拷贝到electron-quick-start文件夹下 3、更改main.js 更换引用的html const {app, Browse

Nodejs 命令行调用 exec 与 spawn 差异

Nodejs 命令行调用 exec 与 spawn 差异 比如在前端工程项目中 Nodejs 要调用命令行命令如: yarn electron:build exec 调用 yarn 命令,为了能使命令行能实时打印输出正在编译的命令 以异步形式调用 exec 使用 stdout.on 方式监听标准输出

使用EF 连接 数据库 SQLserver、MySql 实现 CodeFirst

1.新建项目,下载Nuget安装包 创建项目需要注意几点,如果是基于 .net framework 的项目 需要选择 相应版本的 EF, 如果是跨平台则选择EF Core版本。 我这里选择的是 .net framework 版本。红框里面是 实现EF Code First 需要的包。 对应的版本:

使用explain优化慢查询的业务场景分析

问:你最害怕的事情是什么? 答:搓澡 问:为什么? 答:因为有些人一旦错过,就不在了 Explain 这个词在不同的上下文中有不同的含义。在数据库查询优化的上下文中,"EXPLAIN" 是一个常用的 SQL 命令,用于显示 SQL 查询的执行计划。执行计划是数据库如何执行查询的一个详细描述,包括它将

使用Github Action来辅助项目管理

Github action 是一个Github官方提供的非常流行且速度集成 持续集成和持续交付(CI/CD)的工具。它允许你在GitHub仓库中自动化、定制和执行你的软件开发工作流。你可以发现、创建和分享用于执行任何你想要的工作的操作,包括CI/CD,并在完全定制的工作流中组合操作。 持续集成需要做

使用 GPU 进行 Lightmap 烘焙 - 简单 demo

作者:i_dovelemon 日期:2024-06-16 主题:Lightmap, PathTracer, Compute Shader 引言 一直以来,我都对离线 bake lightmap 操作很着迷。一方面,这个方案历久弥新,虽然很古老,但是一直在实际项目中都有使用;另一方面,它能够产生非常高

使用 Spring 实现控制反转和依赖注入

使用 Spring 实现控制反转和依赖注入 概述 在本文中,我们将介绍IoC(控制反转)和DI(依赖注入)的概念,以及如何在Spring框架中实现它们。 什么是控制反转? 控制反转是软件工程中的一个原则,它将对象或程序的某些部分的控制权转移给容器或框架。我们最常在面向对象编程的上下文中使用它。 与传

使用Kubesec检查YAML文件安全

使用Kubesec检查YAML文件安全,YAML文件是Kubernetes配置的主要载体,因此,检查YAML文件的安全性对于确保Kubernetes集群的安全至关重要,Kubesec简介,使用Kubesec检查YAML文件安全,kubesec scan podyamlsafe.yaml