| [9af201e] | 1 | import * as path from "path";
|
|---|
| 2 | import * as Filesystem from "./filesystem";
|
|---|
| 3 | import * as MappingEntry from "./mapping-entry";
|
|---|
| 4 | import * as TryPath from "./try-path";
|
|---|
| 5 |
|
|---|
| 6 | /**
|
|---|
| 7 | * Function that can match a path
|
|---|
| 8 | */
|
|---|
| 9 | export interface MatchPath {
|
|---|
| 10 | (
|
|---|
| 11 | requestedModule: string,
|
|---|
| 12 | readJson?: Filesystem.ReadJsonSync,
|
|---|
| 13 | fileExists?: (name: string) => boolean,
|
|---|
| 14 | extensions?: ReadonlyArray<string>
|
|---|
| 15 | ): string | undefined;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Creates a function that can resolve paths according to tsconfig paths property.
|
|---|
| 20 | * @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig.
|
|---|
| 21 | * @param paths The paths as specified in tsconfig.
|
|---|
| 22 | * @param mainFields A list of package.json field names to try when resolving module files.
|
|---|
| 23 | * @param addMatchAll Add a match-all "*" rule if none is present
|
|---|
| 24 | * @returns a function that can resolve paths.
|
|---|
| 25 | */
|
|---|
| 26 | export function createMatchPath(
|
|---|
| 27 | absoluteBaseUrl: string,
|
|---|
| 28 | paths: { [key: string]: Array<string> },
|
|---|
| 29 | mainFields: string[] = ["main"],
|
|---|
| 30 | addMatchAll: boolean = true
|
|---|
| 31 | ): MatchPath {
|
|---|
| 32 | const absolutePaths = MappingEntry.getAbsoluteMappingEntries(
|
|---|
| 33 | absoluteBaseUrl,
|
|---|
| 34 | paths,
|
|---|
| 35 | addMatchAll
|
|---|
| 36 | );
|
|---|
| 37 |
|
|---|
| 38 | return (
|
|---|
| 39 | requestedModule: string,
|
|---|
| 40 | readJson?: Filesystem.ReadJsonSync,
|
|---|
| 41 | fileExists?: Filesystem.FileExistsSync,
|
|---|
| 42 | extensions?: Array<string>
|
|---|
| 43 | ) =>
|
|---|
| 44 | matchFromAbsolutePaths(
|
|---|
| 45 | absolutePaths,
|
|---|
| 46 | requestedModule,
|
|---|
| 47 | readJson,
|
|---|
| 48 | fileExists,
|
|---|
| 49 | extensions,
|
|---|
| 50 | mainFields
|
|---|
| 51 | );
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Finds a path from tsconfig that matches a module load request.
|
|---|
| 56 | * @param absolutePathMappings The paths to try as specified in tsconfig but resolved to absolute form.
|
|---|
| 57 | * @param requestedModule The required module name.
|
|---|
| 58 | * @param readJson Function that can read json from a path (useful for testing).
|
|---|
| 59 | * @param fileExists Function that checks for existence of a file at a path (useful for testing).
|
|---|
| 60 | * @param extensions File extensions to probe for (useful for testing).
|
|---|
| 61 | * @param mainFields A list of package.json field names to try when resolving module files.
|
|---|
| 62 | * @returns the found path, or undefined if no path was found.
|
|---|
| 63 | */
|
|---|
| 64 | export function matchFromAbsolutePaths(
|
|---|
| 65 | absolutePathMappings: ReadonlyArray<MappingEntry.MappingEntry>,
|
|---|
| 66 | requestedModule: string,
|
|---|
| 67 | readJson: Filesystem.ReadJsonSync = Filesystem.readJsonFromDiskSync,
|
|---|
| 68 | fileExists: Filesystem.FileExistsSync = Filesystem.fileExistsSync,
|
|---|
| 69 | extensions: Array<string> = Object.keys(require.extensions),
|
|---|
| 70 | mainFields: string[] = ["main"]
|
|---|
| 71 | ): string | undefined {
|
|---|
| 72 | const tryPaths = TryPath.getPathsToTry(
|
|---|
| 73 | extensions,
|
|---|
| 74 | absolutePathMappings,
|
|---|
| 75 | requestedModule
|
|---|
| 76 | );
|
|---|
| 77 |
|
|---|
| 78 | if (!tryPaths) {
|
|---|
| 79 | return undefined;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | return findFirstExistingPath(tryPaths, readJson, fileExists, mainFields);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | function findFirstExistingMainFieldMappedFile(
|
|---|
| 86 | packageJson: Filesystem.PackageJson,
|
|---|
| 87 | mainFields: string[],
|
|---|
| 88 | packageJsonPath: string,
|
|---|
| 89 | fileExists: Filesystem.FileExistsSync
|
|---|
| 90 | ): string | undefined {
|
|---|
| 91 | for (let index = 0; index < mainFields.length; index++) {
|
|---|
| 92 | const mainFieldName = mainFields[index];
|
|---|
| 93 | const candidateMapping = packageJson[mainFieldName];
|
|---|
| 94 | if (candidateMapping && typeof candidateMapping === "string") {
|
|---|
| 95 | const candidateFilePath = path.join(
|
|---|
| 96 | path.dirname(packageJsonPath),
|
|---|
| 97 | candidateMapping
|
|---|
| 98 | );
|
|---|
| 99 | if (fileExists(candidateFilePath)) {
|
|---|
| 100 | return candidateFilePath;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | return undefined;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | function findFirstExistingPath(
|
|---|
| 109 | tryPaths: ReadonlyArray<TryPath.TryPath>,
|
|---|
| 110 | readJson: Filesystem.ReadJsonSync = Filesystem.readJsonFromDiskSync,
|
|---|
| 111 | fileExists: Filesystem.FileExistsSync,
|
|---|
| 112 | mainFields: string[] = ["main"]
|
|---|
| 113 | ): string | undefined {
|
|---|
| 114 | for (const tryPath of tryPaths) {
|
|---|
| 115 | if (
|
|---|
| 116 | tryPath.type === "file" ||
|
|---|
| 117 | tryPath.type === "extension" ||
|
|---|
| 118 | tryPath.type === "index"
|
|---|
| 119 | ) {
|
|---|
| 120 | if (fileExists(tryPath.path)) {
|
|---|
| 121 | return TryPath.getStrippedPath(tryPath);
|
|---|
| 122 | }
|
|---|
| 123 | } else if (tryPath.type === "package") {
|
|---|
| 124 | const packageJson: Filesystem.PackageJson = readJson(tryPath.path);
|
|---|
| 125 | if (packageJson) {
|
|---|
| 126 | const mainFieldMappedFile = findFirstExistingMainFieldMappedFile(
|
|---|
| 127 | packageJson,
|
|---|
| 128 | mainFields,
|
|---|
| 129 | tryPath.path,
|
|---|
| 130 | fileExists
|
|---|
| 131 | );
|
|---|
| 132 | if (mainFieldMappedFile) {
|
|---|
| 133 | return mainFieldMappedFile;
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | } else {
|
|---|
| 137 | TryPath.exhaustiveTypeException(tryPath.type);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | return undefined;
|
|---|
| 141 | }
|
|---|