index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. (function() {
  2. // Define the window start time before the requires so we get a more accurate
  3. // window:start marker.
  4. const startWindowTime = Date.now();
  5. const electron = require('electron');
  6. const path = require('path');
  7. const Module = require('module');
  8. const getWindowLoadSettings = require('../src/get-window-load-settings');
  9. const { getReleaseChannel } = require('../src/get-app-details.js');
  10. const StartupTime = require('../src/startup-time');
  11. const entryPointDirPath = __dirname;
  12. let blobStore = null;
  13. const startupMarkers = electron.remote.getCurrentWindow().startupMarkers;
  14. if (startupMarkers) {
  15. StartupTime.importData(startupMarkers);
  16. }
  17. StartupTime.addMarker('window:start', startWindowTime);
  18. window.onload = async function() {
  19. try {
  20. StartupTime.addMarker('window:onload:start');
  21. const startTime = Date.now();
  22. await require('second-mate').ready
  23. process.on('unhandledRejection', function(error, promise) {
  24. console.error(
  25. 'Unhandled promise rejection %o with error: %o',
  26. promise,
  27. error
  28. );
  29. });
  30. // Normalize to make sure drive letter case is consistent on Windows
  31. process.resourcesPath = path.normalize(process.resourcesPath);
  32. setupAtomHome();
  33. const devMode =
  34. getWindowLoadSettings().devMode ||
  35. !getWindowLoadSettings().resourcePath.startsWith(
  36. process.resourcesPath + path.sep
  37. );
  38. const FileSystemBlobStore = require('../src/file-system-blob-store');
  39. blobStore = FileSystemBlobStore.load(
  40. path.join(process.env.ATOM_HOME, 'blob-store')
  41. );
  42. const NativeCompileCache = require('../src/native-compile-cache');
  43. NativeCompileCache.setCacheStore(blobStore);
  44. NativeCompileCache.setV8Version(process.versions.v8);
  45. NativeCompileCache.install();
  46. if (getWindowLoadSettings().profileStartup) {
  47. profileStartup(Date.now() - startTime);
  48. } else {
  49. StartupTime.addMarker('window:setup-window:start');
  50. setupWindow().then(() => {
  51. StartupTime.addMarker('window:setup-window:end');
  52. });
  53. setLoadTime(Date.now() - startTime);
  54. }
  55. } catch (error) {
  56. handleSetupError(error);
  57. }
  58. StartupTime.addMarker('window:onload:end');
  59. };
  60. function setLoadTime(loadTime) {
  61. if (global.atom) {
  62. global.atom.loadTime = loadTime;
  63. }
  64. }
  65. function handleSetupError(error) {
  66. const currentWindow = electron.remote.getCurrentWindow();
  67. currentWindow.setSize(800, 600);
  68. currentWindow.center();
  69. currentWindow.show();
  70. currentWindow.openDevTools();
  71. console.error(error.stack || error);
  72. }
  73. function setupWindow() {
  74. const CompileCache = require('../src/compile-cache');
  75. CompileCache.setAtomHomeDirectory(process.env.ATOM_HOME);
  76. CompileCache.install(process.resourcesPath, require);
  77. const ModuleCache = require('../src/module-cache');
  78. ModuleCache.register(getWindowLoadSettings());
  79. require('document-register-element');
  80. const Grim = require('grim');
  81. const documentRegisterElement = document.registerElement;
  82. document.registerElement = (type, options) => {
  83. Grim.deprecate(
  84. 'Use `customElements.define` instead of `document.registerElement` see https://javascript.info/custom-elements'
  85. );
  86. return documentRegisterElement(type, options);
  87. };
  88. const { userSettings, appVersion } = getWindowLoadSettings();
  89. const CSON = require('season');
  90. CSON.setCacheDir(path.join(CompileCache.getCacheDirectory(), 'cson'));
  91. const initScriptPath = path.relative(
  92. entryPointDirPath,
  93. getWindowLoadSettings().windowInitializationScript
  94. );
  95. const initialize = require(initScriptPath);
  96. StartupTime.addMarker('window:initialize:start');
  97. return initialize({ blobStore: blobStore }).then(function() {
  98. StartupTime.addMarker('window:initialize:end');
  99. electron.ipcRenderer.send('window-command', 'window:loaded');
  100. });
  101. }
  102. function profileStartup(initialTime) {
  103. function profile() {
  104. console.profile('startup');
  105. const startTime = Date.now();
  106. setupWindow().then(function() {
  107. setLoadTime(Date.now() - startTime + initialTime);
  108. console.profileEnd('startup');
  109. console.log(
  110. 'Switch to the Profiles tab to view the created startup profile'
  111. );
  112. });
  113. }
  114. const webContents = electron.remote.getCurrentWindow().webContents;
  115. if (webContents.devToolsWebContents) {
  116. profile();
  117. } else {
  118. webContents.once('devtools-opened', () => {
  119. setTimeout(profile, 1000);
  120. });
  121. webContents.openDevTools();
  122. }
  123. }
  124. function setupAtomHome() {
  125. if (process.env.ATOM_HOME) {
  126. return;
  127. }
  128. // Ensure ATOM_HOME is always set before anything else is required
  129. // This is because of a difference in Linux not inherited between browser and render processes
  130. // https://github.com/atom/atom/issues/5412
  131. if (getWindowLoadSettings() && getWindowLoadSettings().atomHome) {
  132. process.env.ATOM_HOME = getWindowLoadSettings().atomHome;
  133. }
  134. }
  135. })();