All files / src/utils resolveModule.ts

100% Statements 31/31
100% Branches 5/5
100% Functions 4/4
100% Lines 31/31

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 3210x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 20x 20x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x  
import { sync as resolveSync } from 'resolve';
 
export function resolveModule(
  fileName: string,
  basedir = process.cwd(),
): null | string {
  try {
    return resolveSync(fileName, {
      basedir,
      packageFilter(pkg: {
        main?: string;
        module?: string;
        esnext?: string;
        'jsnext:main'?: string;
      }) {
        pkg.main = pkg.module || pkg.esnext || pkg['jsnext:main'] || pkg.main;
 
        return pkg;
      },
    });
  } catch (error: unknown) {
    if (
      error instanceof Error &&
      (error as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND'
    ) {
      return null;
    }
 
    throw error;
  }
}