electron-starter.js 2.7 KB

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