@@ -211,7 +211,7 @@ export class Project {
211
211
return null
212
212
}
213
213
214
- async getPageData ( loc : { pathname : string , search ?: string } ) : Promise < [ number , any ] > {
214
+ async getSSRData ( loc : { pathname : string , search ?: string } ) : Promise < [ number , any ] > {
215
215
if ( ! this . isSSRable ( loc . pathname ) ) {
216
216
return [ 404 , null ]
217
217
}
@@ -220,10 +220,10 @@ export class Project {
220
220
return [ status , data ]
221
221
}
222
222
223
- async getPageHtml ( loc : { pathname : string , search ?: string } ) : Promise < [ number , string ] > {
223
+ async getPageHtml ( loc : { pathname : string , search ?: string } ) : Promise < [ number , string , Record < string , string > | null ] > {
224
224
if ( ! this . isSSRable ( loc . pathname ) ) {
225
225
const [ url ] = this . #routing. createRouter ( loc )
226
- return [ url . pagePath === '' ? 404 : 200 , await this . getSPAIndexHtml ( ) ]
226
+ return [ url . pagePath === '' ? 404 : 200 , await this . getSPAIndexHtml ( ) , null ]
227
227
}
228
228
229
229
const { baseUrl } = this . config
@@ -240,7 +240,7 @@ export class Project {
240
240
body,
241
241
minify : ! this . isDev
242
242
} )
243
- return [ status , html ]
243
+ return [ status , html , data ]
244
244
}
245
245
246
246
async getSPAIndexHtml ( ) {
@@ -305,13 +305,12 @@ export class Project {
305
305
}
306
306
await Promise . all ( Array . from ( paths ) . map ( async pathname => {
307
307
if ( this . isSSRable ( pathname ) ) {
308
- const [ status , html ] = await this . getPageHtml ( { pathname } )
308
+ const [ status , html , data ] = await this . getPageHtml ( { pathname } )
309
309
if ( status == 200 ) {
310
- const [ _ , data ] = await this . getPageData ( { pathname } )
311
310
const htmlFile = path . join ( outputDir , pathname , 'index.html' )
312
- const dataFile = path . join ( outputDir , '_aleph/data' , pathname , 'data.js' )
313
311
await writeTextFile ( htmlFile , html )
314
312
if ( data ) {
313
+ const dataFile = path . join ( outputDir , '_aleph/data' , pathname , 'data.js' )
315
314
await writeTextFile ( dataFile , `export default ` + JSON . stringify ( data ) )
316
315
}
317
316
log . info ( ' ○' , pathname , colors . dim ( '• ' + util . bytesString ( html . length ) ) )
@@ -548,7 +547,6 @@ export class Project {
548
547
549
548
const precompileUrls = [
550
549
'https://deno.land/x/aleph/bootstrap.ts' ,
551
- 'https://deno.land/x/aleph/renderer.ts' ,
552
550
'https://deno.land/x/aleph/nomodule.ts' ,
553
551
'https://deno.land/x/aleph/tsc/tslib.js' ,
554
552
]
@@ -558,9 +556,9 @@ export class Project {
558
556
for ( const url of precompileUrls ) {
559
557
await this . _compile ( url )
560
558
}
559
+ await this . _compile ( 'https://deno.land/x/aleph/renderer.ts' , { forceTarget : 'es2020' } )
561
560
await this . _createMainModule ( )
562
561
563
- // ensure react in deno is same with browser one
564
562
const { renderPage, renderHead } = await import ( 'file://' + this . #modules. get ( '//deno.land/x/aleph/renderer.js' ) ! . jsFile )
565
563
this . #renderer = { renderPage, renderHead }
566
564
@@ -773,21 +771,7 @@ export class Project {
773
771
hash : this . #modules. get ( String ( url ) . replace ( reHttp , '//' ) . replace ( reModuleExt , '.js' ) ) ?. hash || ''
774
772
} ) )
775
773
776
- try {
777
- let prevHash = ''
778
- if ( this . #modules. has ( module . id ) ) {
779
- prevHash = this . #modules. get ( module . id ) ! . hash
780
- } else if ( existsFileSync ( metaFile ) ) {
781
- const { hash } = JSON . parse ( await Deno . readTextFile ( metaFile ) )
782
- if ( util . isNEString ( hash ) ) {
783
- prevHash = hash
784
- }
785
- }
786
- if ( prevHash !== '' ) {
787
- await Deno . remove ( path . join ( this . buildDir , `main.${ prevHash . slice ( 0 , hashShort ) } .js` ) )
788
- }
789
- } catch ( e ) { }
790
-
774
+ await cleanupCompilation ( module . jsFile )
791
775
await Promise . all ( [
792
776
writeTextFile ( module . jsFile , module . jsContent ) ,
793
777
writeTextFile ( metaFile , JSON . stringify ( {
@@ -803,7 +787,7 @@ export class Project {
803
787
}
804
788
805
789
// todo: force recompile remote modules which URL don't specify version
806
- private async _compile ( url : string , options ?: { sourceCode ?: string , forceCompile ?: boolean } ) {
790
+ private async _compile ( url : string , options ?: { sourceCode ?: string , forceCompile ?: boolean , forceTarget ?: string } ) {
807
791
const mod = this . _moduleFromURL ( url )
808
792
if ( this . #modules. has ( mod . id ) && ! options ?. forceCompile ) {
809
793
return this . #modules. get ( mod . id ) !
@@ -1006,7 +990,7 @@ export class Project {
1006
990
const useDenos : string [ ] = [ ]
1007
991
const compileOptions = {
1008
992
mode : this . mode ,
1009
- target : this . config . buildTarget ,
993
+ target : options ?. forceTarget || this . config . buildTarget ,
1010
994
reactRefresh : this . isDev && ! mod . isRemote ,
1011
995
rewriteImportPath : ( path : string , async ?: boolean ) => this . _rewriteImportPath ( mod , path , async ) ,
1012
996
signUseDeno : ( id : string ) => {
@@ -1086,13 +1070,8 @@ export class Project {
1086
1070
}
1087
1071
1088
1072
if ( fsync ) {
1089
- if ( mod . jsFile != '' ) {
1090
- try {
1091
- await Deno . remove ( mod . jsFile )
1092
- await Deno . remove ( mod . jsFile + '.map' )
1093
- } catch ( e ) { }
1094
- }
1095
1073
mod . jsFile = path . join ( saveDir , name + ( mod . isRemote ? '' : `.${ mod . hash . slice ( 0 , hashShort ) } ` ) ) + '.js'
1074
+ await cleanupCompilation ( mod . jsFile )
1096
1075
await Promise . all ( [
1097
1076
writeTextFile ( metaFile , JSON . stringify ( {
1098
1077
url,
@@ -1244,7 +1223,7 @@ export class Project {
1244
1223
if ( this . isDev ) {
1245
1224
log . warn ( `page '${ url . pathname } ' not found` )
1246
1225
}
1247
- return this . _render404Page ( url )
1226
+ return await this . _render404Page ( url )
1248
1227
}
1249
1228
try {
1250
1229
const appModule = this . #modules. get ( '/app.js' )
@@ -1425,3 +1404,21 @@ async function writeTextFile(filepath: string, content: string) {
1425
1404
await ensureDir ( dir )
1426
1405
await Deno . writeTextFile ( filepath , content )
1427
1406
}
1407
+
1408
+ async function cleanupCompilation ( jsFile : string ) {
1409
+ const dir = path . dirname ( jsFile )
1410
+ const jsFileName = path . basename ( jsFile )
1411
+ if ( ! reHashJs . test ( jsFile ) || ! existsDirSync ( dir ) ) {
1412
+ return
1413
+ }
1414
+ const jsName = jsFileName . split ( '.' ) . slice ( 0 , - 2 ) . join ( '.' ) + '.js'
1415
+ for await ( const entry of Deno . readDir ( dir ) ) {
1416
+ if ( entry . isFile && ( entry . name . endsWith ( '.js' ) || entry . name . endsWith ( '.js.map' ) ) ) {
1417
+ const _jsName = util . trimSuffix ( entry . name , '.map' ) . split ( '.' ) . slice ( 0 , - 2 ) . join ( '.' ) + '.js'
1418
+ if ( _jsName === jsName && jsFileName !== entry . name ) {
1419
+ log . debug ( 'cleanup' , jsFileName , '->' , entry . name )
1420
+ await Deno . remove ( path . join ( dir , entry . name ) )
1421
+ }
1422
+ }
1423
+ }
1424
+ }
0 commit comments