getImportModulePath function

String getImportModulePath(
  1. String moduleUrlStr,
  2. String importedUrlStr
)

Returns the absolute or relative path to use to load the source for a given an import url such as templateUrl or cssUrls.

Implementation

String getImportModulePath(String moduleUrlStr, String importedUrlStr) {
  var absolutePathPrefix = 'package:';
  var moduleUrl = _AssetUrl.parse(moduleUrlStr, false)!;
  var importedUrl = _AssetUrl.parse(importedUrlStr, true);
  if (importedUrl == null) {
    return importedUrlStr;
  }
  // Import self.
  if (moduleUrlStr == importedUrlStr) {
    return importedUrl.modulePath.split(_PATH_SEP).last;
  }
  // Try to create a relative path first
  if (moduleUrl.firstLevelDir == importedUrl.firstLevelDir &&
      moduleUrl.packageName == importedUrl.packageName) {
    return _getRelativePath(moduleUrl.modulePath, importedUrl.modulePath);
  } else if (importedUrl.firstLevelDir == 'lib') {
    return '$absolutePathPrefix${importedUrl.packageName}'
        '/${importedUrl.modulePath}';
  }
  throw StateError("Can't import url $importedUrlStr from $moduleUrlStr");
}