init.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import * as path from 'path';
  2. const Module = require('module');
  3. // We modified the original process.argv to let node.js load the
  4. // init.js, we need to restore it here.
  5. process.argv.splice(1, 1);
  6. // Clear search paths.
  7. require('../common/reset-search-paths');
  8. // Import common settings.
  9. require('@electron/internal/common/init');
  10. // Process command line arguments.
  11. const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line');
  12. // Export node bindings to global.
  13. const { makeRequireFunction } = __non_webpack_require__('internal/modules/cjs/helpers') // eslint-disable-line
  14. global.module = new Module('electron/js2c/worker_init');
  15. global.require = makeRequireFunction(global.module);
  16. // Set the __filename to the path of html file if it is file: protocol.
  17. if (self.location.protocol === 'file:') {
  18. const pathname = process.platform === 'win32' && self.location.pathname[0] === '/' ? self.location.pathname.substr(1) : self.location.pathname;
  19. global.__filename = path.normalize(decodeURIComponent(pathname));
  20. global.__dirname = path.dirname(global.__filename);
  21. // Set module's filename so relative require can work as expected.
  22. global.module.filename = global.__filename;
  23. // Also search for module under the html file.
  24. global.module.paths = Module._nodeModulePaths(global.__dirname);
  25. } else {
  26. // For backwards compatibility we fake these two paths here
  27. global.__filename = path.join(process.resourcesPath, 'electron.asar', 'worker', 'init.js');
  28. global.__dirname = path.join(process.resourcesPath, 'electron.asar', 'worker');
  29. const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
  30. if (appPath) {
  31. // Search for module under the app directory.
  32. global.module.paths = Module._nodeModulePaths(appPath);
  33. }
  34. }