@@ -258,7 +258,7 @@ export class Application implements ServerApplication {
258258 skip : [
259259 / ( ^ | \/ | \\ ) \. / ,
260260 / \. 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
262262 ]
263263 }
264264
@@ -399,6 +399,7 @@ export class Application implements ServerApplication {
399399 }
400400 }
401401
402+ /** check the changed file whether it is a scoped module */
402403 private isScopedModule ( url : string ) {
403404 for ( const ext of moduleExts ) {
404405 if ( url . endsWith ( '.' + ext ) ) {
@@ -452,14 +453,15 @@ export class Application implements ServerApplication {
452453 return this . config . plugins . filter ( isLoaderPlugin )
453454 }
454455
455- /** returns the module by given url. */
456+ /** get the module by given url. */
456457 getModule ( url : string ) : Module | null {
457458 if ( this . #modules. has ( url ) ) {
458459 return this . #modules. get ( url ) !
459460 }
460461 return null
461462 }
462463
464+ /** find the module by given name. */
463465 findModuleByName ( name : string ) : Module | null {
464466 for ( const ext of moduleExts ) {
465467 const url = `/${ util . trimPrefix ( name , '/' ) } .${ ext } `
@@ -470,6 +472,7 @@ export class Application implements ServerApplication {
470472 return null
471473 }
472474
475+ /** lookup style deps of given modules. */
473476 lookupStyleModules ( ...urls : string [ ] ) : Module [ ] {
474477 const mods : Module [ ] = [ ]
475478 urls . forEach ( url => {
@@ -483,10 +486,12 @@ export class Application implements ServerApplication {
483486 return mods
484487 }
485488
489+ /** get page route by given location. */
486490 getPageRoute ( location : { pathname : string , search ?: string } ) : [ RouterURL , RouteModule [ ] ] {
487491 return this . #pageRouting. createRouter ( location )
488492 }
489493
494+ /** get api route by given location. */
490495 getAPIRoute ( location : { pathname : string , search ?: string } ) : [ RouterURL , Module ] | null {
491496 const router = this . #apiRouting. createRouter ( location )
492497 if ( router !== null ) {
@@ -568,16 +573,19 @@ export class Application implements ServerApplication {
568573 return [ status , html ]
569574 }
570575
576+ /** get code injects */
571577 getCodeInjects ( phase : 'compilation' | 'hmr' | 'ssr' ) {
572578 return this . #injects. get ( phase )
573579 }
574580
581+ /** create a fs watcher. */
575582 createFSWatcher ( ) : EventEmitter {
576583 const e = new EventEmitter ( )
577584 this . #fsWatchListeners. push ( e )
578585 return e
579586 }
580587
588+ /** remove the fs watcher. */
581589 removeFSWatcher ( e : EventEmitter ) {
582590 e . removeAllListeners ( )
583591 const index = this . #fsWatchListeners. indexOf ( e )
@@ -586,6 +594,7 @@ export class Application implements ServerApplication {
586594 }
587595 }
588596
597+ /** check the module whether it is hmrable. */
589598 isHMRable ( url : string ) {
590599 if ( ! this . isDev || util . isLikelyHttpURL ( url ) ) {
591600 return false
@@ -730,7 +739,8 @@ export class Application implements ServerApplication {
730739 ]
731740 }
732741
733- async resolveModule ( url : string ) {
742+ /** read the module contents. */
743+ async readModule ( url : string ) {
734744 const { content, contentType } = await this . fetchModule ( url )
735745 const source = await this . precompile ( url , content , contentType )
736746 if ( source === null ) {
@@ -739,6 +749,18 @@ export class Application implements ServerApplication {
739749 return source
740750 }
741751
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+
742764 /** default compiler options */
743765 get sharedCompileOptions ( ) : TransformOptions {
744766 return {
@@ -1002,18 +1024,11 @@ export class Application implements ServerApplication {
10021024 }
10031025 }
10041026
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+ */
10171032 private async compile (
10181033 url : string ,
10191034 options : {
@@ -1170,7 +1185,7 @@ export class Application implements ServerApplication {
11701185 if ( jsContent === '' ) {
11711186 jsContent = await Deno . readTextFile ( mod . jsFile )
11721187 }
1173- jsContent = this . replaceDepHash ( jsContent , dep )
1188+ jsContent = this . updateImportUrls ( jsContent , dep )
11741189 if ( ! fsync ) {
11751190 fsync = true
11761191 }
@@ -1179,23 +1194,26 @@ export class Application implements ServerApplication {
11791194 }
11801195 }
11811196
1182- // update hash by deps
1197+ // update hash using deps status
11831198 if ( mod . deps . length > 0 ) {
11841199 mod . hash = computeHash ( mod . sourceHash + mod . deps . map ( ( { hash } ) => hash ) . join ( '' ) )
11851200 }
11861201
11871202 if ( fsync ) {
1188- await lazyRemove ( util . trimSuffix ( mod . jsFile , '.js' ) + '.bundling.js' )
1203+ if ( jsSourceMap ) {
1204+ jsContent += `//# sourceMappingURL=${ basename ( mod . jsFile ) } .map`
1205+ }
11891206 await Promise . all ( [
11901207 ensureTextFile ( metaFile , JSON . stringify ( {
11911208 url,
11921209 deps : mod . deps ,
11931210 sourceHash : mod . sourceHash ,
11941211 isStyle : mod . isStyle ? true : undefined
11951212 } , undefined , 2 ) ) ,
1196- ensureTextFile ( mod . jsFile , jsContent + ( jsSourceMap ? `//# sourceMappingURL= ${ basename ( mod . jsFile ) } .map` : '' ) ) ,
1213+ ensureTextFile ( mod . jsFile , jsContent ) ,
11971214 jsSourceMap ? ensureTextFile ( mod . jsFile + '.map' , jsSourceMap ) : Promise . resolve ( ) ,
11981215 ] )
1216+ await lazyRemove ( util . trimSuffix ( mod . jsFile , '.js' ) + '.bundling.js' )
11991217 }
12001218
12011219 return mod
@@ -1204,11 +1222,10 @@ export class Application implements ServerApplication {
12041222 /** apply compilation side-effect caused by dependency graph breaking. */
12051223 private async applyCompilationSideEffect ( url : string , callback : ( mod : Module ) => void ) {
12061224 const { hash } = this . #modules. get ( url ) !
1207-
12081225 for ( const mod of this . #modules. values ( ) ) {
12091226 for ( const dep of mod . deps ) {
12101227 if ( dep . url === url ) {
1211- const jsContent = this . replaceDepHash (
1228+ const jsContent = this . updateImportUrls (
12121229 await Deno . readTextFile ( mod . jsFile ) ,
12131230 { url, hash }
12141231 )
@@ -1353,7 +1370,8 @@ export class Application implements ServerApplication {
13531370 return ssr
13541371 }
13551372
1356- private replaceDepHash ( jsContent : string , dep : DependencyDescriptor ) {
1373+ /** update the hash in import url of deps. */
1374+ private updateImportUrls ( jsContent : string , dep : DependencyDescriptor ) {
13571375 const s = `.js#${ dep . url } @`
13581376 return jsContent . split ( s ) . map ( ( p , i ) => {
13591377 if ( i > 0 && p . charAt ( 6 ) === '"' ) {
0 commit comments