lib/resolver/package.js
function resolvePath(filepath) {
  filepath = filepath.replace(RE_TRAILING, '');
  const cwd = process.cwd();
  const npmPackage = filepath.includes('node_modules');

  // Find nearest node_modules directory
  if (npmPackage) {
    const parts = filepath.split(path.sep);
    let idx = parts.lastIndexOf('node_modules');

    if (idx < parts.length - 1) idx += 2;
    // Handle scoped
    if (parts[idx - 1].charAt(0) == '@') idx++;

    const dir = parts.slice(0, idx).join(path.sep);

    // Installed packages must have manifest, otherwise continue
    if (exists(path.join(dir, 'package.json'))) return dir;
  }

  // Find nearest directory with node_modules subdirectory
  if (filepath.includes(cwd)) {
    let depth = maxFileSystemDepth;
    let dir = filepath;
    let parent = '';

    while (true) {
      parent = path.dirname(dir);
      // Stop if we hit max file system depth or root
      // Convert to lowercase to fix problems on Windows
      if (!--depth || parent.toLowerCase() === dir.toLowerCase()) {
        break;
      }

      // Stop at nearest directory with node_modules or cwd
      if (dir == cwd || exists(path.resolve(dir, 'node_modules'))) return dir;

      // Walk
      dir = parent;
    }
  }

  // Return project directory if file isn't a project file
  return cwd;
}
function resolvePath(filepath) {
  filepath = filepath.replace(RE_TRAILING, '');
  const cwd = process.cwd();
  const npmPackage = filepath.includes('node_modules');

  // Find nearest node_modules directory
  if (npmPackage) {
    const parts = filepath.split(path.sep);
    let idx = parts.lastIndexOf('node_modules');

    if (idx < parts.length - 1) idx += 2;
    // Handle scoped
    if (parts[idx - 1].charAt(0) == '@') idx++;

    const dir = parts.slice(0, idx).join(path.sep);

    // Installed packages must have manifest, otherwise continue
    if (exists(path.join(dir, 'package.json'))) return dir;
  }

  // Find nearest directory with node_modules subdirectory
  if (filepath.includes(cwd)) {
    let depth = maxFileSystemDepth;
    let dir = filepath;
    let parent = '';

    while (true) {
      parent = path.dirname(dir);
      // Stop if we hit max file system depth or root
      // Convert to lowercase to fix problems on Windows
      if (!--depth || parent.toLowerCase() === dir.toLowerCase()) {
        break;
      }

      // Stop at nearest directory with node_modules or cwd
      if (dir == cwd || exists(path.resolve(dir, 'node_modules'))) return dir;

      // Walk
      dir = parent;
    }
  }

  // Return project directory if file isn't a project file
  return cwd;
}
function resolvePath ( filepath ) { filepath = filepath . replace ( RE_TRAILING , '' ) ; const cwd = process . cwd ( ) ; const npmPackage = filepath . includes ( 'node_modules' ) ; // Find nearest node_modules directory if ( npmPackage ) { const parts = filepath . split ( path . sep ) ; let idx = parts . lastIndexOf ( 'node_modules' ) ; if ( idx < parts . length - 1 ) idx += 2 ; // Handle scoped if ( parts [ idx - 1 ] . charAt ( 0 ) == '@' ) idx ++ ; const dir = parts . slice ( 0 , idx ) . join ( path . sep ) ; // Installed packages must have manifest, otherwise continue if ( exists ( path . join ( dir , 'package.json' ) ) ) return dir ; } // Find nearest directory with node_modules subdirectory if ( filepath . includes ( cwd ) ) { let depth = maxFileSystemDepth ; let dir = filepath ; let parent = '' ; while ( true ) { parent = path . dirname ( dir ) ; // Stop if we hit max file system depth or root // Convert to lowercase to fix problems on Windows if ( ! -- depth || parent . toLowerCase ( ) === dir . toLowerCase ( ) ) { break ; } // Stop at nearest directory with node_modules or cwd if ( dir == cwd || exists ( path . resolve ( dir , 'node_modules' ) ) ) return dir ; // Walk dir = parent ; } } // Return project directory if file isn't a project file return cwd ; }
Resolve package path from 'filepath'
@param {String} filepath
@returns {String}
Resolve package path from filepath