@@ -93,6 +93,19 @@ const FILE_WARNING_MATCHERS = FILE_WARNINGS.map(warning => ({
9393 matchers : warning . fileNames . map ( createFileWarningMatcher ) ,
9494} ) ) ;
9595
96+ const SOURCE_MAP_PARENT_EXTENSIONS = new Set ( [
97+ 'cjs' ,
98+ 'cts' ,
99+ 'js' ,
100+ 'jsx' ,
101+ 'mjs' ,
102+ 'mts' ,
103+ 'ts' ,
104+ 'tsx' ,
105+ ] ) ;
106+
107+ const HASH_FILE_EXTENSIONS = new Set ( [ 'md5' , 'sha1' , 'sha3' , 'sha256' , 'sha512' ] ) ;
108+
96109export function getFileWarning ( fileName ?: string ) {
97110 if ( ! fileName ) {
98111 return undefined ;
@@ -104,6 +117,32 @@ export function getCodeBrowserFilePath(path: string, prefix?: string) {
104117 return prefix ? path . replace ( prefix , '' ) : path ;
105118}
106119
120+ export function getCodeBrowserNestedFileParentPath ( path : string ) {
121+ const nestedFileExtension = path . split ( '.' ) . pop ( ) ?. toLowerCase ( ) ;
122+
123+ if ( ! nestedFileExtension ) {
124+ return null ;
125+ }
126+
127+ const nestedFileParentPath = path . slice ( 0 , - ( nestedFileExtension . length + 1 ) ) ;
128+
129+ if ( nestedFileExtension === 'map' ) {
130+ const sourceMapParentExtension = nestedFileParentPath . split ( '.' ) . pop ( ) ?. toLowerCase ( ) ;
131+
132+ if ( ! sourceMapParentExtension || ! SOURCE_MAP_PARENT_EXTENSIONS . has ( sourceMapParentExtension ) ) {
133+ return null ;
134+ }
135+
136+ return nestedFileParentPath ;
137+ }
138+
139+ if ( HASH_FILE_EXTENSIONS . has ( nestedFileExtension ) ) {
140+ return nestedFileParentPath ;
141+ }
142+
143+ return null ;
144+ }
145+
107146export function buildCodeBrowserFileTree (
108147 files : UnpkgMeta [ 'files' ] ,
109148 prefix ?: string
@@ -139,6 +178,8 @@ export function buildCodeBrowserFileTree(
139178 } ) ;
140179 } ) ;
141180
181+ nestCodeBrowserSidecarFiles ( root ) ;
182+
142183 return root ;
143184}
144185
@@ -151,6 +192,33 @@ function createCodeBrowserTreeDirectory(name: string, path: string): CodeBrowser
151192 } ;
152193}
153194
195+ function nestCodeBrowserSidecarFiles ( directory : CodeBrowserTreeDirectory ) {
196+ const filesByPath = new Map ( directory . files . map ( file => [ file . path , file ] ) ) ;
197+ const nestedFilePaths = new Set < string > ( ) ;
198+
199+ directory . files . forEach ( file => {
200+ const nestedFileParentPath = getCodeBrowserNestedFileParentPath ( file . path ) ;
201+
202+ if ( ! nestedFileParentPath ) {
203+ return ;
204+ }
205+
206+ const nestedFileParent = filesByPath . get ( nestedFileParentPath ) ;
207+
208+ if ( ! nestedFileParent ) {
209+ return ;
210+ }
211+
212+ nestedFileParent . nestedFiles ??= [ ] ;
213+ nestedFileParent . nestedFiles . push ( file ) ;
214+ nestedFilePaths . add ( file . path ) ;
215+ } ) ;
216+
217+ directory . files = directory . files . filter ( file => ! nestedFilePaths . has ( file . path ) ) ;
218+
219+ Object . values ( directory . directories ) . forEach ( nestCodeBrowserSidecarFiles ) ;
220+ }
221+
154222function createFileWarningMatcher ( pattern : string ) {
155223 if ( ! pattern . includes ( '*' ) ) {
156224 return ( fileName : string ) => fileName === pattern ;
0 commit comments