forked from nodejs/node
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesm_loader.js
47 lines (43 loc) · 1.27 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
47
'use strict';
const { createModuleLoader } = require('internal/modules/esm/loader');
const { getOptionValue } = require('internal/options');
const {
hasUncaughtExceptionCaptureCallback,
} = require('internal/process/execution');
const { kEmptyObject, getCWDURL } = require('internal/util');
/** @typedef {ReturnType<createModuleLoader>} ModuleLoader */
/** @type {ModuleLoader} */
let esmLoader;
module.exports = {
get esmLoader() {
return esmLoader ??= createModuleLoader();
},
/**
* @param {(esmLoader: ModuleLoader) => Promise<void>} callback
* @returns {Promise<void>}
*/
async loadESM(callback) {
esmLoader ??= createModuleLoader();
try {
const userImports = getOptionValue('--import');
if (userImports.length > 0) {
const parentURL = getCWDURL().href;
for (let i = 0; i < userImports.length; i++) {
await esmLoader.import(userImports[i], parentURL, kEmptyObject);
}
} else {
esmLoader.forceLoadHooks();
}
await callback(esmLoader);
} catch (err) {
if (hasUncaughtExceptionCaptureCallback()) {
process._fatalException(err);
return;
}
internalBinding('errors').triggerUncaughtException(
err,
true, /* fromPromise */
);
}
},
};