electron-starter.js 2.5 KB

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