1
- import { ImportMap , SourceType , TransformOptions } from '../compiler/mod.ts'
2
- import { buildChecksum , transform } from '../compiler/mod.ts'
1
+ import { buildChecksum , ImportMap , SourceType , transform , TransformOptions } from '../compiler/mod.ts'
3
2
import { colors , createHash , ensureDir , path , walk } from '../deps.ts'
4
3
import { EventEmitter } from '../framework/core/events.ts'
5
4
import type { RouteModule } from '../framework/core/routing.ts'
@@ -18,10 +17,13 @@ import log from '../shared/log.ts'
18
17
import util from '../shared/util.ts'
19
18
import type {
20
19
Config ,
21
- LoaderPlugin ,
20
+
21
+
22
+
23
+ DependencyDescriptor , LoaderPlugin ,
22
24
LoaderTransformResult ,
23
25
Module ,
24
- DependencyDescriptor ,
26
+
25
27
RouterURL ,
26
28
ServerApplication ,
27
29
TransformFn
@@ -33,8 +35,9 @@ import {
33
35
computeHash ,
34
36
formatBytesWithColor ,
35
37
getAlephPkgUri ,
36
- getRelativePath ,
37
- getDenoDir ,
38
+
39
+ getDenoDir , getRelativePath ,
40
+
38
41
isLoaderPlugin ,
39
42
reFullVersion ,
40
43
toLocalUrl ,
@@ -432,26 +435,6 @@ export class Application implements ServerApplication {
432
435
return [ status , html ]
433
436
}
434
437
435
- getSSRHTMLScripts ( ) {
436
- const { baseUrl } = this . config
437
-
438
- if ( this . isDev ) {
439
- return [
440
- { src : util . cleanPath ( `${ baseUrl } /_aleph/main.js` ) , type : 'module' } ,
441
- { src : util . cleanPath ( `${ baseUrl } /_aleph/-/deno.land/x/aleph/nomodule.js` ) , nomodule : true } ,
442
- ]
443
- }
444
-
445
- return [
446
- bundlerRuntimeCode ,
447
- ...[ 'polyfill' , 'deps' , 'shared' , 'main' ]
448
- . filter ( name => this . #bundler. getBundledFile ( name ) !== null )
449
- . map ( name => ( {
450
- src : util . cleanPath ( `${ baseUrl } /_aleph/${ this . #bundler. getBundledFile ( name ) } ` )
451
- } ) )
452
- ]
453
- }
454
-
455
438
createFSWatcher ( ) : EventEmitter {
456
439
const e = new EventEmitter ( )
457
440
this . #fsWatchListeners. push ( e )
@@ -550,6 +533,27 @@ export class Application implements ServerApplication {
550
533
return code
551
534
}
552
535
536
+ /** get ssr html scripts */
537
+ getSSRHTMLScripts ( ) {
538
+ const { baseUrl } = this . config
539
+
540
+ if ( this . isDev ) {
541
+ return [
542
+ { src : util . cleanPath ( `${ baseUrl } /_aleph/main.js` ) , type : 'module' } ,
543
+ { src : util . cleanPath ( `${ baseUrl } /_aleph/-/deno.land/x/aleph/nomodule.js` ) , nomodule : true } ,
544
+ ]
545
+ }
546
+
547
+ return [
548
+ bundlerRuntimeCode ,
549
+ ...[ 'polyfill' , 'deps' , 'shared' , 'main' ]
550
+ . filter ( name => this . #bundler. getBundledFile ( name ) !== null )
551
+ . map ( name => ( {
552
+ src : util . cleanPath ( `${ baseUrl } /_aleph/${ this . #bundler. getBundledFile ( name ) } ` )
553
+ } ) )
554
+ ]
555
+ }
556
+
553
557
/** fetch module content */
554
558
async fetchModule ( url : string ) : Promise < { content : Uint8Array , contentType : string | null } > {
555
559
if ( ! util . isLikelyHttpURL ( url ) ) {
@@ -1015,7 +1019,7 @@ export class Application implements ServerApplication {
1015
1019
private async bundle ( ) {
1016
1020
const sharedEntryMods = new Set < string > ( )
1017
1021
const entryMods = new Map < string [ ] , boolean > ( )
1018
- const refCounter = new Map < string , Set < string > > ( )
1022
+ const refCounter = new Set < string > ( )
1019
1023
const concatAllEntries = ( ) => [
1020
1024
Array . from ( entryMods . entries ( ) ) . map ( ( [ urls , shared ] ) => urls . map ( url => ( { url, shared } ) ) ) ,
1021
1025
Array . from ( sharedEntryMods ) . map ( url => ( { url, shared : true } ) ) ,
@@ -1027,55 +1031,41 @@ export class Application implements ServerApplication {
1027
1031
true
1028
1032
)
1029
1033
1034
+ // add app/404 modules as shared entry
1030
1035
entryMods . set ( Array . from ( this . #modules. keys ( ) ) . filter ( url => [ '/app' , '/404' ] . includes ( trimModuleExt ( url ) ) ) , true )
1031
1036
1037
+ // add page module entries
1038
+ this . #pageRouting. lookup ( routes => {
1039
+ routes . forEach ( ( { module : { url } } ) => entryMods . set ( [ url ] , false ) )
1040
+ } )
1041
+
1042
+ // add dynamic imported module as entry
1032
1043
this . #modules. forEach ( mod => {
1033
1044
mod . deps . forEach ( ( { url, isDynamic } ) => {
1034
1045
if ( isDynamic ) {
1035
- // add dynamic imported module as entry
1036
1046
entryMods . set ( [ url ] , false )
1037
1047
}
1038
1048
return url
1039
1049
} )
1040
- mod . deps . forEach ( ( { url } ) => {
1041
- if ( ! url . startsWith ( '#' ) ) {
1042
- if ( refCounter . has ( url ) ) {
1043
- refCounter . get ( url ) ! . add ( mod . url )
1044
- } else {
1045
- refCounter . set ( url , new Set ( [ mod . url ] ) )
1046
- }
1047
- }
1048
- } )
1049
1050
} )
1050
1051
1051
- // add page module entries
1052
- this . #pageRouting. lookup ( routes => {
1053
- routes . forEach ( ( { module : { url } } ) => entryMods . set ( [ url ] , false ) )
1054
- } )
1055
-
1056
- refCounter . forEach ( ( refers , url ) => {
1057
- if ( refers . size > 1 ) {
1058
- let shared = 0
1059
- for ( const mods of entryMods . keys ( ) ) {
1060
- const some = mods . some ( u => {
1061
- let scoped = false
1062
- this . lookupDeps ( u , dep => {
1063
- if ( ! dep . isDynamic && refers . has ( dep . url ) ) {
1064
- scoped = true
1065
- return false
1066
- }
1067
- } )
1068
- return scoped
1069
- } )
1070
- if ( some ) {
1071
- shared ++
1052
+ for ( const mods of entryMods . keys ( ) ) {
1053
+ const deps = new Set < string > ( )
1054
+ mods . forEach ( url => {
1055
+ this . lookupDeps ( url , dep => {
1056
+ if ( ! dep . isDynamic ) {
1057
+ deps . add ( dep . url )
1072
1058
}
1073
- }
1074
- if ( shared > 1 ) {
1059
+ } )
1060
+ } )
1061
+ deps . forEach ( url => {
1062
+ if ( refCounter . has ( url ) ) {
1075
1063
sharedEntryMods . add ( url )
1064
+ } else {
1065
+ refCounter . add ( url )
1076
1066
}
1077
- }
1078
- } )
1067
+ } )
1068
+ }
1079
1069
1080
1070
log . info ( '- bundle' )
1081
1071
await this . #bundler. bundle ( concatAllEntries ( ) )
0 commit comments