@@ -31,8 +31,8 @@ export class Appliaction {
31
31
#renderCache: Map < string , Map < string , RenderResult > > = new Map ( )
32
32
#postcssPlugins: Record < string , AcceptedPlugin > = { }
33
33
#cleanCSS = new CleanCSS ( { compatibility : '*' /* Internet Explorer 10+ */ } )
34
- #swcReady : Promise < void > | null = null
35
- #postcssReady: Promise < void [ ] > | null = null
34
+ #compilerReady : Promise < void > | boolean = false
35
+ #postcssReady: Promise < void [ ] > | boolean = false
36
36
#reloading = false
37
37
38
38
constructor ( workingDir = '.' , mode : 'development' | 'production' = 'production' , reload = false ) {
@@ -52,6 +52,7 @@ export class Appliaction {
52
52
plugins : [ ] ,
53
53
postcss : {
54
54
plugins : [
55
+ 'postcss-nested' ,
55
56
'autoprefixer'
56
57
]
57
58
}
@@ -145,9 +146,21 @@ export class Appliaction {
145
146
return null
146
147
}
147
148
148
- /** add a new module by given url and source code. */
149
- async addModule ( source : { url : string , code : string } ) : Promise < Module > {
150
- return await this . compile ( source . url , { sourceCode : source . code } )
149
+ /** add a new page module by given path and source code. */
150
+ async addPageModule ( pathname : string , code : string ) : Promise < void > {
151
+ const url = path . join ( '/pages/' , util . cleanPath ( pathname ) + '.tsx' )
152
+ const mod = await this . compile ( url , { sourceCode : code } )
153
+ this . #pageRouting. update ( this . getRouteModule ( mod ) )
154
+ }
155
+
156
+ /** add a new page module by given path and source code. */
157
+ async removePageModule ( pathname : string ) : Promise < void > {
158
+ const url = path . join ( '/pages/' , util . cleanPath ( pathname ) + '.tsx' )
159
+ if ( this . #modules. has ( url ) ) {
160
+ await cleanupCompilation ( this . #modules. get ( url ) ! . jsFile )
161
+ this . #modules. delete ( url )
162
+ this . #pageRouting. removeRoute ( url )
163
+ }
151
164
}
152
165
153
166
createFSWatcher ( ) : EventEmitter {
@@ -368,7 +381,7 @@ export class Appliaction {
368
381
for ( const name of Array . from ( [ 'ts' , 'js' , 'json' ] ) . map ( ext => 'aleph.config.' + ext ) ) {
369
382
const p = path . join ( this . workingDir , name )
370
383
if ( existsFileSync ( p ) ) {
371
- log . info ( 'config loaded from' , name )
384
+ log . info ( 'Aleph server config loaded from' , name )
372
385
if ( name . endsWith ( '.json' ) ) {
373
386
const conf = JSON . parse ( await Deno . readTextFile ( p ) )
374
387
if ( util . isPlainObject ( conf ) ) {
@@ -442,10 +455,9 @@ export class Appliaction {
442
455
if ( util . isPlainObject ( postcss ) && util . isArray ( postcss . plugins ) ) {
443
456
Object . assign ( this . config , { postcss } )
444
457
} else {
445
- for ( const name of Array . from ( [ 'ts' , 'js' , 'mjs' , ' json'] ) . map ( ext => `postcss.config.${ ext } ` ) ) {
458
+ for ( const name of Array . from ( [ 'ts' , 'js' , 'json' ] ) . map ( ext => `postcss.config.${ ext } ` ) ) {
446
459
const p = path . join ( this . workingDir , name )
447
460
if ( existsFileSync ( p ) ) {
448
- log . info ( ' ✓' , name )
449
461
if ( name . endsWith ( '.json' ) ) {
450
462
const postcss = JSON . parse ( await Deno . readTextFile ( p ) )
451
463
if ( util . isPlainObject ( postcss ) && util . isArray ( postcss . plugins ) ) {
@@ -582,7 +594,7 @@ export class Appliaction {
582
594
const { render } = await import ( 'file://' + this . #modules. get ( rendererUrl ) ! . jsFile )
583
595
this . #renderer = { render }
584
596
585
- // load plugins
597
+ // apply plugins
586
598
for ( const plugin of this . config . plugins ) {
587
599
if ( plugin . type === 'server' ) {
588
600
await plugin . onInit ( this )
@@ -791,7 +803,9 @@ export class Appliaction {
791
803
792
804
/** preprocess css with postcss plugins */
793
805
private async preprocessCSS ( sourceCode : string ) {
794
- if ( this . #postcssReady === null ) {
806
+ let t : number | null = null
807
+ if ( this . #postcssReady === false ) {
808
+ t = performance . now ( )
795
809
this . #postcssReady = Promise . all ( this . config . postcss . plugins . map ( async p => {
796
810
let name : string | null = null
797
811
if ( util . isNEString ( p ) ) {
@@ -805,7 +819,13 @@ export class Appliaction {
805
819
}
806
820
} ) )
807
821
}
808
- await this . #postcssReady
822
+ if ( this . #postcssReady instanceof Promise ) {
823
+ await this . #postcssReady
824
+ this . #postcssReady = true
825
+ }
826
+ if ( t !== null ) {
827
+ log . debug ( `load postcss plugins(${ this . config . postcss . plugins . length } ) in ${ Math . round ( performance . now ( ) - t ) } ms` )
828
+ }
809
829
const pcss = ( await postcss ( this . config . postcss . plugins . map ( p => {
810
830
if ( typeof p === 'string' ) {
811
831
return this . #postcssPlugins[ p ]
@@ -836,13 +856,16 @@ export class Appliaction {
836
856
/** transpile code without types checking. */
837
857
private async transpile ( sourceCode : string , options : TransformOptions ) {
838
858
let t : number | null = null
839
- if ( this . #swcReady === null ) {
859
+ if ( this . #compilerReady === false ) {
840
860
t = performance . now ( )
841
- this . #swcReady = initWasm ( this . #denoCacheDir)
861
+ this . #compilerReady = initWasm ( this . #denoCacheDir)
862
+ }
863
+ if ( this . #compilerReady instanceof Promise ) {
864
+ await this . #compilerReady
865
+ this . #compilerReady = true
842
866
}
843
- await this . #swcReady
844
- if ( t ) {
845
- log . debug ( 'init compiler wasm in ' + Math . round ( performance . now ( ) - t ) + 'ms' )
867
+ if ( t !== null ) {
868
+ log . debug ( `init compiler wasm in ${ Math . round ( performance . now ( ) - t ) } ms` )
846
869
}
847
870
848
871
return transpileSync ( sourceCode , options )
0 commit comments