electron-starter.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. mainWindow.loadURL(startUrl);
  35. // mainWindow.loadURL('http://127.0.0.1:3000');
  36. mainWindow.focus();
  37. // Emitted when the window is closed.
  38. mainWindow.on('closed', function () {
  39. // Dereference the window object, usually you would store windows
  40. // in an array if your app supports multi windows, this is the time
  41. // when you should delete the corresponding element.
  42. mainWindow = null;
  43. });
  44. }
  45. // This method will be called when Electron has finished
  46. // initialization and is ready to create browser windows.
  47. // Some APIs can only be used after this event occurs.
  48. app.on('ready', createWindow);
  49. // Quit when all windows are closed.
  50. app.on('window-all-closed', function () {
  51. // On OS X it is common for applications and their menu bar
  52. // to stay active until the user quits explicitly with Cmd + Q
  53. if (process.platform !== 'darwin') {
  54. app.quit();
  55. }
  56. });
  57. app.on('activate', function () {
  58. // On OS X it's common to re-create a window in the app when the
  59. // dock icon is clicked and there are no other windows open.
  60. if (mainWindow === null) {
  61. createWindow();
  62. }
  63. });