@@ -23,7 +23,6 @@ export class Application {
23
23
readonly ready : Promise < void >
24
24
25
25
#denoCacheDir = ''
26
- #mainJS = { code : '' , hash : '' }
27
26
#modules: Map < string , Module > = new Map ( )
28
27
#pageRouting: Routing = new Routing ( )
29
28
#apiRouting: Routing = new Routing ( )
@@ -645,19 +644,19 @@ export class Application {
645
644
log . info ( 'Start watching code changes...' )
646
645
for await ( const event of w ) {
647
646
for ( const p of event . paths ) {
648
- const path = util . cleanPath ( util . trimPrefix ( p , this . srcDir ) )
647
+ const url = util . cleanPath ( util . trimPrefix ( p , this . srcDir ) )
649
648
const validated = ( ) => {
650
649
// ignore `.aleph` and output directories
651
- if ( path . startsWith ( '/.aleph/' ) || path . startsWith ( this . config . outputDir ) ) {
650
+ if ( url . startsWith ( '/.aleph/' ) || url . startsWith ( this . config . outputDir ) ) {
652
651
return false
653
652
}
654
653
655
654
// is module
656
- if ( isModuleURL ( path ) ) {
657
- if ( path . startsWith ( '/pages/' ) || path . startsWith ( '/api/' ) ) {
655
+ if ( isModuleURL ( url ) ) {
656
+ if ( url . startsWith ( '/pages/' ) || url . startsWith ( '/api/' ) ) {
658
657
return true
659
658
}
660
- switch ( trimModuleExt ( path ) ) {
659
+ switch ( trimModuleExt ( url ) ) {
661
660
case '/404' :
662
661
case '/app' :
663
662
return true
@@ -666,65 +665,66 @@ export class Application {
666
665
667
666
// is dep
668
667
for ( const { deps } of this . #modules. values ( ) ) {
669
- if ( deps . findIndex ( dep => dep . url === path ) > - 1 ) {
668
+ if ( deps . findIndex ( dep => dep . url === url ) > - 1 ) {
670
669
return true
671
670
}
672
671
}
673
672
674
673
// is loaded by plugin
675
- return this . config . plugins . findIndex ( p => p . type === 'loader' && p . test . test ( path ) ) > - 1
674
+ return this . config . plugins . findIndex ( p => p . type === 'loader' && p . test . test ( url ) ) > - 1
676
675
}
677
676
678
677
if ( validated ( ) ) {
679
- util . debounceX ( path , ( ) => {
678
+ util . debounceX ( url , ( ) => {
680
679
if ( existsFileSync ( p ) ) {
681
680
let type = 'modify'
682
- if ( ! this . #modules. has ( path ) ) {
681
+ if ( ! this . #modules. has ( url ) ) {
683
682
type = 'add'
684
683
}
685
- log . info ( type , path )
686
- this . compile ( path , { forceCompile : true } ) . then ( mod => {
684
+ log . info ( type , url )
685
+ this . compile ( url , { forceCompile : true } ) . then ( mod => {
687
686
const hmrable = this . isHMRable ( mod . url )
687
+ const update = ( { url, hash } : Module ) => {
688
+ if ( trimModuleExt ( url ) === '/app' ) {
689
+ this . #renderCache. clear ( )
690
+ } else if ( url . startsWith ( '/pages/' ) ) {
691
+ this . #renderCache. delete ( toPagePath ( url ) )
692
+ this . #pageRouting. update ( this . getRouteModule ( { url, hash } ) )
693
+ } else if ( url . startsWith ( '/api/' ) ) {
694
+ this . #apiRouting. update ( this . getRouteModule ( { url, hash } ) )
695
+ }
696
+ }
688
697
if ( hmrable ) {
689
698
if ( type === 'add' ) {
690
699
this . #fsWatchListeners. forEach ( e => e . emit ( 'add' , this . getRouteModule ( mod ) ) )
691
700
} else {
692
701
this . #fsWatchListeners. forEach ( e => e . emit ( 'modify-' + mod . url , mod . hash ) )
693
702
}
694
703
}
695
- if ( trimModuleExt ( path ) === '/app' ) {
696
- this . #renderCache. clear ( )
697
- } else if ( path . startsWith ( '/pages/' ) ) {
698
- this . #renderCache. delete ( toPagePath ( path ) )
699
- this . #pageRouting. update ( this . getRouteModule ( mod ) )
700
- } else if ( path . startsWith ( '/api/' ) ) {
701
- this . #apiRouting. update ( this . getRouteModule ( mod ) )
702
- }
703
- this . checkCompilationSideEffect ( path , ( { url, hash } ) => {
704
- if ( url . startsWith ( '/pages/' ) ) {
705
- this . #renderCache. delete ( toPagePath ( url ) )
706
- }
707
- if ( ! hmrable && this . isHMRable ( url ) ) {
708
- this . #fsWatchListeners. forEach ( w => w . emit ( 'modify-' + url , hash ) )
704
+ update ( mod )
705
+ this . checkCompilationSideEffect ( url , ( mod ) => {
706
+ update ( mod )
707
+ if ( ! hmrable && this . isHMRable ( mod . url ) ) {
708
+ this . #fsWatchListeners. forEach ( w => w . emit ( 'modify-' + mod . url , mod . hash ) )
709
709
}
710
710
} )
711
711
} ) . catch ( err => {
712
- log . error ( `compile(${ path } ):` , err . message )
712
+ log . error ( `compile(${ url } ):` , err . message )
713
713
} )
714
- } else if ( this . #modules. has ( path ) ) {
715
- if ( trimModuleExt ( path ) === '/app' ) {
714
+ } else if ( this . #modules. has ( url ) ) {
715
+ if ( trimModuleExt ( url ) === '/app' ) {
716
716
this . #renderCache. clear ( )
717
- } else if ( path . startsWith ( '/pages/' ) ) {
718
- this . #renderCache. delete ( toPagePath ( path ) )
719
- this . #pageRouting. removeRoute ( toPagePath ( path ) )
720
- } else if ( path . startsWith ( '/api/' ) ) {
721
- this . #apiRouting. removeRoute ( toPagePath ( path ) )
717
+ } else if ( url . startsWith ( '/pages/' ) ) {
718
+ this . #renderCache. delete ( toPagePath ( url ) )
719
+ this . #pageRouting. removeRoute ( toPagePath ( url ) )
720
+ } else if ( url . startsWith ( '/api/' ) ) {
721
+ this . #apiRouting. removeRoute ( toPagePath ( url ) )
722
722
}
723
- this . #modules. delete ( path )
724
- if ( this . isHMRable ( path ) ) {
725
- this . #fsWatchListeners. forEach ( e => e . emit ( 'remove' , path ) )
723
+ this . #modules. delete ( url )
724
+ if ( this . isHMRable ( url ) ) {
725
+ this . #fsWatchListeners. forEach ( e => e . emit ( 'remove' , url ) )
726
726
}
727
- log . info ( 'remove' , path )
727
+ log . info ( 'remove' , url )
728
728
}
729
729
} , 150 )
730
730
}
@@ -733,7 +733,7 @@ export class Application {
733
733
}
734
734
735
735
/** returns the route module by given module. */
736
- private getRouteModule ( { url, hash } : Module ) : RouteModule {
736
+ private getRouteModule ( { url, hash } : Pick < Module , 'url' | 'hash' > ) : RouteModule {
737
737
const deps = this . lookupDeps ( url ) . filter ( ( { isData, isStyle, isDynamic } ) => ! ! ( isData || ( isStyle && isDynamic ) ) )
738
738
return { url, hash, asyncDeps : deps . length > 0 ? deps : undefined }
739
739
}
0 commit comments