1
- import type { ImportMap , TransformOptions } from '../compiler/mod.ts'
1
+ import { ImportMap , SourceType , TransformOptions } from '../compiler/mod.ts'
2
2
import { buildChecksum , transform } from '../compiler/mod.ts'
3
3
import { colors , createHash , ensureDir , path , walk } from '../deps.ts'
4
4
import { EventEmitter } from '../framework/core/events.ts'
@@ -280,6 +280,35 @@ export class Application implements ServerApplication {
280
280
}
281
281
}
282
282
283
+ private isScopedModule ( url : string ) {
284
+ for ( const ext of moduleExts ) {
285
+ if ( url . endsWith ( '.' + ext ) ) {
286
+ if ( url . startsWith ( '/pages/' ) || url . startsWith ( '/api/' ) ) {
287
+ return true
288
+ }
289
+ switch ( trimModuleExt ( url ) ) {
290
+ case '/404' :
291
+ case '/app' :
292
+ return true
293
+ }
294
+ }
295
+ }
296
+
297
+ // is page module by plugin
298
+ if ( this . config . plugins . some ( p => p . type === 'loader' && p . test . test ( url ) && p . allowPage ) ) {
299
+ return true
300
+ }
301
+
302
+ // is dep
303
+ for ( const { deps } of this . #modules. values ( ) ) {
304
+ if ( deps . some ( dep => dep . url === url ) ) {
305
+ return true
306
+ }
307
+ }
308
+
309
+ return false
310
+ }
311
+
283
312
get isDev ( ) {
284
313
return this . mode === 'development'
285
314
}
@@ -478,8 +507,8 @@ export class Application implements ServerApplication {
478
507
479
508
if ( bundleMode ) {
480
509
return [
481
- `var bootstrap= __ALEPH.pack[" ${ alephPkgUri } /framework/ ${ framework } /bootstrap.ts"].default ;` ,
482
- `bootstrap(${ JSON . stringify ( config ) } );`
510
+ `__ALEPH.baseURL = ${ JSON . stringify ( baseURL ) } ;` ,
511
+ `__ALEPH.pack[" ${ alephPkgUri } /framework/ ${ framework } / bootstrap.ts"].default (${ JSON . stringify ( config ) } );`
483
512
] . join ( '\n' )
484
513
}
485
514
@@ -683,31 +712,24 @@ export class Application implements ServerApplication {
683
712
url : string ,
684
713
sourceContent : Uint8Array ,
685
714
contentType : string | null
686
- ) : Promise < [ string , 'js' | 'jsx' | 'ts' | 'tsx' ] | null > {
715
+ ) : Promise < [ string , SourceType ] | null > {
687
716
let sourceCode = ( new TextDecoder ) . decode ( sourceContent )
688
- let sourceType = path . extname ( url ) . slice ( 1 )
689
-
690
- if ( sourceType == 'mjs' ) {
691
- sourceType = 'js'
692
- }
717
+ let sourceType : SourceType = SourceType . Unknown
693
718
694
719
if ( contentType !== null ) {
695
720
switch ( contentType . split ( ';' ) [ 0 ] . trim ( ) ) {
696
721
case 'application/javascript' :
697
722
case 'text/javascript' :
698
- sourceType = 'js'
723
+ sourceType = SourceType . JS
699
724
break
700
725
case 'text/typescript' :
701
- sourceType = 'ts'
726
+ sourceType = SourceType . TS
702
727
break
703
728
case 'text/jsx' :
704
- sourceType = 'jsx'
729
+ sourceType = SourceType . JSX
705
730
break
706
731
case 'text/tsx' :
707
- sourceType = 'tsx'
708
- break
709
- default :
710
- sourceType = 'js'
732
+ sourceType = SourceType . TSX
711
733
break
712
734
}
713
735
}
@@ -719,19 +741,42 @@ export class Application implements ServerApplication {
719
741
{ url, content : sourceContent }
720
742
)
721
743
sourceCode = code
722
- sourceType = type
744
+ switch ( type ) {
745
+ case 'js' :
746
+ sourceType = SourceType . JS
747
+ break
748
+ case 'jsx' :
749
+ sourceType = SourceType . JSX
750
+ break
751
+ case 'ts' :
752
+ sourceType = SourceType . TS
753
+ break
754
+ case 'tsx' :
755
+ sourceType = SourceType . TSX
756
+ break
757
+ }
723
758
break
724
759
}
725
760
}
726
761
727
- switch ( sourceType ) {
728
- case 'js' :
729
- case 'jsx' :
730
- case 'ts' :
731
- case 'tsx' :
732
- break
733
- default :
734
- return null
762
+ if ( sourceType === SourceType . Unknown ) {
763
+ switch ( path . extname ( url ) . slice ( 1 ) . toLowerCase ( ) ) {
764
+ case 'mjs' :
765
+ case 'js' :
766
+ sourceType = SourceType . JS
767
+ break
768
+ case 'jsx' :
769
+ sourceType = SourceType . JSX
770
+ break
771
+ case 'ts' :
772
+ sourceType = SourceType . TS
773
+ break
774
+ case 'tsx' :
775
+ sourceType = SourceType . TSX
776
+ break
777
+ default :
778
+ return null
779
+ }
735
780
}
736
781
737
782
return [ sourceCode , sourceType ]
@@ -943,30 +988,27 @@ export class Application implements ServerApplication {
943
988
944
989
/** create bundle chunks for production. */
945
990
private async bundle ( ) {
946
- const sharedScopeMods = new Set < string > ( )
947
991
const sharedEntryMods = new Set < string > ( )
948
- const entryMods = new Map < string , boolean > ( )
992
+ const entryMods = new Map < string [ ] , boolean > ( )
949
993
const refCounter = new Map < string , Set < string > > ( )
950
994
const concatAllEntries = ( ) => [
951
- Array . from ( entryMods . entries ( ) ) . map ( ( [ url , shared ] ) => ( { url, shared } ) ) ,
995
+ Array . from ( entryMods . entries ( ) ) . map ( ( [ urls , shared ] ) => urls . map ( url => ( { url, shared } ) ) ) ,
952
996
Array . from ( sharedEntryMods ) . map ( url => ( { url, shared : true } ) ) ,
953
- ] . flat ( )
997
+ ] . flat ( 2 )
954
998
955
999
// add framwork bootstrap module as shared entry
956
- entryMods . set ( `${ getAlephPkgUri ( ) } /framework/${ this . config . framework } /bootstrap.ts` , true )
1000
+ entryMods . set (
1001
+ [ `${ getAlephPkgUri ( ) } /framework/${ this . config . framework } /bootstrap.ts` ] ,
1002
+ true
1003
+ )
1004
+
1005
+ entryMods . set ( Array . from ( this . #modules. keys ( ) ) . filter ( url => [ '/app' , '/404' ] . includes ( trimModuleExt ( url ) ) ) , true )
957
1006
958
1007
this . #modules. forEach ( mod => {
959
- switch ( trimModuleExt ( mod . url ) ) {
960
- case '/app' :
961
- case '/404' :
962
- // add custom app/404 module as shared entry
963
- entryMods . set ( mod . url , true )
964
- break
965
- }
966
1008
mod . deps . forEach ( ( { url, isDynamic } ) => {
967
1009
if ( isDynamic ) {
968
1010
// add dynamic imported module as entry
969
- entryMods . set ( url , false )
1011
+ entryMods . set ( [ url ] , false )
970
1012
}
971
1013
return url
972
1014
} )
@@ -983,44 +1025,34 @@ export class Application implements ServerApplication {
983
1025
984
1026
// add page module entries
985
1027
this . #pageRouting. lookup ( routes => {
986
- routes . forEach ( ( { module : { url } } ) => entryMods . set ( url , false ) )
1028
+ routes . forEach ( ( { module : { url } } ) => entryMods . set ( [ url ] , false ) )
987
1029
} )
988
1030
989
1031
refCounter . forEach ( ( refers , url ) => {
990
1032
if ( refers . size > 1 ) {
991
- for ( const [ key , shared ] of entryMods . entries ( ) ) {
992
- if ( ( ! shared || ! util . isLikelyHttpURL ( key ) ) && refers . has ( key ) ) {
993
- // add shared module entry
994
- sharedEntryMods . add ( url )
995
- break
1033
+ let shared = 0
1034
+ for ( const mods of entryMods . keys ( ) ) {
1035
+ const some = mods . some ( u => {
1036
+ let scoped = false
1037
+ this . lookupDeps ( u , dep => {
1038
+ if ( ! dep . isDynamic && refers . has ( dep . url ) ) {
1039
+ scoped = true
1040
+ return false
1041
+ }
1042
+ } )
1043
+ return scoped
1044
+ } )
1045
+ if ( some ) {
1046
+ shared ++
996
1047
}
997
1048
}
998
- }
999
- } )
1000
-
1001
- // some modules are shared deeply
1002
- const entries = concatAllEntries ( )
1003
- entries . forEach ( ( { url, shared } ) => {
1004
- if ( shared ) {
1005
- this . lookupDeps ( url , dep => {
1006
- if ( ! dep . isDynamic ) {
1007
- sharedScopeMods . add ( dep . url )
1008
- }
1009
- } )
1010
- }
1011
- } )
1012
- refCounter . forEach ( ( refers , url ) => {
1013
- if ( refers . size > 1 ) {
1014
- const scopedMods = [
1015
- ...Array . from ( sharedScopeMods ) ,
1016
- ...entries . map ( ( { url } ) => url )
1017
- ]
1018
- if ( scopedMods . every ( url => ! refers . has ( url ) ) ) {
1049
+ if ( shared > 1 ) {
1019
1050
sharedEntryMods . add ( url )
1020
1051
}
1021
1052
}
1022
1053
} )
1023
1054
1055
+ log . info ( '- bundle' )
1024
1056
await this . #bundler. bundle ( concatAllEntries ( ) )
1025
1057
}
1026
1058
@@ -1130,37 +1162,8 @@ export class Application implements ServerApplication {
1130
1162
return ssr
1131
1163
}
1132
1164
1133
- private isScopedModule ( url : string ) {
1134
- for ( const ext of moduleExts ) {
1135
- if ( url . endsWith ( '.' + ext ) ) {
1136
- if ( url . startsWith ( '/pages/' ) || url . startsWith ( '/api/' ) ) {
1137
- return true
1138
- }
1139
- switch ( trimModuleExt ( url ) ) {
1140
- case '/404' :
1141
- case '/app' :
1142
- return true
1143
- }
1144
- }
1145
- }
1146
-
1147
- // is page module by plugin
1148
- if ( this . config . plugins . some ( p => p . type === 'loader' && p . test . test ( url ) && p . allowPage ) ) {
1149
- return true
1150
- }
1151
-
1152
- // is dep
1153
- for ( const { deps } of this . #modules. values ( ) ) {
1154
- if ( deps . some ( dep => dep . url === url ) ) {
1155
- return true
1156
- }
1157
- }
1158
-
1159
- return false
1160
- }
1161
-
1162
1165
/** lookup deps recurively. */
1163
- private lookupDeps (
1166
+ lookupDeps (
1164
1167
url : string ,
1165
1168
callback : ( dep : DependencyDescriptor ) => false | void ,
1166
1169
__tracing : Set < string > = new Set ( )
0 commit comments