首页 > 其他分享 >使用Electron-builder将web项目封装客户端安装包 发布

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

时间:2022-11-06 13:55:24浏览次数:51  
标签:npm web ico true app electron build 安装包 builder

背景:之前用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 <[email protected]>",
  "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给他人装即可

标签:npm,web,ico,true,app,electron,build,安装包,builder
From: https://www.cnblogs.com/qwer123dsada/p/16862489.html

相关文章

  • HBuilder X打包H5网址登录后又跳转登录页
    程序部署在IIS上,用HBuilderX发布H5网站现象:1.程序未更新。2.线上使用的PDA有十几把,只有几把有问题,陆续出现更多的PDA无法登录,昨天都可以登陆,第二天就无法登陆。3.对......
  • webpack5打包出的js在ie11, ie10中报错
    使用webpack5默认打包出来的js文件在ie11,ie10中报错SCRIPT1002:语法错误bundle.js(1,14)。原因和babel设置无关,webpack5中默认打包出来的js文件会用es6语法中的箭头函......
  • 使用nw.js打包以后的web项目 发布客户端
    一、下载nw.js直接前往官网下载即可 https://nwjs.io/downloads/二、封装最简单的客户端nw.js下载完成后,在任意位置新建文件夹,例如nwtest,然后在文件夹中新建两个文件:in......
  • webpack教程
    参考:https://blog.csdn.net/qq_43682422/article/details/124054740官网:https://webpack.js.org/1简介在webpack官网有个图片,形象的展示了webpack的作用  webpac......
  • webpack : 无法加载文件 C:\Users\13573\AppData\Roaming\npm\webpack.ps1,因为
    问题:C:\Users\XXX\AppData\Roaming\npm\webpack.ps1因为在此系统上禁止运行脚本管理员输入  set-ExecutionPolicyRemoteSigned  回车输入A或者Y即可解决 ......
  • webpack : 无法加载文件 C:\Users\13573\AppData\Roaming\npm\webpack.ps1,因为
    问题:C:\Users\XXX\AppData\Roaming\npm\webpack.ps1因为在此系统上禁止运行脚本管理员输入  set-ExecutionPolicyRemoteSigned  回车输入A或者Y即可解决 ......
  • 超好用Web服务端主动推送技术SSE
    1、前端代码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><divid="msg_from_server"></div><divid......
  • webpack5 学习手册
    1.初始化webpack项目npminit-y(初始化一个package.json文件)2.npmiwebpackwebpack-cli-D下载这两个依赖包3.npxwebpack./src/main.js--modedevelopme......
  • web
    title:建站之旅date:{{date}}时间历程2022/11/04入园,打算好好打理博客.2022/11/05做了一天的hexo,终于可以在本地实现界面.荆棘之路fatal:Thecurrentbranch......
  • 不使用hbuilderx创建 基于vue3 + vite的uniapp
    创建以javascript开发的工程npxdegitdcloudio/uni-preset-vue#vitemy-vue3-project创建以typescript开发的工程npxdegitdcloudio/uni-preset-vue#vite-tsmy-......