src/index.js
function adaptModulePath(modulePath, state) {
  // const fileName = state.file.opts.filename;
  const options = getStateOptions(state);
  const filesMap = createFilesMap(options);
  const rootPath = getRootPath(options);

  let module = determineContext(modulePath, options);

  // Do not generate infinite cyrcular references on empty nodes
  if (!module.file) {
    return null;
  }

  // Safeguard against circular calls
  if (lastIn === lastOut) {
    return null;
  }

  // Remove relative path prefix before replace
  const absoluteModule = path.isAbsolute(module.file);

  // Try to replace aliased module
  let found = false;
  let constantModulePart;
  filesMap.keys.filter((k) => {
    const d = filesMap.contents[k];
    let idx = module.file.search(d.regexp);

    if (!found && idx > -1) {
      // throw `Found ${d} in ${module.file}`
      // constantModulePart = module.file.slice(0, idx) || './';
      constantModulePart = './';

      if (module.file[idx] === '/') {
        idx += 1;
      }

      const value = d.value[0] === '.' && idx > 0 ? d.value.slice(2) : d.value;
      // const value = d.value;

      // Replace the alias with it's path and continue
      module.file = `${module.file.slice(0, idx) || ''}${value}${module.file.slice(idx + d.expose.length) || ''}`;
      found = true;


      // Revaluate npm and react flags based on the new mapping
      module = determineContext(module.file, options);

      return true;
    }

    return false;
  });

  // Leave NPM modules as resolved, do not remap and ignore wrongfully formatted strings of form require('npm:')
  if (module.npm) {
    return module.file || null;
  }

  // Do not touch direct requires to npm modules (non annotated)
  if (module.file.indexOf('./') !== 0 && module.file.indexOf('/') !== 0) {
    return null;
  }

  // Check if any substitution took place
  if (found) {
    // Module alias substituted
    if (!path.isAbsolute(module.file)) {
      if (!absoluteModule && module.file[0] !== '.') {
        // Add the relative notation back
        module.file = `./${module.file}`;
      }
    }
  }

  // Do not break node_modules required by name
  if (!found && module.file[0] !== '.') {
    if (reactOsFileInfer(options)) {
      const aux2 = mapForReact(module.file);

      if (aux2 !== module.file) {
        return aux2;
      }
    }

    return null;
  }

  // Enforce absolute paths on absolute mode
  if (rootPath) {
    if (!path.isAbsolute(module.file)) {
      module.file = path.join(rootPath, module.file);

      if (reactOsFileInfer(options)) {
        return mapForReact(module.file);
      }

      return module.file;
    }

    // After the node is replaced the visitor will be called again.
    // Without this condition these functions will generate a circular loop.
    return null;
  }

  // throw `Gets here: ${JSON.stringify(module.file)}`;

  // Do not bother with relative paths that are not aliased
  if (!found) {
    return null;
  }

  let moduleMapped = mapToRelative(module.file, constantModulePart, options);
  if (moduleMapped.indexOf('./') !== 0) {
    moduleMapped = `./${moduleMapped}`;
  }

  // throw JSON.stringify({
  //   moduleMapped, modulePath, module, constantModulePart
  // }, null, 2);
  return moduleMapped !== modulePath ? moduleMapped : null;
}
function adaptModulePath(modulePath, state) {
  // const fileName = state.file.opts.filename;
  const options = getStateOptions(state);
  const filesMap = createFilesMap(options);
  const rootPath = getRootPath(options);

  let module = determineContext(modulePath, options);

  // Do not generate infinite cyrcular references on empty nodes
  if (!module.file) {
    return null;
  }

  // Safeguard against circular calls
  if (lastIn === lastOut) {
    return null;
  }

  // Remove relative path prefix before replace
  const absoluteModule = path.isAbsolute(module.file);

  // Try to replace aliased module
  let found = false;
  let constantModulePart;
  filesMap.keys.filter((k) => {
    const d = filesMap.contents[k];
    let idx = module.file.search(d.regexp);

    if (!found && idx > -1) {
      // throw `Found ${d} in ${module.file}`
      // constantModulePart = module.file.slice(0, idx) || './';
      constantModulePart = './';

      if (module.file[idx] === '/') {
        idx += 1;
      }

      const value = d.value[0] === '.' && idx > 0 ? d.value.slice(2) : d.value;
      // const value = d.value;

      // Replace the alias with it's path and continue
      module.file = `${module.file.slice(0, idx) || ''}${value}${module.file.slice(idx + d.expose.length) || ''}`;
      found = true;


      // Revaluate npm and react flags based on the new mapping
      module = determineContext(module.file, options);

      return true;
    }

    return false;
  });

  // Leave NPM modules as resolved, do not remap and ignore wrongfully formatted strings of form require('npm:')
  if (module.npm) {
    return module.file || null;
  }

  // Do not touch direct requires to npm modules (non annotated)
  if (module.file.indexOf('./') !== 0 && module.file.indexOf('/') !== 0) {
    return null;
  }

  // Check if any substitution took place
  if (found) {
    // Module alias substituted
    if (!path.isAbsolute(module.file)) {
      if (!absoluteModule && module.file[0] !== '.') {
        // Add the relative notation back
        module.file = `./${module.file}`;
      }
    }
  }

  // Do not break node_modules required by name
  if (!found && module.file[0] !== '.') {
    if (reactOsFileInfer(options)) {
      const aux2 = mapForReact(module.file);

      if (aux2 !== module.file) {
        return aux2;
      }
    }

    return null;
  }

  // Enforce absolute paths on absolute mode
  if (rootPath) {
    if (!path.isAbsolute(module.file)) {
      module.file = path.join(rootPath, module.file);

      if (reactOsFileInfer(options)) {
        return mapForReact(module.file);
      }

      return module.file;
    }

    // After the node is replaced the visitor will be called again.
    // Without this condition these functions will generate a circular loop.
    return null;
  }

  // throw `Gets here: ${JSON.stringify(module.file)}`;

  // Do not bother with relative paths that are not aliased
  if (!found) {
    return null;
  }

  let moduleMapped = mapToRelative(module.file, constantModulePart, options);
  if (moduleMapped.indexOf('./') !== 0) {
    moduleMapped = `./${moduleMapped}`;
  }

  // throw JSON.stringify({
  //   moduleMapped, modulePath, module, constantModulePart
  // }, null, 2);
  return moduleMapped !== modulePath ? moduleMapped : null;
}
function adaptModulePath ( modulePath , state ) { // const fileName = state.file.opts.filename; const options = getStateOptions ( state ) ; const filesMap = createFilesMap ( options ) ; const rootPath = getRootPath ( options ) ; let module = determineContext ( modulePath , options ) ; // Do not generate infinite cyrcular references on empty nodes if ( ! module . file ) { return null ; } // Safeguard against circular calls if ( lastIn === lastOut ) { return null ; } // Remove relative path prefix before replace const absoluteModule = path . isAbsolute ( module . file ) ; // Try to replace aliased module let found = false ; let constantModulePart ; filesMap . keys . filter ( ( k ) => { const d = filesMap . contents [ k ] ; let idx = module . file . search ( d . regexp ) ; if ( ! found && idx > - 1 ) { // throw `Found ${d} in ${module.file}` // constantModulePart = module.file.slice(0, idx) || './'; constantModulePart = './' ; if ( module . file [ idx ] === '/' ) { idx += 1 ; } const value = d . value [ 0 ] === '.' && idx > 0 ? d . value . slice ( 2 ) : d . value ; // const value = d.value; // Replace the alias with it's path and continue module . file = ` ${ module . file . slice ( 0 , idx ) || '' } ${ value } ${ module . file . slice ( idx + d . expose . length ) || '' } ` ; found = true ; // Revaluate npm and react flags based on the new mapping module = determineContext ( module . file , options ) ; return true ; } return false ; } ) ; // Leave NPM modules as resolved, do not remap and ignore wrongfully formatted strings of form require('npm:') if ( module . npm ) { return module . file || null ; } // Do not touch direct requires to npm modules (non annotated) if ( module . file . indexOf ( './' ) !== 0 && module . file . indexOf ( '/' ) !== 0 ) { return null ; } // Check if any substitution took place if ( found ) { // Module alias substituted if ( ! path . isAbsolute ( module . file ) ) { if ( ! absoluteModule && module . file [ 0 ] !== '.' ) { // Add the relative notation back module . file = ` ${ module . file } ` ; } } } // Do not break node_modules required by name if ( ! found && module . file [ 0 ] !== '.' ) { if ( reactOsFileInfer ( options ) ) { const aux2 = mapForReact ( module . file ) ; if ( aux2 !== module . file ) { return aux2 ; } } return null ; } // Enforce absolute paths on absolute mode if ( rootPath ) { if ( ! path . isAbsolute ( module . file ) ) { module . file = path . join ( rootPath , module . file ) ; if ( reactOsFileInfer ( options ) ) { return mapForReact ( module . file ) ; } return module . file ; } // After the node is replaced the visitor will be called again. // Without this condition these functions will generate a circular loop. return null ; } // throw `Gets here: ${JSON.stringify(module.file)}`; // Do not bother with relative paths that are not aliased if ( ! found ) { return null ; } let moduleMapped = mapToRelative ( module . file , constantModulePart , options ) ; if ( moduleMapped . indexOf ( './' ) !== 0 ) { moduleMapped = ` ${ moduleMapped } ` ; } // throw JSON.stringify({ //   moduleMapped, modulePath, module, constantModulePart // }, null, 2); return moduleMapped !== modulePath ? moduleMapped : null ; }
Adapts a module path to the context of caller
Adapts a module path to the context of caller