Skip to content

Commit

Permalink
Streamline
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Oct 1, 2023
1 parent 9688f53 commit 6c6aefe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ const path = require('path');

/**
* Get the absolute path to the main entry point.
* Only used in `--experimental-default-type=commonjs` mode.
* @param {string} main - Entry point path
*/
function resolveMainPath(main) {
/** @type {string} */
let mainPath;
if (getOptionValue('--experimental-default-type') === 'module') {
mainPath = path.resolve(main);
} else {
// Extension searching for the main entry point is supported only in legacy mode.
// Module._findPath is monkey-patchable here.
const { Module } = require('internal/modules/cjs/loader');
mainPath = Module._findPath(path.resolve(main), null, true);
}
// Note extension resolution for the main entry point can be deprecated in a
// future major.
// Module._findPath is monkey-patchable here.
const { Module } = require('internal/modules/cjs/loader');
let mainPath = Module._findPath(path.resolve(main), null, true);
if (!mainPath) { return; }

const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
Expand All @@ -35,11 +31,10 @@ function resolveMainPath(main) {

/**
* Determine whether the main entry point should be loaded through the ESM Loader.
* Only used in `--experimental-default-type=commonjs` mode.
* @param {string} mainPath - Absolute path to the main entry point
*/
function shouldUseESMLoader(mainPath) {
if (getOptionValue('--experimental-default-type') === 'module') { return true; }

/**
* @type {string[]} userLoaders A list of custom loaders registered by the user
* (or an empty list when none have been registered).
Expand All @@ -64,7 +59,7 @@ function shouldUseESMLoader(mainPath) {

/**
* Run the main entry point through the ESM Loader.
* @param {string} mainPath - Absolute path for the main entry point
* @param {string} mainPath - Path to the main entry point, either absolute or relative to CWD
*/
function runMainESM(mainPath) {
const { loadESM } = require('internal/process/esm_loader');
Expand Down Expand Up @@ -102,6 +97,11 @@ async function handleMainPromise(promise) {
* @param {string} main - Resolved absolute path for the main entry point, if found
*/
function executeUserEntryPoint(main = process.argv[1]) {
if (getOptionValue('--experimental-default-type') === 'module') {
runMainESM(main);
return;
}

const resolvedMain = resolveMainPath(main);
const useESMLoader = shouldUseESMLoader(resolvedMain);
if (useESMLoader) {
Expand Down
2 changes: 1 addition & 1 deletion test/es-module/test-esm-type-flag-errors.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('--experimental-default-type=module should not support extension search
cwd: fixtures.path('es-modules/package-without-type'),
});

match(stderr, /ENOENT/);
match(stderr, /ERR_MODULE_NOT_FOUND/);
strictEqual(stdout, '');
strictEqual(code, 1);
strictEqual(signal, null);
Expand Down

0 comments on commit 6c6aefe

Please sign in to comment.