electron-starter.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const electron = require('electron');
  2. require('./dist/app');
  3. // Module to control application life.
  4. const app = electron.app;
  5. // Module to create native browser window.
  6. const BrowserWindow = electron.BrowserWindow;
  7. const path = require('path');
  8. const url = require('url');
  9. // Keep a global reference of the window object, if you don't, the window will
  10. // be closed automatically when the JavaScript object is garbage collected.
  11. let mainWindow;
  12. function createWindow() {
  13. // Create the browser window.
  14. mainWindow = new BrowserWindow({
  15. width: 1200,
  16. height: 800,
  17. webPreferences: {
  18. devTools: true,
  19. webSecurity: false,
  20. nodeIntegration: true,
  21. enableRemoteModule: true,
  22. },
  23. });
  24. // mainWindow.maximize();
  25. // mainWindow.show();
  26. // and load the index.html of the app.
  27. const startUrl =
  28. process.env.ELECTRON_START_URL ||
  29. url.format({
  30. pathname: path.join(__dirname, './build/index.html'),
  31. protocol: 'file:',
  32. slashes: true,
  33. });
  34. console.log(startUrl);
  35. mainWindow.loadURL(startUrl);
  36. // mainWindow.loadURL('http://127.0.0.1:3000');
  37. mainWindow.focus();
  38. // Open the DevTools.
  39. mainWindow.webContents.openDevTools();
  40. // Emitted when the window is closed.
  41. mainWindow.on('closed', function () {
  42. // Dereference the window object, usually you would store windows
  43. // in an array if your app supports multi windows, this is the time
  44. // when you should delete the corresponding element.
  45. mainWindow = null;
  46. });
  47. }
  48. // This method will be called when Electron has finished
  49. // initialization and is ready to create browser windows.
  50. // Some APIs can only be used after this event occurs.
  51. app.on('ready', createWindow);
  52. // Quit when all windows are closed.
  53. app.on('window-all-closed', function () {
  54. // On OS X it is common for applications and their menu bar
  55. // to stay active until the user quits explicitly with Cmd + Q
  56. if (process.platform !== 'darwin') {
  57. app.quit();
  58. }
  59. });
  60. app.on('activate', function () {
  61. // On OS X it's common to re-create a window in the app when the
  62. // dock icon is clicked and there are no other windows open.
  63. if (mainWindow === null) {
  64. createWindow();
  65. }
  66. });