From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Wed, 26 Jul 2023 17:03:15 -0700 Subject: fix: do not resolve electron entrypoints This wastes fs cycles and can result in strange behavior if this path actually exists on disk diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 96869be3c48b84106a5fe2cb752fe979a211d96a..64f8902b1a3d5c335a3b8889b2dc801e8f9ec79c 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -404,6 +404,10 @@ function cjsPreparseModuleExports(filename, source, format) { return { module, exportNames: module[kModuleExportNames] }; } + if (filename === 'electron') { + return { module, exportNames: new SafeSet(['default', ...Object.keys(module.exports)]) }; + } + if (source === undefined) { ({ source } = loadSourceForCJSWithHooks(module, filename, format)); } diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js index d337eeca801b9c187d1513519d7faf27310dbc41..34025f0c2c984b1b9993709cbd00be4158ea1db3 100644 --- a/lib/internal/modules/run_main.js +++ b/lib/internal/modules/run_main.js @@ -2,6 +2,7 @@ const { StringPrototypeEndsWith, + StringPrototypeStartsWith, globalThis, } = primordials; @@ -27,6 +28,13 @@ const { * @returns {string|undefined} */ function resolveMainPath(main) { + // For built-in modules used as the main entry point we _never_ + // want to waste cycles resolving them to file paths on disk + // that actually might exist + if (typeof main === 'string' && StringPrototypeStartsWith(main, 'electron/js2c')) { + return main; + } + /** @type {string} */ let mainPath; // Extension searching for the main entry point is supported for backward compatibility. @@ -50,6 +58,13 @@ function resolveMainPath(main) { * @returns {boolean} */ function shouldUseESMLoader(mainPath) { + // For built-in modules used as the main entry point we _never_ + // want to waste cycles resolving them to file paths on disk + // that actually might exist + if (typeof mainPath === 'string' && StringPrototypeStartsWith(mainPath, 'electron/js2c')) { + return false; + } + /** * @type {string[]} userLoaders A list of custom loaders registered by the user * (or an empty list when none have been registered).