vue3使用Electron打包成exe的方法与打包报错解决

  // main.js

  // Modules to control application life and create native browser window

  const { app, BrowserWindow,Menu } = require('electron')

  const createWindow = () => {

  // Create the browser window.

  const mainWindow = new BrowserWindow({

  width: 800,

  height: 600,

  webPreferences: {

  nodeIntegration: true, // 启用 Node.js 集成

  contextIsolation: false, // 禁用上下文隔离(Node.js 集成的一个选项)

  webSecurity: false, // 禁用同源策略

  contextIsolation: false,

  session: {

  cookies: true // 这行确保启用了cookie

  }

  },

  icon: 'build/.icon-ico/icon.ico'//这里是自动生成的图标,默认情况下不需要改

  })

  // and load the index.html of the app.

  mainWindow.loadFile('http://www.jb51.net/javascript/dist/index.html')//如果要本地化,这样写,如果写远程的,那这里就是请求的域名

  //隐藏顶部菜单

  // mainWindow.setMenu(null);

  // Open the DevTools.

  // Open the DevTools.

  //mainWindow.webContents.openDevTools()

  mainWindow.maximize();//默认最大化

  }

  //设置中文菜单,默认是英文的

  const createMenu = () => {

  const template = [

  {

  label: '文件',

  submenu: [

  {

  label: '退出',

  accelerator: 'CmdOrCtrl+Q',

  click: () => {

  app.quit();

  }

  }

  ]

  },

  {

  label: '查看',

  submenu: [

  { label: '重新加载', accelerator: 'F5', role: 'reload' },

  { label: '全屏', accelerator: 'F11', role: 'togglefullscreen' },

  { label: '开发者工具', role: 'toggledevtools' }

  ]

  }

  ];

  const menu = Menu.buildFromTemplate(template);

  Menu.setApplicationMenu(menu);

  }

  // 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()

  createMenu()//菜单设置

  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 (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', () => {

  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.