@@ -2,8 +2,8 @@ import { buildChecksum, initWasm, SWCOptions, TransformOptions, transpileSync }
2
2
import type { AcceptedPlugin , ECMA } from '../deps.ts'
3
3
import { CleanCSS , colors , ensureDir , minify , path , postcss , Sha256 , walk } from '../deps.ts'
4
4
import { EventEmitter } from '../framework/core/events.ts'
5
- import { isModuleURL , RouteModule , Routing , toPagePath , trimModuleExt } from '../framework/core/routing.ts'
6
- import { defaultReactVersion , minDenoVersion , pageModuleExts } from '../shared/constants.ts'
5
+ import { isModuleURL , RouteModule , Routing , toPagePath } from '../framework/core/routing.ts'
6
+ import { defaultReactVersion , minDenoVersion , moduleExts } from '../shared/constants.ts'
7
7
import { ensureTextFile , existsDirSync , existsFileSync , lazyRemove } from '../shared/fs.ts'
8
8
import log from '../shared/log.ts'
9
9
import util from '../shared/util.ts'
@@ -87,8 +87,8 @@ export class Application {
87
87
if ( url . endsWith ( '.' + ext ) ) {
88
88
return url . startsWith ( '/pages/' ) ||
89
89
url . startsWith ( '/components/' ) ||
90
- trimModuleExt ( url ) === '/app' ||
91
- trimModuleExt ( url ) === '/404'
90
+ util . trimModuleExt ( url ) === '/app' ||
91
+ util . trimModuleExt ( url ) === '/404'
92
92
}
93
93
}
94
94
for ( const plugin of this . config . plugins ) {
@@ -117,7 +117,7 @@ export class Application {
117
117
if ( / [ a - z ] / . test ( ext ) ) {
118
118
if ( ext === 'css' || ext === 'pcss' ) {
119
119
mod . loader = 'css'
120
- } else if ( pageModuleExts . includes ( ext ) ) {
120
+ } else if ( moduleExts . includes ( ext ) ) {
121
121
if ( ext === 'mjs' ) {
122
122
mod . loader = 'js'
123
123
} else {
@@ -506,7 +506,7 @@ export class Application {
506
506
const t = performance . now ( )
507
507
const alephPkgUrl = getAlephPkgUrl ( )
508
508
const { env, framework, plugins, ssr } = this . config
509
- const walkOptions = { includeDirs : false , exts : [ '.ts' , '.js' , '.mjs' ] , skip : [ / ^ \. / , / \. d \. t s $ / i, / \. ( t e s t | s p e c | e 2 e ) \. m ? ( j | t ) s x ? $ / i] }
509
+ const walkOptions = { includeDirs : false , exts : moduleExts , skip : [ / ^ \. / , / \. d \. t s $ / i, / \. ( t e s t | s p e c | e 2 e ) \. m ? ( j | t ) s x ? $ / i] }
510
510
const apiDir = path . join ( this . srcDir , 'api' )
511
511
const pagesDir = path . join ( this . srcDir , 'pages' )
512
512
@@ -561,20 +561,14 @@ export class Application {
561
561
}
562
562
563
563
// check custom components
564
- for await ( const { path : p , } of walk ( this . srcDir , { ...walkOptions , maxDepth : 1 , exts : [ ... walkOptions . exts , '.tsx' , '.jsx' ] } ) ) {
564
+ for await ( const { path : p , } of walk ( this . srcDir , { ...walkOptions , maxDepth : 1 } ) ) {
565
565
const name = path . basename ( p )
566
- let isCustom = true
567
- switch ( trimModuleExt ( name ) ) {
566
+ switch ( util . trimModuleExt ( name ) ) {
568
567
case 'app' :
569
568
case '404' :
570
569
case 'loading' :
570
+ await this . compile ( '/' + name )
571
571
break
572
- default :
573
- isCustom = false
574
- break
575
- }
576
- if ( isCustom ) {
577
- await this . compile ( '/' + name )
578
572
}
579
573
}
580
574
@@ -587,7 +581,7 @@ export class Application {
587
581
}
588
582
589
583
// create page routing
590
- for await ( const { path : p } of walk ( pagesDir , { ...walkOptions , exts : [ ... walkOptions . exts , '.tsx' , '.jsx' , '.md' ] } ) ) {
584
+ for await ( const { path : p } of walk ( pagesDir , { ...walkOptions } ) ) {
591
585
const mod = await this . compile ( util . cleanPath ( '/pages/' + util . trimPrefix ( p , pagesDir ) ) )
592
586
this . #pageRouting. update ( this . getRouteModule ( mod ) )
593
587
}
@@ -626,15 +620,10 @@ export class Application {
626
620
627
621
log . debug ( `init project in ${ Math . round ( performance . now ( ) - t ) } ms` )
628
622
629
- if ( this . isDev ) {
630
- this . watch ( )
631
- } else {
632
- this . #modules. forEach ( ( { url } ) => {
633
- if ( ! util . isLikelyHttpURL ( url ) ) {
634
- this . checkCompilationSideEffect ( url )
635
- }
636
- } )
623
+ if ( ! this . isDev ) {
637
624
await this . bundle ( )
625
+ } else {
626
+ this . watch ( )
638
627
}
639
628
}
640
629
@@ -656,7 +645,7 @@ export class Application {
656
645
if ( url . startsWith ( '/pages/' ) || url . startsWith ( '/api/' ) ) {
657
646
return true
658
647
}
659
- switch ( trimModuleExt ( url ) ) {
648
+ switch ( util . trimModuleExt ( url ) ) {
660
649
case '/404' :
661
650
case '/app' :
662
651
return true
@@ -685,12 +674,14 @@ export class Application {
685
674
this . compile ( url , { forceCompile : true } ) . then ( mod => {
686
675
const hmrable = this . isHMRable ( mod . url )
687
676
const update = ( { url, hash } : Module ) => {
688
- if ( trimModuleExt ( url ) === '/app' ) {
677
+ if ( util . trimModuleExt ( url ) === '/app' ) {
689
678
this . #renderCache. clear ( )
690
679
} else if ( url . startsWith ( '/pages/' ) ) {
691
680
this . #renderCache. delete ( toPagePath ( url ) )
692
- this . #pageRouting. update ( this . getRouteModule ( { url, hash } ) )
693
- } else if ( url . startsWith ( '/api/' ) ) {
681
+ if ( type === 'add' ) {
682
+ this . #pageRouting. update ( this . getRouteModule ( { url, hash } ) )
683
+ }
684
+ } else if ( url . startsWith ( '/api/' ) && type === 'add' ) {
694
685
this . #apiRouting. update ( this . getRouteModule ( { url, hash } ) )
695
686
}
696
687
}
@@ -712,7 +703,7 @@ export class Application {
712
703
log . error ( `compile(${ url } ):` , err . message )
713
704
} )
714
705
} else if ( this . #modules. has ( url ) ) {
715
- if ( trimModuleExt ( url ) === '/app' ) {
706
+ if ( util . trimModuleExt ( url ) === '/app' ) {
716
707
this . #renderCache. clear ( )
717
708
} else if ( url . startsWith ( '/pages/' ) ) {
718
709
this . #renderCache. delete ( toPagePath ( url ) )
@@ -748,7 +739,7 @@ export class Application {
748
739
locales : [ ] ,
749
740
routes : this . #pageRouting. routes ,
750
741
sharedModules : Array . from ( this . #modules. values ( ) ) . filter ( ( { url } ) => {
751
- switch ( trimModuleExt ( url ) ) {
742
+ switch ( util . trimModuleExt ( url ) ) {
752
743
case '/404' :
753
744
case '/app' :
754
745
return true
@@ -777,7 +768,7 @@ export class Application {
777
768
if ( isRemote ) {
778
769
const url = new URL ( importUrl )
779
770
let pathname = url . pathname
780
- let ok = [ ...pageModuleExts , 'css' , 'pcss' ] . includes ( path . extname ( pathname ) . slice ( 1 ) )
771
+ let ok = [ ...moduleExts , 'css' , 'pcss' ] . includes ( path . extname ( pathname ) . slice ( 1 ) )
781
772
if ( ok ) {
782
773
for ( const plugin of this . config . plugins ) {
783
774
if ( plugin . type === 'loader' && plugin . test . test ( pathname ) ) {
@@ -887,7 +878,7 @@ export class Application {
887
878
const alephPkgUrl = getAlephPkgUrl ( )
888
879
const isRemote = util . isLikelyHttpURL ( url )
889
880
const localUrl = this . fixImportUrl ( url )
890
- const name = trimModuleExt ( path . basename ( localUrl ) )
881
+ const name = util . trimModuleExt ( path . basename ( localUrl ) )
891
882
const saveDir = path . join ( this . buildDir , path . dirname ( localUrl ) )
892
883
const metaFile = path . join ( saveDir , `${ name } .meta.json` )
893
884
const { sourceCode, forceCompile, bundleMode, bundledModules } = options ?? { }
@@ -1131,7 +1122,7 @@ export class Application {
1131
1122
if ( ! util . isLikelyHttpURL ( dep . url ) ) {
1132
1123
const relativePathname = getRelativePath (
1133
1124
path . dirname ( url ) ,
1134
- trimModuleExt ( dep . url )
1125
+ util . trimModuleExt ( dep . url )
1135
1126
)
1136
1127
if ( ! changed && jsContent === '' ) {
1137
1128
if ( ! bundleMode ) {
@@ -1195,7 +1186,6 @@ export class Application {
1195
1186
}
1196
1187
1197
1188
/** check compilation side-effect caused by dependency graph. */
1198
- // use browser native import maps to hand changes in the future.
1199
1189
private async checkCompilationSideEffect ( url : string , callback ?: ( mod : Module ) => void ) {
1200
1190
const { hash, sourceHash } = this . #modules. get ( url ) !
1201
1191
@@ -1207,7 +1197,7 @@ export class Application {
1207
1197
dep . hash = hash
1208
1198
const relativePath = getRelativePath (
1209
1199
path . dirname ( mod . url ) ,
1210
- trimModuleExt ( dep . url )
1200
+ util . trimModuleExt ( dep . url )
1211
1201
)
1212
1202
let jsContent = await Deno . readTextFile ( mod . jsFile )
1213
1203
jsContent = jsContent . replace ( reHashResolve , ( s , key , spaces , ql , importPath , qr ) => {
@@ -1312,8 +1302,8 @@ export class Application {
1312
1302
}
1313
1303
}
1314
1304
const mods = Array . from ( this . #modules. values ( ) )
1315
- const appModule = mods . find ( ( { url } ) => trimModuleExt ( url ) == '/app' )
1316
- const e404Module = mods . find ( ( { url } ) => trimModuleExt ( url ) == '/404' )
1305
+ const appModule = mods . find ( ( { url } ) => util . trimModuleExt ( url ) == '/app' )
1306
+ const e404Module = mods . find ( ( { url } ) => util . trimModuleExt ( url ) == '/404' )
1317
1307
const pageModules : Module [ ] = [ ]
1318
1308
1319
1309
// add framework bootstrap
@@ -1608,7 +1598,7 @@ export class Application {
1608
1598
return await this . render404Page ( url )
1609
1599
}
1610
1600
try {
1611
- const appModule = Array . from ( this . #modules. values ( ) ) . find ( ( { url } ) => trimModuleExt ( url ) == '/app' )
1601
+ const appModule = Array . from ( this . #modules. values ( ) ) . find ( ( { url } ) => util . trimModuleExt ( url ) == '/app' )
1612
1602
const { default : App } = appModule ? await import ( 'file://' + appModule . jsFile ) : { } as any
1613
1603
const imports = pageModuleChain . map ( async ( { url } ) => {
1614
1604
const mod = this . #modules. get ( url ) !
@@ -1678,7 +1668,7 @@ export class Application {
1678
1668
const ret : RenderResult = { url, status : 404 , head : [ ] , scripts : [ ] , body : '<div id="__aleph"></div>' , data : null }
1679
1669
try {
1680
1670
const e404Module = Array . from ( this . #modules. keys ( ) )
1681
- . filter ( url => trimModuleExt ( url ) == '/404' )
1671
+ . filter ( url => util . trimModuleExt ( url ) == '/404' )
1682
1672
. map ( url => this . #modules. get ( url ) ) [ 0 ]
1683
1673
const { default : E404 } = e404Module ? await import ( 'file://' + e404Module . jsFile ) : { } as any
1684
1674
const { head, body, data, scripts } = await this . #renderer. render (
@@ -1708,9 +1698,7 @@ export class Application {
1708
1698
1709
1699
/** render custom loading page for SPA mode. */
1710
1700
private async renderLoadingPage ( ) {
1711
- const loadingModule = Array . from ( this . #modules. keys ( ) )
1712
- . filter ( url => trimModuleExt ( url ) == '/loading' )
1713
- . map ( url => this . #modules. get ( url ) ) [ 0 ]
1701
+ const loadingModule = Array . from ( this . #modules. values ( ) ) . find ( ( { url } ) => util . trimModuleExt ( url ) === '/loading' )
1714
1702
if ( loadingModule ) {
1715
1703
const { default : Loading } = await import ( 'file://' + loadingModule . jsFile )
1716
1704
const router = {
0 commit comments