main.js 953 B

12345678910111213141516171819202122232425262728293031323334
  1. const { app, BrowserWindow } = require('electron');
  2. const path = require('path');
  3. const url = require('url'); // 引入 url 模块处理路径
  4. function createWindow() {
  5. const win = new BrowserWindow({
  6. width: 1200,
  7. height: 800,
  8. webPreferences: {
  9. nodeIntegration: true,
  10. contextIsolation: false,
  11. },
  12. });
  13. const isDev = !app.isPackaged;
  14. if (isDev) {
  15. win.loadURL('http://localhost:8081');
  16. win.webContents.openDevTools();
  17. } else {
  18. // 生产环境:使用 url.format 构建文件路径
  19. // Electron Forge 会将 dist 文件夹打包进 resources/app.asar
  20. // 所以路径是 ./dist/index.html
  21. win.loadURL(
  22. url.format({
  23. pathname: path.join(__dirname, 'dist','index.html'),
  24. protocol: 'file:',
  25. slashes: true,
  26. })
  27. );
  28. }
  29. }
  30. app.whenReady().then(createWindow);