11import  {  Monaco  }  from  '@monaco-editor/react' 
22
3+ // A type representing a single type file (.d.ts) to be added to the Monaco editor, containing its virtual path and content. 
34type  Library  =  {  filePath : string ;  content : string  } 
5+ 
6+ // A type defining the minimum required fields from a package.json for type loading. 
47type  PackageJson  =  { 
58  name ?: string 
69  version ?: string 
@@ -19,6 +22,7 @@ function isRelative(p: string): boolean {
1922  return  p . startsWith ( './' )  ||  p . startsWith ( '../' )  ||  p . startsWith ( '/' ) 
2023} 
2124
25+ // Extracts the base package name from an import path (e.g., 'viem/chains' -> 'viem'). 
2226function  normalizeBareSpecifier ( p : string ) : string  { 
2327  if  ( ! p )  return  p 
2428  if  ( p . startsWith ( '@' ) )  return  p . split ( '/' ) . slice ( 0 ,  2 ) . join ( '/' ) 
@@ -32,6 +36,7 @@ function toTypesScopedName(pkg: string): string {
3236  return  '@types/'  +  pkg 
3337} 
3438
39+ // Converts a CDN URL to a virtual file system path used by the Monaco editor. 
3540function  toVirtual ( url : string ) : string  { 
3641  return  url . replace ( CDN_BASE ,  VIRTUAL_BASE ) 
3742} 
@@ -46,13 +51,15 @@ async function fetchJson<T = any>(url: string): Promise<T> {
4651  return  res . json ( ) 
4752} 
4853
49- // Guesses potential type definition file (.d.ts) paths from a JS file path. 
54+ // Guesses a list of potential TypeScript Definition file (.d.ts) paths from a given JS-like file path. 
55+ // For example, 'index.js' is converted to 'index.d.ts', 'index.ts', 'index/index.d.ts', etc. 
5056function  guessDtsFromJs ( jsPath : string ) : string [ ]  { 
5157  const  base  =  stripJsLike ( jsPath ) 
5258  return  [ `${ base }  ,  `${ base }  ,  `${ base }  ,  `${ base }  ] 
5359} 
5460
55- // Analyzes the 'exports' field of package.json to create a map of subpaths to their type file URLs. 
61+ // Analyzes the 'exports' field of a package.json to create a map of subpath entry points to their corresponding type definition file URLs. 
62+ // This map is crucial for correctly resolving subpath imports like 'viem' vs. 'viem/chains'. 
5663function  buildExportTypeMap ( pkgName : string ,  pkgJson : PackageJson ) : Record < string ,  string [ ] >  { 
5764  const  map : Record < string ,  string [ ] >  =  { } 
5865  const  base  =  `${ CDN_BASE } ${ pkgName }  
@@ -78,6 +85,8 @@ function buildExportTypeMap(pkgName: string, pkgJson: PackageJson): Record<strin
7885  return  map 
7986} 
8087
88+ // Given an array of candidate URLs, this function tries to fetch each one sequentially and returns the content of the first successful request. 
89+ // This is used to find the correct file from the list of possibilities generated by 'guessDtsFromJs'. 
8190async  function  tryFetchOne ( urls : string [ ] ) : Promise < ResolveResult  |  null >  { 
8291  for  ( const  u  of  [ ...new  Set ( urls ) ] )  { 
8392    try  { 
@@ -121,13 +130,13 @@ async function crawl(entryUrl: string, pkgName: string, visited: Set<string>, en
121130  return  out 
122131} 
123132
124- // Main function for the type loading process. Fetches  type files  for a package and all its dependencies . 
133+ // [3/4] The core service that, upon request from 'editor.ts', fetches  type definitions (.d.ts)  for NPM packages from a CDN . 
125134export  async  function  startTypeLoadingProcess ( packageName : string ) : Promise < {  mainVirtualPath : string ;  libs : Library [ ] ;  subpathMap : Record < string ,  string >  }  |  void >  { 
126135  const  visitedPackages  =  new  Set < string > ( ) 
127136  const  collected : Library [ ]  =  [ ] 
128137  const  subpathMap : Record < string ,  string >  =  { } 
129138
130-   // An  inner function that recursively loads packages . 
139+   // The core  inner function that recursively loads a package and its dependencies . 
131140  async  function  loadPackage ( pkgNameToLoad : string )  { 
132141    if  ( visitedPackages . has ( pkgNameToLoad ) )  return 
133142    visitedPackages . add ( pkgNameToLoad ) 
0 commit comments