Sha256: 76905dc24648fad4b962442474fa327bbf77443190148881b00433e7b16c1e3b

Contents?: true

Size: 1.95 KB

Versions: 50

Compression:

Stored size: 1.95 KB

Contents

import * as path from "path";

export interface MappingEntry {
  readonly pattern: string;
  readonly paths: ReadonlyArray<string>;
}

export interface Paths {
  readonly [key: string]: ReadonlyArray<string>;
}

/**
 * Converts an absolute baseUrl and paths to an array of absolute mapping entries.
 * The array is sorted by longest prefix.
 * Having an array with entries allows us to keep a sorting order rather than
 * sort by keys each time we use the mappings.
 * @param absoluteBaseUrl
 * @param paths
 * @param addMatchAll
 */
export function getAbsoluteMappingEntries(
  absoluteBaseUrl: string,
  paths: Paths,
  addMatchAll: boolean
): ReadonlyArray<MappingEntry> {
  // Resolve all paths to absolute form once here, and sort them by
  // longest prefix once here, this saves time on each request later.
  // We need to put them in an array to preserve the sorting order.
  const sortedKeys = sortByLongestPrefix(Object.keys(paths));
  const absolutePaths: Array<MappingEntry> = [];
  for (const key of sortedKeys) {
    absolutePaths.push({
      pattern: key,
      paths: paths[key].map((pathToResolve) =>
        path.join(absoluteBaseUrl, pathToResolve)
      ),
    });
  }
  // If there is no match-all path specified in the paths section of tsconfig, then try to match
  // all paths relative to baseUrl, this is how typescript works.
  if (!paths["*"] && addMatchAll) {
    absolutePaths.push({
      pattern: "*",
      paths: [`${absoluteBaseUrl.replace(/\/$/, "")}/*`],
    });
  }

  return absolutePaths;
}

/**
 * Sort path patterns.
 * If a module name can be matched with multiple patterns then pattern with the longest prefix will be picked.
 */
function sortByLongestPrefix(arr: Array<string>): Array<string> {
  return arr
    .concat()
    .sort((a: string, b: string) => getPrefixLength(b) - getPrefixLength(a));
}

function getPrefixLength(pattern: string): number {
  const prefixLength = pattern.indexOf("*");
  return pattern.substr(0, prefixLength).length;
}

Version data entries

50 entries across 50 versions & 2 rubygems

Version Path
immosquare-cleaner-0.1.60 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.59 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.58 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.57 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.56 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.55 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.54 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.53 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.52 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.51 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.50 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.49 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.48 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.47 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.46 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.45 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.44 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.43 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.42 node_modules/tsconfig-paths/src/mapping-entry.ts
immosquare-cleaner-0.1.41 node_modules/tsconfig-paths/src/mapping-entry.ts