@@ -258,7 +258,7 @@ export class Application implements ServerApplication {
258
258
skip : [
259
259
/ ( ^ | \/ | \\ ) \. / ,
260
260
/ \. d \. t s $ / i,
261
- / ( \. | _ ) ( t e s t | s p e c | e 2 e ) \. ( t s x ? | j s x ? | m j s ) ? $ / i
261
+ / ( \. | _ ) ( t e s t | s p e c | e 2 e ) \. [ a - z ] + $ / i
262
262
]
263
263
}
264
264
@@ -399,6 +399,7 @@ export class Application implements ServerApplication {
399
399
}
400
400
}
401
401
402
+ /** check the changed file whether it is a scoped module */
402
403
private isScopedModule ( url : string ) {
403
404
for ( const ext of moduleExts ) {
404
405
if ( url . endsWith ( '.' + ext ) ) {
@@ -452,14 +453,15 @@ export class Application implements ServerApplication {
452
453
return this . config . plugins . filter ( isLoaderPlugin )
453
454
}
454
455
455
- /** returns the module by given url. */
456
+ /** get the module by given url. */
456
457
getModule ( url : string ) : Module | null {
457
458
if ( this . #modules. has ( url ) ) {
458
459
return this . #modules. get ( url ) !
459
460
}
460
461
return null
461
462
}
462
463
464
+ /** find the module by given name. */
463
465
findModuleByName ( name : string ) : Module | null {
464
466
for ( const ext of moduleExts ) {
465
467
const url = `/${ util . trimPrefix ( name , '/' ) } .${ ext } `
@@ -470,6 +472,7 @@ export class Application implements ServerApplication {
470
472
return null
471
473
}
472
474
475
+ /** lookup style deps of given modules. */
473
476
lookupStyleModules ( ...urls : string [ ] ) : Module [ ] {
474
477
const mods : Module [ ] = [ ]
475
478
urls . forEach ( url => {
@@ -483,10 +486,12 @@ export class Application implements ServerApplication {
483
486
return mods
484
487
}
485
488
489
+ /** get page route by given location. */
486
490
getPageRoute ( location : { pathname : string , search ?: string } ) : [ RouterURL , RouteModule [ ] ] {
487
491
return this . #pageRouting. createRouter ( location )
488
492
}
489
493
494
+ /** get api route by given location. */
490
495
getAPIRoute ( location : { pathname : string , search ?: string } ) : [ RouterURL , Module ] | null {
491
496
const router = this . #apiRouting. createRouter ( location )
492
497
if ( router !== null ) {
@@ -568,16 +573,19 @@ export class Application implements ServerApplication {
568
573
return [ status , html ]
569
574
}
570
575
576
+ /** get code injects */
571
577
getCodeInjects ( phase : 'compilation' | 'hmr' | 'ssr' ) {
572
578
return this . #injects. get ( phase )
573
579
}
574
580
581
+ /** create a fs watcher. */
575
582
createFSWatcher ( ) : EventEmitter {
576
583
const e = new EventEmitter ( )
577
584
this . #fsWatchListeners. push ( e )
578
585
return e
579
586
}
580
587
588
+ /** remove the fs watcher. */
581
589
removeFSWatcher ( e : EventEmitter ) {
582
590
e . removeAllListeners ( )
583
591
const index = this . #fsWatchListeners. indexOf ( e )
@@ -586,6 +594,7 @@ export class Application implements ServerApplication {
586
594
}
587
595
}
588
596
597
+ /** check the module whether it is hmrable. */
589
598
isHMRable ( url : string ) {
590
599
if ( ! this . isDev || util . isLikelyHttpURL ( url ) ) {
591
600
return false
@@ -730,7 +739,8 @@ export class Application implements ServerApplication {
730
739
]
731
740
}
732
741
733
- async resolveModule ( url : string ) {
742
+ /** read the module contents. */
743
+ async readModule ( url : string ) {
734
744
const { content, contentType } = await this . fetchModule ( url )
735
745
const source = await this . precompile ( url , content , contentType )
736
746
if ( source === null ) {
@@ -739,6 +749,18 @@ export class Application implements ServerApplication {
739
749
return source
740
750
}
741
751
752
+ /** parse the export names of the module. */
753
+ async parseModuleExportNames ( url : string ) : Promise < string [ ] > {
754
+ const source = await this . readModule ( url )
755
+ const names = await parseExportNames ( url , source . code , { sourceType : source . type } )
756
+ return ( await Promise . all ( names . map ( async name => {
757
+ if ( name . startsWith ( '{' ) && name . startsWith ( '}' ) ) {
758
+ return await this . parseModuleExportNames ( name . slice ( 1 , - 1 ) )
759
+ }
760
+ return name
761
+ } ) ) ) . flat ( )
762
+ }
763
+
742
764
/** default compiler options */
743
765
get sharedCompileOptions ( ) : TransformOptions {
744
766
return {
@@ -1002,18 +1024,11 @@ export class Application implements ServerApplication {
1002
1024
}
1003
1025
}
1004
1026
1005
- async parseModuleExportNames ( url : string ) : Promise < string [ ] > {
1006
- const source = await this . resolveModule ( url )
1007
- const names = await parseExportNames ( url , source . code , { sourceType : source . type } )
1008
- return ( await Promise . all ( names . map ( async name => {
1009
- if ( name . startsWith ( '{' ) && name . startsWith ( '}' ) ) {
1010
- return await this . parseModuleExportNames ( name . slice ( 1 , - 1 ) )
1011
- }
1012
- return name
1013
- } ) ) ) . flat ( )
1014
- }
1015
-
1016
- /** compile a moudle by given url, then cache on the disk. */
1027
+ /**
1028
+ * compile a moudle by given url, then cache on the disk.
1029
+ * each moudle only be compiled once unless you set the
1030
+ * `forceCompile` option to true.
1031
+ */
1017
1032
private async compile (
1018
1033
url : string ,
1019
1034
options : {
@@ -1170,7 +1185,7 @@ export class Application implements ServerApplication {
1170
1185
if ( jsContent === '' ) {
1171
1186
jsContent = await Deno . readTextFile ( mod . jsFile )
1172
1187
}
1173
- jsContent = this . replaceDepHash ( jsContent , dep )
1188
+ jsContent = this . updateImportUrls ( jsContent , dep )
1174
1189
if ( ! fsync ) {
1175
1190
fsync = true
1176
1191
}
@@ -1179,23 +1194,26 @@ export class Application implements ServerApplication {
1179
1194
}
1180
1195
}
1181
1196
1182
- // update hash by deps
1197
+ // update hash using deps status
1183
1198
if ( mod . deps . length > 0 ) {
1184
1199
mod . hash = computeHash ( mod . sourceHash + mod . deps . map ( ( { hash } ) => hash ) . join ( '' ) )
1185
1200
}
1186
1201
1187
1202
if ( fsync ) {
1188
- await lazyRemove ( util . trimSuffix ( mod . jsFile , '.js' ) + '.bundling.js' )
1203
+ if ( jsSourceMap ) {
1204
+ jsContent += `//# sourceMappingURL=${ basename ( mod . jsFile ) } .map`
1205
+ }
1189
1206
await Promise . all ( [
1190
1207
ensureTextFile ( metaFile , JSON . stringify ( {
1191
1208
url,
1192
1209
deps : mod . deps ,
1193
1210
sourceHash : mod . sourceHash ,
1194
1211
isStyle : mod . isStyle ? true : undefined
1195
1212
} , undefined , 2 ) ) ,
1196
- ensureTextFile ( mod . jsFile , jsContent + ( jsSourceMap ? `//# sourceMappingURL= ${ basename ( mod . jsFile ) } .map` : '' ) ) ,
1213
+ ensureTextFile ( mod . jsFile , jsContent ) ,
1197
1214
jsSourceMap ? ensureTextFile ( mod . jsFile + '.map' , jsSourceMap ) : Promise . resolve ( ) ,
1198
1215
] )
1216
+ await lazyRemove ( util . trimSuffix ( mod . jsFile , '.js' ) + '.bundling.js' )
1199
1217
}
1200
1218
1201
1219
return mod
@@ -1204,11 +1222,10 @@ export class Application implements ServerApplication {
1204
1222
/** apply compilation side-effect caused by dependency graph breaking. */
1205
1223
private async applyCompilationSideEffect ( url : string , callback : ( mod : Module ) => void ) {
1206
1224
const { hash } = this . #modules. get ( url ) !
1207
-
1208
1225
for ( const mod of this . #modules. values ( ) ) {
1209
1226
for ( const dep of mod . deps ) {
1210
1227
if ( dep . url === url ) {
1211
- const jsContent = this . replaceDepHash (
1228
+ const jsContent = this . updateImportUrls (
1212
1229
await Deno . readTextFile ( mod . jsFile ) ,
1213
1230
{ url, hash }
1214
1231
)
@@ -1353,7 +1370,8 @@ export class Application implements ServerApplication {
1353
1370
return ssr
1354
1371
}
1355
1372
1356
- private replaceDepHash ( jsContent : string , dep : DependencyDescriptor ) {
1373
+ /** update the hash in import url of deps. */
1374
+ private updateImportUrls ( jsContent : string , dep : DependencyDescriptor ) {
1357
1375
const s = `.js#${ dep . url } @`
1358
1376
return jsContent . split ( s ) . map ( ( p , i ) => {
1359
1377
if ( i > 0 && p . charAt ( 6 ) === '"' ) {
0 commit comments