All files / lib/internal/modules/esm initialize_import_meta.js

100% Statements 43/43
100% Branches 9/9
100% Functions 3/3
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 44144x 144x 144x 144x 144x 144x 144x 3x 3x 18x 18x 18x 18x 18x 15x 4x 15x 11x 11x 15x 7x 7x 3x 3x 144x 144x 144x 144x 144x 154x 154x 154x 154x 154x 3x 3x 154x 154x 154x 144x 144x 144x 144x  
'use strict';
 
const { getOptionValue } = require('internal/options');
const experimentalImportMetaResolve =
  getOptionValue('--experimental-import-meta-resolve');
const asyncESM = require('internal/process/esm_loader');
 
function createImportMetaResolve(defaultParentUrl) {
  return function resolve(specifier, parentUrl = defaultParentUrl) {
    let url;
 
    try {
      ({ url } = asyncESM.esmLoader.resolve(specifier, parentUrl));
    } catch (error) {
      if (error.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
        ({ url } = error);
      } else {
        throw error;
      }
    }
 
    return url;
  };
}
 
/**
 * @param {object} meta
 * @param {{url: string}} context
 */
function initializeImportMeta(meta, context) {
  const { url } = context;
 
  // Alphabetical
  if (experimentalImportMetaResolve) {
    meta.resolve = createImportMetaResolve(url);
  }
 
  meta.url = url;
}
 
module.exports = {
  initializeImportMeta
};