init.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import * as path from 'path';
  2. const Module = require('module') as NodeJS.ModuleInternal;
  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');
  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. // NB. 'self' isn't defined in an AudioWorklet.
  18. if (typeof self !== 'undefined' && self.location.protocol === 'file:') {
  19. const pathname = process.platform === 'win32' && self?.location.pathname[0] === '/' ? self?.location.pathname.substr(1) : self?.location.pathname;
  20. global.__filename = path.normalize(decodeURIComponent(pathname));
  21. global.__dirname = path.dirname(global.__filename);
  22. // Set module's filename so relative require can work as expected.
  23. global.module.filename = global.__filename;
  24. // Also search for module under the html file.
  25. global.module.paths = Module._nodeModulePaths(global.__dirname);
  26. } else {
  27. // For backwards compatibility we fake these two paths here
  28. global.__filename = path.join(process.resourcesPath, 'electron.asar', 'worker', 'init.js');
  29. global.__dirname = path.join(process.resourcesPath, 'electron.asar', 'worker');
  30. const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
  31. if (appPath) {
  32. // Search for module under the app directory.
  33. global.module.paths = Module._nodeModulePaths(appPath);
  34. }
  35. }