init.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. // Import common settings.
  7. require('@electron/internal/common/init');
  8. // Process command line arguments.
  9. const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_command_line');
  10. // Export node bindings to global.
  11. const { makeRequireFunction } = __non_webpack_require__('internal/modules/helpers');
  12. global.module = new Module('electron/js2c/worker_init');
  13. global.require = makeRequireFunction(global.module);
  14. // Set the __filename to the path of html file if it is file: protocol.
  15. // NB. 'self' isn't defined in an AudioWorklet.
  16. if (typeof self !== 'undefined' && self.location.protocol === 'file:') {
  17. const pathname = process.platform === 'win32' && self?.location.pathname[0] === '/' ? self?.location.pathname.substr(1) : self?.location.pathname;
  18. global.__filename = path.normalize(decodeURIComponent(pathname));
  19. global.__dirname = path.dirname(global.__filename);
  20. // Set module's filename so relative require can work as expected.
  21. global.module.filename = global.__filename;
  22. // Also search for module under the html file.
  23. global.module.paths = Module._nodeModulePaths(global.__dirname);
  24. } else {
  25. // For backwards compatibility we fake these two paths here
  26. global.__filename = path.join(process.resourcesPath, 'electron.asar', 'worker', 'init.js');
  27. global.__dirname = path.join(process.resourcesPath, 'electron.asar', 'worker');
  28. const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;
  29. if (appPath) {
  30. // Search for module under the app directory.
  31. global.module.paths = Module._nodeModulePaths(appPath);
  32. }
  33. }