@@ -20,14 +20,14 @@ export interface MatchPath {
2020 *
2121 * @param absoluteBaseUrl Absolute version of baseUrl as specified in tsconfig.
2222 * @param paths The paths as specified in tsconfig.
23- * @param mainFields A list of package.json field names to try when resolving module files.
23+ * @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names.
2424 * @param addMatchAll Add a match-all "*" rule if none is present
2525 * @returns a function that can resolve paths.
2626 */
2727export function createMatchPath (
2828 absoluteBaseUrl : string ,
2929 paths : { [ key : string ] : Array < string > } ,
30- mainFields : string [ ] = [ "main" ] ,
30+ mainFields : ( string | string [ ] ) [ ] = [ "main" ] ,
3131 addMatchAll : boolean = true
3232) : MatchPath {
3333 const absolutePaths = MappingEntry . getAbsoluteMappingEntries (
@@ -60,7 +60,7 @@ export function createMatchPath(
6060 * @param readJson Function that can read json from a path (useful for testing).
6161 * @param fileExists Function that checks for existence of a file at a path (useful for testing).
6262 * @param extensions File extensions to probe for (useful for testing).
63- * @param mainFields A list of package.json field names to try when resolving module files.
63+ * @param mainFields A list of package.json field names to try when resolving module files. Select a nested field using an array of field names.
6464 * @returns the found path, or undefined if no path was found.
6565 */
6666export function matchFromAbsolutePaths (
@@ -69,7 +69,7 @@ export function matchFromAbsolutePaths(
6969 readJson : Filesystem . ReadJsonSync = Filesystem . readJsonFromDiskSync ,
7070 fileExists : Filesystem . FileExistsSync = Filesystem . fileExistsSync ,
7171 extensions : Array < string > = Object . keys ( require . extensions ) ,
72- mainFields : string [ ] = [ "main" ]
72+ mainFields : ( string | string [ ] ) [ ] = [ "main" ]
7373) : string | undefined {
7474 const tryPaths = TryPath . getPathsToTry (
7575 extensions ,
@@ -86,13 +86,16 @@ export function matchFromAbsolutePaths(
8686
8787function findFirstExistingMainFieldMappedFile (
8888 packageJson : Filesystem . PackageJson ,
89- mainFields : string [ ] ,
89+ mainFields : ( string | string [ ] ) [ ] ,
9090 packageJsonPath : string ,
9191 fileExists : Filesystem . FileExistsSync
9292) : string | undefined {
9393 for ( let index = 0 ; index < mainFields . length ; index ++ ) {
94- const mainFieldName = mainFields [ index ] ;
95- const candidateMapping = packageJson [ mainFieldName ] ;
94+ const mainFieldSelector = mainFields [ index ] ;
95+ const candidateMapping =
96+ typeof mainFieldSelector === "string"
97+ ? packageJson [ mainFieldSelector ]
98+ : mainFieldSelector . reduce ( ( obj , key ) => obj [ key ] , packageJson ) ;
9699 if ( candidateMapping && typeof candidateMapping === "string" ) {
97100 const candidateFilePath = path . join (
98101 path . dirname ( packageJsonPath ) ,
@@ -111,7 +114,7 @@ function findFirstExistingPath(
111114 tryPaths : ReadonlyArray < TryPath . TryPath > ,
112115 readJson : Filesystem . ReadJsonSync = Filesystem . readJsonFromDiskSync ,
113116 fileExists : Filesystem . FileExistsSync ,
114- mainFields : string [ ] = [ "main" ]
117+ mainFields : ( string | string [ ] ) [ ] = [ "main" ]
115118) : string | undefined {
116119 for ( const tryPath of tryPaths ) {
117120 if (
0 commit comments