Skip to content

Commit

Permalink
move main functions into pre_execution
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Oct 14, 2019
1 parent fe0c923 commit c750488
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 64 deletions.
60 changes: 60 additions & 0 deletions lib/internal/bootstrap/pre_execution.js
Expand Up @@ -5,6 +5,7 @@ const { Object, SafeWeakMap } = primordials;
const { getOptionValue } = require('internal/options');
const { Buffer } = require('buffer');
const { ERR_MANIFEST_ASSERT_INTEGRITY } = require('internal/errors').codes;
const path = require('path');

function prepareMainThreadExecution(expandArgv1 = false) {
// Patch the process object with legacy properties and normalizations
Expand Down Expand Up @@ -437,11 +438,70 @@ function loadPreloadModules() {
}
}

function resolveMainPath(main) {
const { toRealPath, Module: CJSModule } =
require('internal/modules/cjs/loader');

// Note extension resolution for the main entry point can be deprecated in a
// future major.
let mainPath = CJSModule._findPath(path.resolve(main), null, true);
if (!mainPath)
return;

const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
if (!preserveSymlinksMain)
mainPath = toRealPath(mainPath);

return mainPath;
}

function shouldUseESMLoader(mainPath) {
const experimentalModules = getOptionValue('--experimental-modules');
if (!experimentalModules)
return false;
const userLoader = getOptionValue('--experimental-loader');
if (userLoader)
return true;
// Determine the module format of the main
if (mainPath && mainPath.endsWith('.mjs'))
return true;
if (!mainPath || mainPath.endsWith('.cjs'))
return false;
const { readPackageScope } = require('internal/modules/cjs/loader');
const pkg = readPackageScope(mainPath);
return pkg && pkg.data.type === 'module';
}

function runMainESM(mainPath) {
const esmLoader = require('internal/process/esm_loader');
const { pathToFileURL } = require('internal/url');
const { hasUncaughtExceptionCaptureCallback } =
require('internal/process/execution');
return (esmLoader.initializeLoader() || Promise.resolve()).then(() => {
const main = path.isAbsolute(mainPath) ?
pathToFileURL(mainPath).href : mainPath;
return esmLoader.ESMLoader.import(main).catch((e) => {
if (hasUncaughtExceptionCaptureCallback()) {
process._fatalException(e);
return;
}
internalBinding('errors').triggerUncaughtException(
e,
true /* fromPromise */
);
});
});
}


module.exports = {
patchProcessObject,
resolveMainPath,
runMainESM,
setupCoverageHooks,
setupWarningHandler,
setupDebugEnv,
shouldUseESMLoader,
prepareMainThreadExecution,
initializeDeprecations,
initializeESMLoader,
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/cjs/loader.js
Expand Up @@ -73,7 +73,7 @@ const {
resolveMainPath,
shouldUseESMLoader,
runMainESM
} = require('internal/process/main');
} = require('internal/bootstrap/pre_execution');
const pendingDeprecation = getOptionValue('--pending-deprecation');

module.exports = { wrapSafe, Module, toRealPath, readPackageScope };
Expand Down
61 changes: 0 additions & 61 deletions lib/internal/process/main.js

This file was deleted.

1 change: 0 additions & 1 deletion node.gyp
Expand Up @@ -160,7 +160,6 @@
'lib/internal/priority_queue.js',
'lib/internal/process/esm_loader.js',
'lib/internal/process/execution.js',
'lib/internal/process/main.js',
'lib/internal/process/main_thread_only.js',
'lib/internal/process/per_thread.js',
'lib/internal/process/policy.js',
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-bootstrap-modules.js
Expand Up @@ -51,7 +51,6 @@ const expectedModules = new Set([
'NativeModule internal/options',
'NativeModule internal/priority_queue',
'NativeModule internal/process/execution',
'NativeModule internal/process/main',
'NativeModule internal/process/per_thread',
'NativeModule internal/process/promises',
'NativeModule internal/process/task_queues',
Expand Down

0 comments on commit c750488

Please sign in to comment.