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 463e76cb1abc0c2fdddba4db2ca2e00f7c591e12..d7bc3c35c77b5bf9ec122b38248d0cf1f4d2a548 100644
  8. --- a/lib/internal/modules/esm/load.js
  9. +++ b/lib/internal/modules/esm/load.js
  10. @@ -111,7 +111,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 06b31af80ebbfbf35ec787a94f345775eb512ebf..deca5aa4b8829ba9921440fcb5c285a10e40c8f0 100644
  21. --- a/lib/internal/modules/esm/translators.js
  22. +++ b/lib/internal/modules/esm/translators.js
  23. @@ -354,6 +354,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 1e1a1ea46fc6c1b43cad4038ab0d9cdf21d6ba3d..95e2fa5479ea31559fdb5a2e03515f243b231b75 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. @@ -63,6 +71,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. /**