-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
25 lines (23 loc) · 885 Bytes
/
index.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
const Module = require('module');
const {isAbsolute} = require('path');
const {pathToFileURL} = require('url');
exports.dynamicImport = importEsm;
exports.importEsm = importEsm;
async function importEsm(specifier, module) {
if(isAbsolute(specifier)) {
return import(pathToFileURL(specifier).href);
}
let resolvedPath;
try {
const req = Module.createRequire(module.filename);
try {
resolvedPath = req.resolve(Path.posix.join(specifier, 'package.json'));
} catch {
resolvedPath = req.resolve(specifier);
}
resolvedPath = pathToFileURL(resolvedPath).href;
} catch {
throw new Error(`Unable to locate module "${specifier}" relative to "${module?.filename}" using the CommonJS resolver. Consider passing an absolute path to the target module.`);
}
return import(resolvedPath);
}