• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  SafePromiseAllReturnVoid,
5} = primordials;
6
7const { createModuleLoader } = require('internal/modules/esm/loader');
8const { getOptionValue } = require('internal/options');
9const {
10  hasUncaughtExceptionCaptureCallback,
11} = require('internal/process/execution');
12const { kEmptyObject, getCWDURL } = require('internal/util');
13
14let esmLoader;
15
16module.exports = {
17  get esmLoader() {
18    return esmLoader ??= createModuleLoader(true);
19  },
20  async loadESM(callback) {
21    esmLoader ??= createModuleLoader(true);
22    try {
23      const userImports = getOptionValue('--import');
24      if (userImports.length > 0) {
25        const parentURL = getCWDURL().href;
26        await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
27          specifier,
28          parentURL,
29          kEmptyObject,
30        ));
31      } else {
32        esmLoader.forceLoadHooks();
33      }
34      await callback(esmLoader);
35    } catch (err) {
36      if (hasUncaughtExceptionCaptureCallback()) {
37        process._fatalException(err);
38        return;
39      }
40      internalBinding('errors').triggerUncaughtException(
41        err,
42        true, /* fromPromise */
43      );
44    }
45  },
46};
47