uniapp打包桌面应用

MoMaek

共 2667字,需浏览 6分钟

 · 2021-02-05

uniapp项目打包为桌面应用

一、在项目根目录下安装electron,这里建议使用cnpm,否则会很慢

cnpm install electron --save-dev

二、在项目根目录下安装electron-packager(打包用)

cnpm install electron-packager -g

三、修改uniapp的manifest.json配置文件

  1. 找到h5配置,将运行的基础路径改为./,否则打包出来后会白屏,读取不到,因为打包出来的h5默认加载地址为/static/

  1. 去掉启用https协议前的√,否则会出现网络无法加载,去掉https不影响你请求后端的https协议

四、H5打包

  1. HbuilderX顶部的菜单栏 --> 发行 --> 网站-h5手机版 --> 发行

  1. 发行后的默认目录为:unpackage\dist\build\h5

五、新建配置文件

  1. 发行后的目录新建package.json并添加以下内容

{
"name" : "app-name",
"version" : "0.1.0",
"main" : "main.js"
}
  1. 发行后的目录新建main.js并添加以下内容,本文件就是package.json文件中main键的值,可根据需求修改

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
// 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 win

function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
win.loadURL(url.format({
 pathname: path.join(__dirname, 'index.html'),
 protocol: 'file:',
 slashes: true
}))

// Open the DevTools.
// win.webContents.openDevTools()
// Emitted when the window is closed.

win.on('closed', () => {
 // 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.
 win = 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', () => {
// 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', () => {
// 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 (win === null) {
 createWindow()
}
})

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

ps: 若是你的网页首页文件名不是index.html,那么请在main.js中将index.html修改为你的网页首页

六、打包

  1. 使用cmd外置命令窗口进入H5打包后的目录unpackage\dist\build\h5运行以下命令

electron-packager . 可执行文件的文件名 --win --out 打包成的文件夹名 --arch=x64位还是32位 --electron-version版本号(不是你的h5版本号,是electron版本号) --overwrite --ignore=node_modules
  1. 也可直接复制以下示例命令

electron-packager . MyApp --win --out MyApp --arch=x64 --electron-version 1.4.14 --overwrite --ignore=node_modules
  1. 打包后本目录会出现MyApp目录,进入后找到.exe后缀文件打开后即可看到桌面应用


PS:当网页更新内容后再次使用h5发行前,记得备份package.jsonmain.js


浏览 54
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报