fix_do_not_resolve_electron_entrypoints.patch 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Samuel Attard <[email protected]>
  3. Date: Wed, 26 Jul 2023 17:03:15 -0700
  4. Subject: fix: do not resolve electron entrypoints
  5. This wastes fs cycles and can result in strange behavior if this path actually exists on disk
  6. diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js
  7. index c9d4a3536d0f60375ae623b48ca2fa7095c88d42..d818320fbbc430d06a0c2852e4723981d6e1a844 100644
  8. --- a/lib/internal/modules/esm/load.js
  9. +++ b/lib/internal/modules/esm/load.js
  10. @@ -109,7 +109,7 @@ async function defaultLoad(url, context = kEmptyObject) {
  11. source = null;
  12. format ??= 'builtin';
  13. } else if (format !== 'commonjs' || defaultType === 'module') {
  14. - if (source == null) {
  15. + if (format !== 'electron' && source == null) {
  16. ({ responseURL, source } = await getSource(urlInstance, context));
  17. context = { __proto__: context, source };
  18. }
  19. diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js
  20. index 0f138b1099b4833176e8a0ce681c745422efc24a..c7dd203e4bd5d611186300d9c0f2255afdf81684 100644
  21. --- a/lib/internal/modules/esm/translators.js
  22. +++ b/lib/internal/modules/esm/translators.js
  23. @@ -287,6 +287,9 @@ function cjsPreparseModuleExports(filename, source) {
  24. if (module && module[kModuleExportNames] !== undefined) {
  25. return { module, exportNames: module[kModuleExportNames] };
  26. }
  27. + if (filename === 'electron') {
  28. + return { module, exportNames: new SafeSet(['default', ...Object.keys(module.exports)]) };
  29. + }
  30. const loaded = Boolean(module);
  31. if (!loaded) {
  32. module = new CJSModule(filename);
  33. diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js
  34. index ab4783a7982b9feb8fa85b62e3e3b181f93309bd..34f91026451d7347ae278712d083e4fe281e50f3 100644
  35. --- a/lib/internal/modules/run_main.js
  36. +++ b/lib/internal/modules/run_main.js
  37. @@ -2,6 +2,7 @@
  38. const {
  39. StringPrototypeEndsWith,
  40. + StringPrototypeStartsWith,
  41. globalThis,
  42. } = primordials;
  43. @@ -26,6 +27,13 @@ const {
  44. * @param {string} main - Entry point path
  45. */
  46. function resolveMainPath(main) {
  47. + // For built-in modules used as the main entry point we _never_
  48. + // want to waste cycles resolving them to file paths on disk
  49. + // that actually might exist
  50. + if (typeof main === 'string' && StringPrototypeStartsWith(main, 'electron/js2c')) {
  51. + return main;
  52. + }
  53. +
  54. const defaultType = getOptionValue('--experimental-default-type');
  55. /** @type {string} */
  56. let mainPath;
  57. @@ -62,6 +70,13 @@ function resolveMainPath(main) {
  58. * @param {string} mainPath - Absolute path to the main entry point
  59. */
  60. function shouldUseESMLoader(mainPath) {
  61. + // For built-in modules used as the main entry point we _never_
  62. + // want to waste cycles resolving them to file paths on disk
  63. + // that actually might exist
  64. + if (typeof mainPath === 'string' && StringPrototypeStartsWith(mainPath, 'electron/js2c')) {
  65. + return false;
  66. + }
  67. +
  68. if (getOptionValue('--experimental-default-type') === 'module') { return true; }
  69. /**