@@ -53,7 +53,7 @@ type LoadListener = {
53
53
54
54
type TransformListener = {
55
55
test : RegExp | 'hmr' | 'main' ,
56
- transform ( input : TransformInput ) : TransformOutput ,
56
+ transform ( input : TransformInput ) : TransformOutput | void | Promise < TransformOutput > | Promise < void > ,
57
57
}
58
58
59
59
type SsrListener = ( input : SSRInput ) => SSROutput
@@ -143,6 +143,7 @@ export class Aleph implements IAleph {
143
143
const pagesDir = join ( srcDir , 'pages' )
144
144
const buildManifestFile = join ( this . #buildDir, 'build.manifest.json' )
145
145
const importMapString = JSON . stringify ( this . #importMap)
146
+ const pluginNames = this . #config. plugins . map ( ( { name } ) => name ) . join ( ',' )
146
147
147
148
let shouldRebuild = ! await existsFile ( buildManifestFile )
148
149
let saveManifestFile = shouldRebuild
@@ -153,7 +154,8 @@ export class Aleph implements IAleph {
153
154
typeof v !== 'object' ||
154
155
v === null ||
155
156
v . compiler !== wasmChecksum ||
156
- ( v . importMap !== importMapString && confirm ( 'The import-maps has been changed, rebuild modules?' ) )
157
+ ( v . importMap !== importMapString && confirm ( 'The import-maps has been changed, clean build cache?' ) ) ||
158
+ ( v . plugins !== pluginNames && confirm ( 'The plugin list has been updated, clean build cache?' ) )
157
159
)
158
160
if ( ! shouldRebuild && v . importMap !== importMapString ) {
159
161
saveManifestFile = true
@@ -176,6 +178,7 @@ export class Aleph implements IAleph {
176
178
deno : Deno . version . deno ,
177
179
compiler : wasmChecksum ,
178
180
importMap : importMapString ,
181
+ plugins : pluginNames ,
179
182
} , undefined , 2 ) )
180
183
}
181
184
@@ -299,6 +302,11 @@ export class Aleph implements IAleph {
299
302
if ( this . #modules. has ( specifier ) ) {
300
303
try {
301
304
const prevModule = this . #modules. get ( specifier ) !
305
+ if ( prevModule . jsFile === '/aleph.config.js' ) {
306
+ log . info ( `${ prevModule . specifier . slice ( 1 ) } has be changed, please restart the server.` )
307
+ return
308
+ }
309
+
302
310
const module = await this . compile ( specifier , {
303
311
forceRefresh : true ,
304
312
ignoreDeps : true ,
@@ -491,7 +499,7 @@ export class Aleph implements IAleph {
491
499
this . #loadListeners. push ( { test, load : callback } )
492
500
}
493
501
494
- onTransform ( test : RegExp | 'hmr' | 'main' , callback : ( input : TransformInput ) => TransformOutput ) : void {
502
+ onTransform ( test : RegExp | 'hmr' | 'main' , callback : ( input : TransformInput ) => TransformOutput | Promise < TransformOutput > ) : void {
495
503
this . #transformListeners. push ( { test, transform : callback } )
496
504
}
497
505
@@ -501,10 +509,19 @@ export class Aleph implements IAleph {
501
509
502
510
/** add a module by given path and optional source code. */
503
511
async addModule ( specifier : string , sourceCode : string ) : Promise < Module > {
512
+ let sourceType = getSourceType ( specifier )
513
+ if ( sourceType === SourceType . Unknown ) {
514
+ throw new Error ( "addModule: unknown souce type" )
515
+ }
516
+ if ( sourceType === SourceType . CSS ) {
517
+ const ret = await cssLoader ( { specifier, data : ( new TextEncoder ) . encode ( sourceCode ) } , this )
518
+ sourceCode = ret . code
519
+ sourceType = SourceType . JS
520
+ }
504
521
const module = await this . compile ( specifier , {
505
522
source : {
506
523
code : sourceCode ,
507
- type : getSourceType ( specifier ) ,
524
+ type : sourceType ,
508
525
isStyle : false ,
509
526
}
510
527
} )
@@ -642,7 +659,7 @@ export class Aleph implements IAleph {
642
659
}
643
660
644
661
/** create main bootstrap script in javascript. */
645
- createMainJS ( bundleMode = false ) : string {
662
+ async createMainJS ( bundleMode = false ) : Promise < string > {
646
663
const alephPkgUri = getAlephPkgUri ( )
647
664
const alephPkgPath = alephPkgUri . replace ( 'https://' , '' ) . replace ( 'http://localhost:' , 'http_localhost_' )
648
665
const { framework, basePath : basePath , i18n : { defaultLocale } } = this . #config
@@ -671,9 +688,9 @@ export class Aleph implements IAleph {
671
688
`bootstrap(${ JSON . stringify ( config , undefined , this . isDev ? 2 : undefined ) } );`
672
689
] . filter ( Boolean ) . join ( '\n' )
673
690
}
674
- this . #transformListeners . forEach ( ( { test, transform } ) => {
691
+ for ( const { test, transform } of this . #transformListeners ) {
675
692
if ( test === 'main' ) {
676
- const ret = transform ( {
693
+ let ret = await transform ( {
677
694
module : {
678
695
specifier : '/main.js' ,
679
696
deps : [ ] ,
@@ -683,10 +700,10 @@ export class Aleph implements IAleph {
683
700
code,
684
701
} )
685
702
if ( util . isFilledString ( ret ?. code ) ) {
686
- code = ret . code
703
+ code = ret ! . code
687
704
}
688
705
}
689
- } )
706
+ }
690
707
return code
691
708
}
692
709
@@ -919,16 +936,16 @@ export class Aleph implements IAleph {
919
936
// todo: merge source map
920
937
}
921
938
}
922
- this . #transformListeners . forEach ( ( { test, transform } ) => {
939
+ for ( const { test, transform } of this . #transformListeners ) {
923
940
if ( test === 'hmr' ) {
924
941
const { jsBuffer, ready, ...rest } = module
925
- const ret = transform ( { module : structuredClone ( rest ) , code } )
942
+ const ret = await transform ( { module : rest , code } )
926
943
if ( util . isFilledString ( ret ?. code ) ) {
927
- code = ret . code
944
+ code = ret ! . code
928
945
}
929
946
// todo: merge source map
930
947
}
931
- } )
948
+ }
932
949
return new TextEncoder ( ) . encode ( [
933
950
`import.meta.hot = $createHotContext(${ JSON . stringify ( specifier ) } );` ,
934
951
'' ,
@@ -1216,18 +1233,18 @@ export class Aleph implements IAleph {
1216
1233
}
1217
1234
}
1218
1235
1219
- this . #transformListeners . forEach ( ( { test, transform } ) => {
1236
+ for ( const { test, transform } of this . #transformListeners ) {
1220
1237
if ( test instanceof RegExp && test . test ( specifier ) ) {
1221
1238
const { jsBuffer, ready, ...rest } = module
1222
- const { code , map } = transform ( { module : structuredClone ( rest ) , code : jsCode , map : sourceMap } ) || { }
1223
- if ( util . isFilledString ( code ) ) {
1224
- jsCode = code
1239
+ const ret = await transform ( { module : rest , code : jsCode , map : sourceMap } )
1240
+ if ( util . isFilledString ( ret ?. code ) ) {
1241
+ jsCode = ret ! . code
1225
1242
}
1226
- if ( util . isFilledString ( map ) ) {
1227
- sourceMap = map
1243
+ if ( util . isFilledString ( ret ?. map ) ) {
1244
+ sourceMap = ret ! . map
1228
1245
}
1229
1246
}
1230
- } )
1247
+ }
1231
1248
1232
1249
// add source mapping url
1233
1250
if ( sourceMap ) {
0 commit comments