@@ -78,6 +78,7 @@ export default class Editor extends Plugin {
7878 this . typeLoaderDebounce = null
7979
8080 this . tsModuleMappings = { }
81+ this . processedPackages = new Set ( )
8182 }
8283
8384 setDispatch ( dispatch ) {
@@ -142,15 +143,21 @@ export default class Editor extends Plugin {
142143 this . activated = true
143144 this . on ( 'editor' , 'editorMounted' , ( ) => {
144145 if ( ! this . monaco ) return
145- const tsDefaults = this . monaco . languages . typescript . typescriptDefaults
146+ const ts = this . monaco . languages . typescript
147+ const tsDefaults = ts . typescriptDefaults
146148
147149 tsDefaults . setCompilerOptions ( {
148- moduleResolution : this . monaco . languages . typescript . ModuleResolutionKind . NodeJs ,
149- target : this . monaco . languages . typescript . ScriptTarget . ES2020 ,
150+ moduleResolution : ts . ModuleResolutionKind . Bundler ,
151+ module : ts . ModuleKind . ESNext ,
152+ target : ts . ScriptTarget . ES2022 ,
153+ lib : [ 'es2022' , 'dom' , 'dom.iterable' ] ,
150154 allowNonTsExtensions : true ,
155+ allowSyntheticDefaultImports : true ,
156+ skipLibCheck : true ,
151157 baseUrl : 'file:///node_modules/' ,
152- paths : { }
158+ paths : this . tsModuleMappings ,
153159 } )
160+ console . log ( '[DIAGNOSE-SETUP] Initial CompilerOptions set.' )
154161 } )
155162 this . on ( 'sidePanel' , 'focusChanged' , ( name ) => {
156163 this . keepDecorationsFor ( name , 'sourceAnnotationsPerFile' )
@@ -181,23 +188,32 @@ export default class Editor extends Plugin {
181188 }
182189
183190 updateTsCompilerOptions ( ) {
184- console . log ( '[Module Mapper] Updating TS compiler options with new paths:' , this . tsModuleMappings )
191+ if ( ! this . monaco ) return
192+ console . log ( '[DIAGNOSE-PATHS] Updating TS compiler options...' )
193+ console . log ( '[DIAGNOSE-PATHS] Current path mappings:' , JSON . stringify ( this . tsModuleMappings , null , 2 ) )
194+
185195 const tsDefaults = this . monaco . languages . typescript . typescriptDefaults
186- const oldOptions = tsDefaults . getCompilerOptions ( )
196+ const currentOptions = tsDefaults . getCompilerOptions ( )
187197
188- const newOptions = {
189- ...oldOptions ,
190- baseUrl : 'file:///node_modules/' ,
191- paths : this . tsModuleMappings
192- }
193-
194- console . log ( '[DEBUG 3] Updating TS compiler with new options:' , JSON . stringify ( newOptions , null , 2 ) )
195- tsDefaults . setCompilerOptions ( newOptions )
198+ tsDefaults . setCompilerOptions ( {
199+ ...currentOptions ,
200+ paths : { ...currentOptions . paths , ...this . tsModuleMappings }
201+ } )
202+ console . log ( '[DIAGNOSE-PATHS] TS compiler options updated.' )
203+ }
204+
205+ addExtraLibs ( libs ) {
206+ if ( ! this . monaco || ! libs || libs . length === 0 ) return
207+ console . log ( `[DIAGNOSE-LIBS] Adding ${ libs . length } new files to Monaco...` )
196208
197- setTimeout ( ( ) => {
198- const allLibs = tsDefaults . getExtraLibs ( )
199- console . log ( '[DEBUG 4] Final check - Monaco extraLibs state:' , Object . keys ( allLibs ) . length , 'libs loaded.' )
200- } , 2000 )
209+ const tsDefaults = this . monaco . languages . typescript . typescriptDefaults
210+
211+ libs . forEach ( lib => {
212+ if ( ! tsDefaults . getExtraLibs ( ) [ lib . filePath ] ) {
213+ tsDefaults . addExtraLib ( lib . content , lib . filePath )
214+ }
215+ } )
216+ console . log ( `[DIAGNOSE-LIBS] Files added. Total extra libs now: ${ Object . keys ( tsDefaults . getExtraLibs ( ) ) . length } .` )
201217 }
202218
203219 async _onChange ( file ) {
@@ -210,58 +226,53 @@ export default class Editor extends Plugin {
210226 const model = this . monaco . editor . getModel ( this . monaco . Uri . parse ( file ) )
211227 if ( ! model ) return
212228 const code = model . getValue ( )
213-
214- try {
215229
216- const extractPackageName = ( importPath ) => {
217- if ( importPath . startsWith ( '@' ) ) {
218- const parts = importPath . split ( '/' )
219- return `${ parts [ 0 ] } /${ parts [ 1 ] } `
220- }
221- return importPath . split ( '/' ) [ 0 ]
230+ try {
231+ console . log ( '[DIAGNOSE-ONCHANGE] Change detected, analyzing imports...' )
232+ const extractPackageName = ( p ) => p . startsWith ( '@' ) ? p . split ( '/' ) . slice ( 0 , 2 ) . join ( '/' ) : p . split ( '/' ) [ 0 ]
233+ const rawImports = [ ...code . matchAll ( / (?: f r o m | i m p o r t ) \s + [ ' " ] ( (? ! \. ) .* ?) [ ' " ] / g) ] . map ( match => match [ 1 ] )
234+ const uniquePackages = [ ...new Set ( rawImports . map ( extractPackageName ) ) ]
235+
236+ const newPackages = uniquePackages . filter ( p => ! this . processedPackages . has ( p ) )
237+ if ( newPackages . length === 0 ) {
238+ console . log ( '[DIAGNOSE-ONCHANGE] No new packages to process.' )
239+ return
222240 }
223-
224- const rawImports = [ ...code . matchAll ( / f r o m \s + [ ' " ] ( (? ! [ . / ] ) .* ?) [ ' " ] / g) ] . map ( match => match [ 1 ] )
225241
226- const uniquePackages = [ ...new Set ( rawImports . map ( extractPackageName ) ) ]
227- console . log ( '[DEBUG 1] Extracted Package Names:' , uniquePackages )
228- const newPackages = uniquePackages . filter ( p => ! this . tsModuleMappings [ p ] )
229- if ( newPackages . length === 0 ) return
230-
231- console . log ( '[Module Mapper] New packages detected:' , newPackages )
242+ console . log ( '[DIAGNOSE-ONCHANGE] New packages to process:' , newPackages )
232243
233244 let newPathsFound = false
234245 const promises = newPackages . map ( async ( pkg ) => {
235- try {
236- const result = await startTypeLoadingProcess ( pkg , this . monaco )
246+ this . processedPackages . add ( pkg )
247+ const result = await startTypeLoadingProcess ( pkg )
248+
249+ console . log ( `[DIAGNOSE-ONCHANGE] Result received for "${ pkg } ":` , result ? { mainVirtualPath : result . mainVirtualPath , libsCount : result . libs . length } : 'null' )
250+
251+ if ( result && result . libs && result . libs . length > 0 ) {
252+ this . addExtraLibs ( result . libs )
237253
238- if ( result && result . virtualPath ) {
239- // this.tsModuleMappings[pkg] = [`${pkg}/`]
240- // this.tsModuleMappings[`${pkg}/*`] = [`${pkg}/*`]
241- const typeFileRelativePath = result . virtualPath . replace ( 'file:///node_modules/' , '' )
242-
243- this . tsModuleMappings [ pkg ] = [ typeFileRelativePath ]
254+ if ( result . mainVirtualPath ) {
255+ this . tsModuleMappings [ pkg ] = [ result . mainVirtualPath . replace ( 'file:///node_modules/' , '' ) ]
244256 this . tsModuleMappings [ `${ pkg } /*` ] = [ `${ pkg } /*` ]
245-
246257 newPathsFound = true
258+ } else {
259+ console . warn ( `[DIAGNOSE-ONCHANGE] No mainVirtualPath found for "${ pkg } ", path mapping will be incomplete.` )
247260 }
248- } catch ( error ) {
249- console . error ( `[Module Mapper] Failed to process types for ${ pkg } ` , error )
250261 }
251262 } )
252-
253263 await Promise . all ( promises )
254264
255265 if ( newPathsFound ) {
256- setTimeout ( ( ) => this . updateTsCompilerOptions ( ) , 1000 )
266+ this . updateTsCompilerOptions ( )
267+ } else {
268+ console . log ( '[DIAGNOSE-ONCHANGE] No new paths were mapped.' )
257269 }
258270
259271 } catch ( error ) {
260- console . error ( '[Type Loader ] Error during type loading process:' , error )
272+ console . error ( '[DIAGNOSE-ONCHANGE ] Error during type loading process:' , error )
261273 }
262274 } , 1500 )
263275 }
264-
265276 const currentFile = await this . call ( 'fileManager' , 'file' )
266277 if ( ! currentFile ) {
267278 return
0 commit comments