resolvePath method

  1. @override
Future<String?> resolvePath(
  1. String name,
  2. String? currentPath
)
override

Resolves the full path of a module.

name is the name of the module (as specified when calling require())

currentPath is the __module.path value of the current module, if require() was called inside of a module.

This function must return the same path for names that refer to the same module.

Implementation

@override
Future<String?> resolvePath(String name, String? currentPath) async {
  if (sanitizePaths) {
    if (p.isAbsolute(name) ||
        p.split(name).any((part) => part == '..')) return null;
  }

  // Add and prioritize the current module directory if available
  final paths = currentPath == null ?
      this.paths :
      [p.dirname(currentPath), ...this.paths];

  // Find the file
  File? file;
  for (final path in paths) {
    var modulePath = p.normalize(p.join(path, name));
    if (ext != null) {
      if (p.extension(name) != ext) modulePath += ext!;
    }

    final f = File(modulePath);
    if (await f.exists()) {
      file = f;
      break;
    }
  }
  if (file == null) return null;

  return file.absolute.path;
}