electron-starter.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // get system lang
  36. mainWindow.webContents.executeJavaScript(`
  37. localStorage.setItem('lang', '${app.getLocale()}');
  38. `);
  39. // mainWindow.maximize();
  40. // mainWindow.show();
  41. // and load the index.html of the app.
  42. const startUrl =
  43. process.env.ELECTRON_START_URL ||
  44. url.format({
  45. pathname: path.join(__dirname, './build/index.html'),
  46. protocol: 'file:',
  47. slashes: true,
  48. });
  49. mainWindow.loadURL(startUrl);
  50. // mainWindow.loadURL('http://127.0.0.1:3000');
  51. mainWindow.focus();
  52. // Emitted when the window is closed.
  53. mainWindow.on('closed', function () {
  54. // Dereference the window object, usually you would store windows
  55. // in an array if your app supports multi windows, this is the time
  56. // when you should delete the corresponding element.
  57. mainWindow = null;
  58. });
  59. }
  60. // This method will be called when Electron has finished
  61. // initialization and is ready to create browser windows.
  62. // Some APIs can only be used after this event occurs.
  63. app.on('ready', createWindow);
  64. // Quit when all windows are closed.
  65. app.on('window-all-closed', function () {
  66. // On OS X it is common for applications and their menu bar
  67. // to stay active until the user quits explicitly with Cmd + Q
  68. if (process.platform !== 'darwin') {
  69. app.quit();
  70. }
  71. });
  72. app.on('activate', function () {
  73. // On OS X it's common to re-create a window in the app when the
  74. // dock icon is clicked and there are no other windows open.
  75. if (mainWindow === null) {
  76. createWindow();
  77. }
  78. });