【Electron】使用Electron将web项目打包成桌面应用程序

清疚 2022-09-05 01:41 819阅读 0赞

目录

  • 一、所需环境&打包前准备
      • 1、安装node.js
      • 2、安装electron
      • 3、web项目
  • 二、打包过程
      • 1、打包配置
      • 2、 安装打包器
      • 3、执行打包命令:

Electron是由GitHub开发,使用 JavaScript,HTML 和 CSS 构建跨平台的桌面应用程序,可以帮我们把web网页项目直接打包成桌面应用程序。

一、所需环境&打包前准备

1、安装node.js

electron 依赖于node.js 需要安装node.js:
安装node(步骤,不是重点,略过)

2、安装electron

dos下,全局安装:

  1. npm install -g electron

检查是否安装成功:

  1. electron -v

3、web项目

二、打包过程

1、打包配置

在需要打包的web网页项目根目录下分别新建:main.js、package.json

package.json:在package.json中加入如下内容

  1. {
  2. "name" : "App", // 项目名称
  3. "version" : "0.1.0",
  4. "main" : "main.js" // 根目录下的main.js
  5. }

main.js:在main.js中加入如下代码

  1. const {
  2. app, BrowserWindow} = require('electron')
  3. const path = require('path')
  4. const url = require('url')
  5. // Keep a global reference of the window object, if you don't, the window will
  6. // be closed automatically when the JavaScript object is garbage collected.
  7. let win
  8. function createWindow () {
  9. // Create the browser window.
  10. win = new BrowserWindow({
  11. width: 800, height: 600})
  12. // and load the index.html of the app.
  13. win.loadURL(url.format({
  14. pathname: path.join(__dirname, 'index.html'),
  15. protocol: 'file:',
  16. slashes: true
  17. }))
  18. // Open the DevTools.
  19. // win.webContents.openDevTools()
  20. // Emitted when the window is closed.
  21. win.on('closed', () => {
  22. // Dereference the window object, usually you would store windows
  23. // in an array if your app supports multi windows, this is the time
  24. // when you should delete the corresponding element.
  25. win = null
  26. })
  27. }
  28. // This method will be called when Electron has finished
  29. // initialization and is ready to create browser windows.
  30. // Some APIs can only be used after this event occurs.
  31. app.on('ready', createWindow)
  32. // Quit when all windows are closed.
  33. app.on('window-all-closed', () => {
  34. // On macOS it is common for applications and their menu bar
  35. // to stay active until the user quits explicitly with Cmd + Q
  36. if (process.platform !== 'darwin') {
  37. app.quit()
  38. }
  39. })
  40. app.on('activate', () => {
  41. // On macOS it's common to re-create a window in the app when the
  42. // dock icon is clicked and there are no other windows open.
  43. if (win === null) {
  44. createWindow()
  45. }
  46. })
  47. // In this file you can include the rest of your app's specific main process
  48. // code. You can also put them in separate files and require them here.

注意:在main.js 中的index.html 为你web项目的首页,及入口文件
请添加图片描述

2、 安装打包器

打开 DOS窗口,cd 到你的项目目录下,输入下面命令,全局安装electron打包器

  1. npm install electron-packager -g

3、执行打包命令:

在根目录下执行打包命令:

  1. electron-packager . app --win --out App --arch=x64 --electron-version 1.4.14 --overwrite --ignore=node_modules

请添加图片描述

打包成功后在根目录下会生成打包好的工程:
请添加图片描述

打开工程中的应用程序即可运行web项目:
请添加图片描述

请添加图片描述

发表评论

表情:
评论列表 (有 0 条评论,819人围观)

还没有评论,来说两句吧...

相关阅读