uniapp 打包生成EXE文件

一、安装nodejs

二、安装electron(主程序)

npm install electron -g

三、安装electron-packager(打包用)

npm install electron-packager -g

四、uniapp的manifest.json修改

uniapp 打包生成EXE文件_第1张图片

五、h5打包

注意:h5打包不需要输入网站域名,直接打包即可

六、h5文件夹下新建package.json和main.js

uniapp 打包生成EXE文件_第2张图片
package.json

{
  "name"    : "app-name",
  "version" : "0.1.0",
  "main"    : "main.js"
}

main.js

const {app, BrowserWindow,ipcMain  } = require('electron')
const fs=require('fs')
const electron = require('electron')
const path = require('path')
const url = require('url')

// ipcMain.on('asynchronous-message', function(event, arg) {
//    // arg是从渲染进程返回来的数据
//   console.log(arg,1);
  
//   // 这里是传给渲染进程的数据C:/Users/Administrator/Desktop/test.txt
//   fs.readFile(path.join(__dirname,"./test.txt"),"utf8",(err,data)=>{
//   	if(err){
// 		event.sender.send('asynchronous-reply', "读取失败");
// 	}else{
// 		event.sender.send('asynchronous-reply', data);
// 	}
    
//   })
// });

// 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
const Menu = electron.Menu
function createWindow () {
  // Create the browser window.
  Menu.setApplicationMenu(null)
  win = new BrowserWindow({
	  fullscreen: true,
	  webPreferences: {
	  nodeIntegration: true, // 使渲染进程拥有node环境
	  
	  }
	  })

  // and load the index.html of the app.
  win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))
//cpu
	
	
  // 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
  })
  
  // ipcMain.on('asynchronous-message', function(event, arg) {
  //    // arg是从渲染进程返回来的数据
  //   console.log(arg,2);
    
  //   // 这里是传给渲染进程的数据C:/Users/Administrator/Desktop/test.txt
  //   fs.readFile(path.join(__dirname,"./test.txt"),"utf8",(err,data)=>{
  //   	if(err){
  // 		event.sender.send('asynchronous-reply', "读取失败");
  // 	}else{
  // 		event.sender.send('asynchronous-reply', data);
  // 	}
      
  //   })
  // });
}

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

七、electron-packager打包

electron-packager . MyApp --win --out MyApp --arch=x64 --electron-version 12.0.7 --overwrite --ignore=node_modules

总结

借鉴:https://www.cnblogs.com/shangrao/p/14661884.html
**

你可能感兴趣的:(学习记录,好记性不如难笔头,前端,npm,node.js,exe)