mirror of
https://github.com/nodejs/node.git
synced 2025-05-08 09:07:45 +00:00

PR-URL: https://github.com/nodejs/node/pull/39240 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
33 lines
900 B
JavaScript
33 lines
900 B
JavaScript
'use strict';
|
|
|
|
const { getOptionValue } = require('internal/options');
|
|
const experimentalImportMetaResolve =
|
|
getOptionValue('--experimental-import-meta-resolve');
|
|
const { PromisePrototypeThen, PromiseReject } = primordials;
|
|
const asyncESM = require('internal/process/esm_loader');
|
|
|
|
function createImportMetaResolve(defaultParentUrl) {
|
|
return async function resolve(specifier, parentUrl = defaultParentUrl) {
|
|
return PromisePrototypeThen(
|
|
asyncESM.esmLoader.resolve(specifier, parentUrl),
|
|
({ url }) => url,
|
|
(error) => (
|
|
error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ?
|
|
error.url : PromiseReject(error))
|
|
);
|
|
};
|
|
}
|
|
|
|
function initializeImportMeta(meta, context) {
|
|
const url = context.url;
|
|
|
|
// Alphabetical
|
|
if (experimentalImportMetaResolve)
|
|
meta.resolve = createImportMetaResolve(url);
|
|
meta.url = url;
|
|
}
|
|
|
|
module.exports = {
|
|
initializeImportMeta
|
|
};
|