-
Notifications
You must be signed in to change notification settings - Fork 29.6k
/
esm_loader.js
46 lines (42 loc) · 1.16 KB
/
esm_loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
const {
SafePromiseAllReturnVoid,
} = primordials;
const { createModuleLoader } = require('internal/modules/esm/loader');
const { getOptionValue } = require('internal/options');
const {
hasUncaughtExceptionCaptureCallback,
} = require('internal/process/execution');
const { kEmptyObject, getCWDURL } = require('internal/util');
let esmLoader;
module.exports = {
get esmLoader() {
return esmLoader ??= createModuleLoader(true);
},
async loadESM(callback) {
esmLoader ??= createModuleLoader(true);
try {
const userImports = getOptionValue('--import');
if (userImports.length > 0) {
const parentURL = getCWDURL().href;
await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
specifier,
parentURL,
kEmptyObject,
));
} else {
esmLoader.forceLoadHooks();
}
await callback(esmLoader);
} catch (err) {
if (hasUncaughtExceptionCaptureCallback()) {
process._fatalException(err);
return;
}
internalBinding('errors').triggerUncaughtException(
err,
true, /* fromPromise */
);
}
},
};