@@ -104,6 +104,12 @@ const SOURCE_MAP_PARENT_EXTENSIONS = new Set([
104104 'tsx' ,
105105] ) ;
106106
107+ const DECLARATION_FILE_PARENT_EXTENSIONS = new Map ( [
108+ [ '.d.cts' , [ 'cjs' ] ] ,
109+ [ '.d.mts' , [ 'mjs' ] ] ,
110+ [ '.d.ts' , [ 'js' , 'mjs' , 'cjs' ] ] ,
111+ ] ) ;
112+
107113const HASH_FILE_EXTENSIONS = new Set ( [ 'md5' , 'sha1' , 'sha3' , 'sha256' , 'sha512' ] ) ;
108114
109115export function getFileWarning ( fileName ?: string ) {
@@ -118,10 +124,26 @@ export function getCodeBrowserFilePath(path: string, prefix?: string) {
118124}
119125
120126export function getCodeBrowserNestedFileParentPath ( path : string ) {
127+ return getCodeBrowserNestedFileParentPaths ( path ) [ 0 ] ?? null ;
128+ }
129+
130+ export function getCodeBrowserNestedFileParentPaths ( path : string ) {
131+ const lowerCasedPath = path . toLowerCase ( ) ;
132+
133+ for ( const [ declarationFileSuffix , parentExtensions ] of DECLARATION_FILE_PARENT_EXTENSIONS ) {
134+ if ( ! lowerCasedPath . endsWith ( declarationFileSuffix ) ) {
135+ continue ;
136+ }
137+
138+ const declarationFileBasePath = path . slice ( 0 , - declarationFileSuffix . length ) ;
139+
140+ return parentExtensions . map ( parentExtension => `${ declarationFileBasePath } .${ parentExtension } ` ) ;
141+ }
142+
121143 const nestedFileExtension = path . split ( '.' ) . pop ( ) ?. toLowerCase ( ) ;
122144
123145 if ( ! nestedFileExtension ) {
124- return null ;
146+ return [ ] ;
125147 }
126148
127149 const nestedFileParentPath = path . slice ( 0 , - ( nestedFileExtension . length + 1 ) ) ;
@@ -130,17 +152,17 @@ export function getCodeBrowserNestedFileParentPath(path: string) {
130152 const sourceMapParentExtension = nestedFileParentPath . split ( '.' ) . pop ( ) ?. toLowerCase ( ) ;
131153
132154 if ( ! sourceMapParentExtension || ! SOURCE_MAP_PARENT_EXTENSIONS . has ( sourceMapParentExtension ) ) {
133- return null ;
155+ return [ ] ;
134156 }
135157
136- return nestedFileParentPath ;
158+ return [ nestedFileParentPath ] ;
137159 }
138160
139161 if ( HASH_FILE_EXTENSIONS . has ( nestedFileExtension ) ) {
140- return nestedFileParentPath ;
162+ return [ nestedFileParentPath ] ;
141163 }
142164
143- return null ;
165+ return [ ] ;
144166}
145167
146168export function buildCodeBrowserFileTree (
@@ -197,13 +219,9 @@ function nestCodeBrowserSidecarFiles(directory: CodeBrowserTreeDirectory) {
197219 const nestedFilePaths = new Set < string > ( ) ;
198220
199221 directory . files . forEach ( file => {
200- const nestedFileParentPath = getCodeBrowserNestedFileParentPath ( file . path ) ;
201-
202- if ( ! nestedFileParentPath ) {
203- return ;
204- }
205-
206- const nestedFileParent = filesByPath . get ( nestedFileParentPath ) ;
222+ const nestedFileParent = getCodeBrowserNestedFileParentPaths ( file . path )
223+ . map ( nestedFileParentPath => filesByPath . get ( nestedFileParentPath ) )
224+ . find ( Boolean ) ;
207225
208226 if ( ! nestedFileParent ) {
209227 return ;
0 commit comments