@@ -177,7 +177,7 @@ export default class Project {
177
177
}
178
178
)
179
179
if ( url . pagePath === '' ) {
180
- return [ 200 , this . getSPAIndexHtml ( ) ]
180
+ return [ 200 , this . getDefaultIndexHtml ( ) ]
181
181
}
182
182
183
183
const mainModule = this . #modules. get ( './main.js' ) !
@@ -195,7 +195,7 @@ export default class Project {
195
195
return [ code , html ]
196
196
}
197
197
198
- getSPAIndexHtml ( ) : string {
198
+ getDefaultIndexHtml ( ) : string {
199
199
const { baseUrl, defaultLocale } = this . config
200
200
const mainModule = this . #modules. get ( './main.js' ) !
201
201
const html = createHtml ( {
@@ -226,16 +226,6 @@ export default class Project {
226
226
return { }
227
227
}
228
228
229
- async importModuleAsComponent ( moduleId : string ) {
230
- if ( this . #modules. has ( moduleId ) ) {
231
- const { default : Component } = await import ( this . #modules. get ( moduleId ) ! . jsFile )
232
- if ( util . isLikelyReactComponent ( Component ) ) {
233
- return { Component }
234
- }
235
- }
236
- return { }
237
- }
238
-
239
229
async build ( ) {
240
230
const start = performance . now ( )
241
231
const outputDir = path . join ( this . srcDir , this . config . outputDir )
@@ -246,7 +236,7 @@ export default class Project {
246
236
outputModules . add ( moduleId )
247
237
const { deps } = this . #modules. get ( moduleId ) !
248
238
deps . forEach ( ( { url } ) => {
249
- const { id } = this . _parseUrl ( url )
239
+ const { id } = this . _newModule ( url )
250
240
lookup ( id )
251
241
} )
252
242
}
@@ -292,9 +282,9 @@ export default class Project {
292
282
await writeTextFile ( htmlFile , html )
293
283
}
294
284
const fallback = path . join ( outputDir , util . isPlainObject ( ssr ) && ssr . fallback ? ssr . fallback : '404.html' )
295
- await writeTextFile ( fallback , this . getSPAIndexHtml ( ) )
285
+ await writeTextFile ( fallback , this . getDefaultIndexHtml ( ) )
296
286
} else {
297
- await writeTextFile ( path . join ( outputDir , 'index.html' ) , this . getSPAIndexHtml ( ) )
287
+ await writeTextFile ( path . join ( outputDir , 'index.html' ) , this . getDefaultIndexHtml ( ) )
298
288
}
299
289
300
290
// copy public files
@@ -561,6 +551,27 @@ export default class Project {
561
551
}
562
552
}
563
553
554
+ private _newModule ( url : string ) : Module {
555
+ const { importMap } = this . config
556
+ const isRemote = reHttp . test ( url ) || ( url in importMap . imports && reHttp . test ( importMap . imports [ url ] ) )
557
+ const sourceFilePath = renameImportUrl ( url )
558
+ const id = ( isRemote ? '//' + util . trimPrefix ( sourceFilePath , '/-/' ) : '.' + sourceFilePath ) . replace ( reModuleExt , '.js' )
559
+
560
+ return {
561
+ id,
562
+ url,
563
+ isRemote,
564
+ sourceFilePath,
565
+ sourceType : path . extname ( sourceFilePath ) . slice ( 1 ) . replace ( 'mjs' , 'js' ) || 'js' ,
566
+ sourceHash : '' ,
567
+ deps : [ ] ,
568
+ jsFile : '' ,
569
+ jsContent : '' ,
570
+ jsSourceMap : '' ,
571
+ hash : '' ,
572
+ } as Module
573
+ }
574
+
564
575
private async _createMainModule ( ) : Promise < Module > {
565
576
const { baseUrl, defaultLocale } = this . config
566
577
const config : Record < string , any > = {
@@ -570,7 +581,7 @@ export default class Project {
570
581
keyModules : { } ,
571
582
pageModules : { }
572
583
}
573
- const module = this . _parseUrl ( './main.js' )
584
+ const module = this . _newModule ( './main.js' )
574
585
const deps = [
575
586
'https://deno.land/x/aleph/vendor/tslib/tslib.js' ,
576
587
'https://deno.land/x/aleph/app.ts' ,
@@ -634,29 +645,8 @@ export default class Project {
634
645
return module
635
646
}
636
647
637
- private _parseUrl ( url : string ) : Module {
638
- const { importMap } = this . config
639
- const isRemote = reHttp . test ( url ) || ( url in importMap . imports && reHttp . test ( importMap . imports [ url ] ) )
640
- const sourceFilePath = renameImportUrl ( url )
641
- const id = ( isRemote ? '//' + util . trimPrefix ( sourceFilePath , '/-/' ) : '.' + sourceFilePath ) . replace ( reModuleExt , '.js' )
642
-
643
- return {
644
- id,
645
- url,
646
- isRemote,
647
- sourceFilePath,
648
- sourceType : path . extname ( sourceFilePath ) . slice ( 1 ) . replace ( 'mjs' , 'js' ) || 'js' ,
649
- sourceHash : '' ,
650
- deps : [ ] ,
651
- jsFile : '' ,
652
- jsContent : '' ,
653
- jsSourceMap : '' ,
654
- hash : '' ,
655
- } as Module
656
- }
657
-
658
648
private async _compile ( url : string , options ?: { sourceCode ?: string , implicitDeps ?: { url : string , hash : string } [ ] , forceCompile ?: boolean } ) {
659
- const mod = this . _parseUrl ( url )
649
+ const mod = this . _newModule ( url )
660
650
if ( this . #modules. has ( mod . id ) && ! options ?. forceCompile ) {
661
651
return this . #modules. get ( mod . id ) !
662
652
}
@@ -996,33 +986,33 @@ export default class Project {
996
986
private async _renderPage ( url : RouterURL ) {
997
987
const start = performance . now ( )
998
988
const ret : RenderResult = { code : 200 , head : [ ] , body : '<main></main>' }
999
- const pm = this . #pageModules. get ( url . pagePath ) !
1000
- if ( pm . rendered . has ( url . pathname ) ) {
1001
- const cache = pm . rendered . get ( url . pathname ) !
989
+ const page = this . #pageModules. get ( url . pagePath ) !
990
+ if ( page . rendered . has ( url . pathname ) ) {
991
+ const cache = page . rendered . get ( url . pathname ) !
1002
992
return { ...cache }
1003
993
}
1004
994
try {
1005
995
const appModule = this . #modules. get ( './app.js' )
1006
- const pageModule = this . #modules. get ( pm . moduleId ) !
996
+ const pageModule = this . #modules. get ( page . moduleId ) !
1007
997
const [
1008
998
{ renderPage, renderHead } ,
1009
- app ,
1010
- page
999
+ { default : App } ,
1000
+ { default : Page }
1011
1001
] = await Promise . all ( [
1012
1002
import ( this . #modules. get ( '//deno.land/x/aleph/renderer.js' ) ! . jsFile ) ,
1013
- appModule ? this . importModuleAsComponent ( './app.js' ) : Promise . resolve ( { } ) ,
1014
- this . importModuleAsComponent ( pm . moduleId )
1003
+ appModule ? await import ( appModule . jsFile ) : Promise . resolve ( { } ) ,
1004
+ await import ( pageModule . jsFile )
1015
1005
] )
1016
1006
const data = await this . getData ( )
1017
- const html = renderPage ( data , url , appModule ? app : undefined , page )
1007
+ const html = renderPage ( data , url , appModule ? App : undefined , Page )
1018
1008
const head = renderHead ( [
1019
1009
pageModule . deps . map ( ( { url } ) => url ) . filter ( url => reStyleModuleExt . test ( url ) ) ,
1020
1010
appModule ?. deps . map ( ( { url } ) => url ) . filter ( url => reStyleModuleExt . test ( url ) )
1021
1011
] . filter ( Boolean ) . flat ( ) )
1022
1012
ret . code = 200
1023
1013
ret . head = head
1024
1014
ret . body = `<main>${ html } </main>`
1025
- pm . rendered . set ( url . pathname , { ...ret } )
1015
+ page . rendered . set ( url . pathname , { ...ret } )
1026
1016
log . debug ( `render page '${ url . pagePath } ' in ${ Math . round ( performance . now ( ) - start ) } ms` )
1027
1017
} catch ( err ) {
1028
1018
ret . code = 500
0 commit comments