@@ -9,19 +9,15 @@ const path = require('path');
9
9
10
10
/**
11
11
* Get the absolute path to the main entry point.
12
+ * Only used in `--experimental-default-type=commonjs` mode.
12
13
* @param {string } main - Entry point path
13
14
*/
14
15
function resolveMainPath ( main ) {
15
- /** @type {string } */
16
- let mainPath ;
17
- if ( getOptionValue ( '--experimental-default-type' ) === 'module' ) {
18
- mainPath = path . resolve ( main ) ;
19
- } else {
20
- // Extension searching for the main entry point is supported only in legacy mode.
21
- // Module._findPath is monkey-patchable here.
22
- const { Module } = require ( 'internal/modules/cjs/loader' ) ;
23
- mainPath = Module . _findPath ( path . resolve ( main ) , null , true ) ;
24
- }
16
+ // Note extension resolution for the main entry point can be deprecated in a
17
+ // future major.
18
+ // Module._findPath is monkey-patchable here.
19
+ const { Module } = require ( 'internal/modules/cjs/loader' ) ;
20
+ let mainPath = Module . _findPath ( path . resolve ( main ) , null , true ) ;
25
21
if ( ! mainPath ) { return ; }
26
22
27
23
const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
@@ -35,11 +31,10 @@ function resolveMainPath(main) {
35
31
36
32
/**
37
33
* Determine whether the main entry point should be loaded through the ESM Loader.
34
+ * Only used in `--experimental-default-type=commonjs` mode.
38
35
* @param {string } mainPath - Absolute path to the main entry point
39
36
*/
40
37
function shouldUseESMLoader ( mainPath ) {
41
- if ( getOptionValue ( '--experimental-default-type' ) === 'module' ) { return true ; }
42
-
43
38
/**
44
39
* @type {string[] } userLoaders A list of custom loaders registered by the user
45
40
* (or an empty list when none have been registered).
@@ -64,7 +59,7 @@ function shouldUseESMLoader(mainPath) {
64
59
65
60
/**
66
61
* Run the main entry point through the ESM Loader.
67
- * @param {string } mainPath - Absolute path for the main entry point
62
+ * @param {string } mainPath - Path to the main entry point, either absolute or relative to CWD
68
63
*/
69
64
function runMainESM ( mainPath ) {
70
65
const { loadESM } = require ( 'internal/process/esm_loader' ) ;
@@ -102,6 +97,11 @@ async function handleMainPromise(promise) {
102
97
* @param {string } main - Resolved absolute path for the main entry point, if found
103
98
*/
104
99
function executeUserEntryPoint ( main = process . argv [ 1 ] ) {
100
+ if ( getOptionValue ( '--experimental-default-type' ) === 'module' ) {
101
+ runMainESM ( main ) ;
102
+ return ;
103
+ }
104
+
105
105
const resolvedMain = resolveMainPath ( main ) ;
106
106
const useESMLoader = shouldUseESMLoader ( resolvedMain ) ;
107
107
if ( useESMLoader ) {
0 commit comments