@@ -165,24 +165,44 @@ const convertToModuleReferenceID = (
165
165
}
166
166
} ;
167
167
168
- const addTypecellModuleToRuntime = async (
168
+ const builtInModules = [ "react" , "scheduler" , "prop-types" , "csstype" ] ;
169
+
170
+ const isBuiltInModule = ( mod : string ) => {
171
+ const mainMod = mod . split ( "/" ) [ 0 ] || mod ;
172
+ return builtInModules . includes ( mainMod ) ;
173
+ } ;
174
+
175
+ const addBuiltInTypesToRuntime = async (
169
176
mod : string ,
170
177
path : string ,
171
178
config : ATAConfig
172
179
) => {
173
- const typecellPath = mod === "typecell" ? "@typecell-org/editor" : mod ;
180
+ let typePath = mod ;
174
181
175
- let content : string ;
182
+ if ( mod === "typecell" ) {
183
+ typePath = "@typecell-org/editor" ;
184
+ } else if ( isBuiltInModule ( mod ) ) {
185
+ if ( mod === "csstype" ) {
186
+ typePath = "@types/react/node_modules/csstype" ; // TODO: would be better to have 1 version of csstype, and in @types/csstype
187
+ } else {
188
+ typePath = "@types/" + mod ;
189
+ }
190
+ } else if ( mod . startsWith ( "@typecell-org" ) ) {
191
+ typePath = mod ;
192
+ } else {
193
+ throw new Error ( "unknown local type module" ) ;
194
+ }
195
+
196
+ let content : string | void ;
176
197
if ( import . meta. env . NODE_ENV === "test" ) {
177
198
// TODO: extract this case
178
199
let fs = require ( "fs" ) ;
179
- content = fs . readFileSync (
180
- "public/types/" + typecellPath + "/" + path ,
181
- "utf-8"
182
- ) ;
200
+ content = fs . readFileSync ( "public/types/" + typePath + "/" + path , "utf-8" ) ;
183
201
} else {
184
- const url = new URL ( "/types/" + typecellPath + "/" + path , import . meta. url ) ;
185
- content = await ( await config . fetcher ( url . toString ( ) ) ) . text ( ) ;
202
+ const url = new URL ( "/types/" + typePath + "/" + path , import . meta. url ) ;
203
+ // console.log("RESOLVE", mod, url.toString(), path);
204
+ content = await getCachedDTSString ( config , url . toString ( ) ) ;
205
+ // content = await (await config.fetcher(url.toString())).text();
186
206
}
187
207
if ( ! content ) {
188
208
return errorMsg (
@@ -194,7 +214,10 @@ const addTypecellModuleToRuntime = async (
194
214
// Now look and grab dependent modules where you need the
195
215
await getDependenciesForModule ( content , mod , path , config ) ;
196
216
197
- config . logger . log ( "adding typecell module" , path ) ;
217
+ config . logger . log (
218
+ "adding typecell module" ,
219
+ `file:///node_modules/@types/${ mod } /${ path } `
220
+ ) ;
198
221
config . addLibraryToRuntime (
199
222
content ,
200
223
`file:///node_modules/@types/${ mod } /${ path } `
@@ -260,6 +283,7 @@ const getModuleAndRootDefTypePath = async (
260
283
const url = moduleJSONURL ( packageName ) ;
261
284
262
285
const response = await config . fetcher ( url ) ;
286
+
263
287
if ( ! response . ok ) {
264
288
return errorMsg (
265
289
`Could not get Algolia JSON for the module '${ packageName } '` ,
@@ -366,6 +390,7 @@ const getCachedDTSString = async (config: ATAConfig, url: string) => {
366
390
}
367
391
368
392
const response = await config . fetcher ( url ) ;
393
+
369
394
if ( ! response . ok ) {
370
395
return errorMsg (
371
396
`Could not get DTS response for the module at ${ url } ` ,
@@ -374,6 +399,10 @@ const getCachedDTSString = async (config: ATAConfig, url: string) => {
374
399
) ;
375
400
}
376
401
402
+ if ( response . headers . get ( "content-type" ) === "text/html" ) {
403
+ // this happens when the file is not found, and the server is returning a dynamic route (html fallback) instead
404
+ console . warn ( `possibly wrong file for typescript types at ${ url } ` ) ;
405
+ }
377
406
// TODO: handle checking for a resolve to index.d.ts whens someone imports the folder
378
407
let content = await response . text ( ) ;
379
408
if ( ! content ) {
@@ -407,6 +436,10 @@ const getReferenceDependencies = async (
407
436
if ( relativePath ) {
408
437
let newPath = mapRelativePath ( relativePath , path ) ;
409
438
if ( newPath ) {
439
+ if ( isBuiltInModule ( mod ) ) {
440
+ await addBuiltInTypesToRuntime ( mod , newPath , config ) ;
441
+ return ;
442
+ }
410
443
const dtsRefURL = unpkgURL ( mod , newPath ) ;
411
444
412
445
const dtsReferenceResponseText = await getCachedDTSString (
@@ -519,18 +552,19 @@ const getDependenciesForModule = async (
519
552
moduleToDownload . split ( "/" ) . length === 1 ;
520
553
const isPackageRootImport = modIsPackageOnly || modIsScopedPackageOnly ;
521
554
const isDenoModule = moduleToDownload . indexOf ( "https://" ) === 0 ;
522
-
555
+ const isPackageSpecificFileImport =
556
+ ! isPackageRootImport &&
557
+ ! moduleToDownload . startsWith ( "." ) &&
558
+ moduleToDownload . includes ( "/" ) ; // absolute path
523
559
if ( moduleToDownload . startsWith ( "!@" ) ) {
524
560
// typecell imports are loaded in TypecellTypeResolver
525
561
return ;
526
562
// config.addLibraryToRuntime(code.dtsCode, moduleToDownload+".d.ts");
527
563
} else if ( isPackageRootImport ) {
528
564
if ( moduleToDownload . startsWith ( "@typecell-org/" ) ) {
529
- await addTypecellModuleToRuntime (
530
- moduleToDownload ,
531
- "index.d.ts" ,
532
- config
533
- ) ;
565
+ await addBuiltInTypesToRuntime ( moduleToDownload , "index.d.ts" , config ) ;
566
+ } else if ( builtInModules . includes ( moduleToDownload ) ) {
567
+ await addBuiltInTypesToRuntime ( moduleToDownload , "index.d.ts" , config ) ;
534
568
} else {
535
569
// So it doesn't run twice for a package
536
570
acquiredTypeDefs [ moduleID ] = null ;
@@ -549,22 +583,16 @@ const getDependenciesForModule = async (
549
583
} else if ( isDenoModule ) {
550
584
// E.g. import { serve } from "https://deno.land/[email protected] /http/server.ts";
551
585
await addModuleToRuntime ( moduleToDownload , moduleToDownload , config ) ;
552
- // TODO: Possible fix for scheduler/tracing, but not critical / should file with original repo
553
- // } else if (
554
- // !moduleToDownload.startsWith(".") &&
555
- // moduleToDownload.includes("/")
556
- // ) {
557
- // const parts = moduleToDownload.split("/", 2);
558
- // const packageDef = await getModuleAndRootDefTypePath(parts[0], config);
559
-
560
- // if (packageDef) {
561
- // acquiredTypeDefs[moduleID] = packageDef.packageJSON;
562
- // const absolutePathForModule = mapRelativePath(
563
- // parts[1] + ".d.ts",
564
- // packageDef.path
565
- // );
566
- // await addModuleToRuntime(packageDef.mod, absolutePathForModule, config);
567
- // }
586
+ } else if ( isPackageSpecificFileImport ) {
587
+ // fix for scheduler/tracing
588
+ const parts = moduleToDownload . split ( "/" , 2 ) ;
589
+ const modname = parts [ 0 ] ;
590
+ const pathName = parts [ 1 ] + ".d.ts" ;
591
+ if ( isBuiltInModule ( moduleName ! ) ) {
592
+ await addBuiltInTypesToRuntime ( modname , pathName , config ) ;
593
+ } else {
594
+ await addModuleToRuntime ( modname , pathName , config ) ;
595
+ }
568
596
} else {
569
597
// E.g. import {Component} from "./MyThing"
570
598
if ( ! moduleToDownload || ! path )
@@ -594,14 +622,16 @@ const getDependenciesForModule = async (
594
622
moduleName ?. startsWith ( "typecell" ) ||
595
623
moduleName ?. startsWith ( "@typecell-org/" )
596
624
) {
597
- await addTypecellModuleToRuntime ( moduleName ! , resolvedFilepath , config ) ;
625
+ await addBuiltInTypesToRuntime ( moduleName ! , resolvedFilepath , config ) ;
626
+ } else if ( isBuiltInModule ( moduleName ! ) ) {
627
+ await addBuiltInTypesToRuntime ( moduleName ! , resolvedFilepath , config ) ;
598
628
} else {
599
629
await addModuleToRuntime ( moduleName ! , resolvedFilepath , config ) ;
600
630
}
601
631
}
602
632
} ) ;
603
633
604
- // Also support the
634
+ // Also support the <reference> comments
605
635
promises . push (
606
636
getReferenceDependencies ( sourceCode , moduleName ! , path ! , config )
607
637
) ;
0 commit comments