@@ -16,10 +16,11 @@ interface Module {
16
16
id : string
17
17
url : string
18
18
isRemote : boolean
19
- deps : { url : string , hash : string , async ?: boolean } [ ]
19
+ asPage ? : { meta : Record < string , any > }
20
20
sourceFilePath : string
21
21
sourceType : string
22
22
sourceHash : string
23
+ deps : { url : string , hash : string , async ?: boolean } [ ]
23
24
jsFile : string
24
25
jsContent : string
25
26
jsSourceMap : string
@@ -33,13 +34,10 @@ interface RenderResult {
33
34
}
34
35
35
36
export interface AlephEnv {
36
- version : string
37
- build : {
38
- mode : 'development' | 'production'
39
- buildID : string
40
- appRoot : string
41
- config : Config
42
- }
37
+ [ key : string ] : string
38
+ __version : string
39
+ __appRoot : string
40
+ __buildID : string
43
41
}
44
42
45
43
export default class Project {
@@ -68,7 +66,8 @@ export default class Project {
68
66
sourceMap : false ,
69
67
importMap : {
70
68
imports : { }
71
- }
69
+ } ,
70
+ env : { }
72
71
}
73
72
this . ready = ( async ( ) => {
74
73
const t = performance . now ( )
@@ -222,7 +221,7 @@ export default class Project {
222
221
return html
223
222
}
224
223
225
- async getData ( ) {
224
+ async getStaticData ( ) {
226
225
const mod = this . #modules. get ( '/data.js' )
227
226
if ( mod ) {
228
227
try {
@@ -233,11 +232,10 @@ export default class Project {
233
232
}
234
233
if ( util . isPlainObject ( data ) ) {
235
234
return data
236
- } else {
237
- log . warn ( `module '${ mod . url } ' should return a plain object as default` )
238
235
}
236
+ log . warn ( `module '${ mod . url } ' should return a plain object as default` )
239
237
} catch ( error ) {
240
- log . error ( error )
238
+ log . error ( 'getStaticData:' , error )
241
239
}
242
240
}
243
241
return { }
@@ -295,7 +293,7 @@ export default class Project {
295
293
// write static data
296
294
if ( this . #modules. has ( '/data.js' ) ) {
297
295
const { hash } = this . #modules. get ( '/data.js' ) !
298
- const data = this . getData ( )
296
+ const data = this . getStaticData ( )
299
297
await writeTextFile ( path . join ( distDir , `data.${ hash . slice ( 0 , hashShort ) } .js` ) , `export default ${ JSON . stringify ( data ) } ` )
300
298
}
301
299
@@ -354,10 +352,11 @@ export default class Project {
354
352
srcDir,
355
353
ouputDir,
356
354
baseUrl,
357
- ssr,
358
355
buildTarget,
359
356
sourceMap,
360
- defaultLocale
357
+ defaultLocale,
358
+ ssr,
359
+ env
361
360
} = config
362
361
if ( util . isNEString ( srcDir ) ) {
363
362
Object . assign ( this . config , { srcDir : util . cleanPath ( srcDir ) } )
@@ -368,6 +367,12 @@ export default class Project {
368
367
if ( util . isNEString ( baseUrl ) ) {
369
368
Object . assign ( this . config , { baseUrl : util . cleanPath ( encodeURI ( baseUrl ) ) } )
370
369
}
370
+ if ( / ^ e s ( 2 0 \d { 2 } | n e x t ) $ / i. test ( buildTarget ) ) {
371
+ Object . assign ( this . config , { buildTarget : buildTarget . toLowerCase ( ) } )
372
+ }
373
+ if ( typeof sourceMap === 'boolean' ) {
374
+ Object . assign ( this . config , { sourceMap } )
375
+ }
371
376
if ( util . isNEString ( defaultLocale ) ) {
372
377
Object . assign ( this . config , { defaultLocale } )
373
378
}
@@ -379,11 +384,8 @@ export default class Project {
379
384
const exclude = util . isArray ( ssr . exclude ) ? ssr . exclude : [ ]
380
385
Object . assign ( this . config , { ssr : { fallback, include, exclude } } )
381
386
}
382
- if ( / ^ e s ( 2 0 \d { 2 } | n e x t ) $ / i. test ( buildTarget ) ) {
383
- Object . assign ( this . config , { buildTarget : buildTarget . toLowerCase ( ) } )
384
- }
385
- if ( typeof sourceMap === 'boolean' ) {
386
- Object . assign ( this . config , { sourceMap } )
387
+ if ( util . isPlainObject ( env ) ) {
388
+ Object . assign ( this . config , { env } )
387
389
}
388
390
389
391
// Gen build ID after config loaded
@@ -401,13 +403,10 @@ export default class Project {
401
403
402
404
Object . assign ( globalThis , {
403
405
ALEPH_ENV : {
404
- version,
405
- build : {
406
- appRoot : this . appRoot ,
407
- buildID : this . buildID ,
408
- config : this . config ,
409
- mode : this . mode ,
410
- }
406
+ ...this . config . env ,
407
+ __version : version ,
408
+ __appRoot : this . appRoot ,
409
+ __buildID : this . buildID ,
411
410
} as AlephEnv ,
412
411
document : new Document ( ) ,
413
412
innerWidth : 1920 ,
@@ -620,7 +619,7 @@ export default class Project {
620
619
const config : Record < string , any > = {
621
620
baseUrl,
622
621
defaultLocale,
623
- locales : { } ,
622
+ locales : [ ] ,
624
623
routing : { }
625
624
}
626
625
const module = this . _moduleFromURL ( '/main.js' )
@@ -853,18 +852,20 @@ export default class Project {
853
852
mod . hash = hash
854
853
} else if ( mod . sourceType === 'sass' || mod . sourceType === 'scss' ) {
855
854
// todo: support sass
855
+ } else if ( mod . sourceType === 'md' || mod . sourceType === 'mdx' ) {
856
+ const { __content, ...props } = loadFront ( sourceContent )
857
+ const html = marked . parse ( __content )
858
+ mod . jsContent = [
859
+ `import React from ${ JSON . stringify ( relativePath ( path . dirname ( mod . sourceFilePath ) , '/-/esm.sh/react.js' ) ) } ;` ,
860
+ `export default function Markdown() {` ,
861
+ ` return React.createElement("div", ${ JSON . stringify ( { className : 'markdown' , dangerouslySetInnerHTML : { __html : html } } ) } );` ,
862
+ `}` ,
863
+ `Markdown.meta = ${ JSON . stringify ( props , undefined , this . isDev ? 4 : undefined ) } ;` ,
864
+ this . isDev && `$RefreshReg$(Markdown, "Markdown");` ,
865
+ ] . filter ( Boolean ) . join ( this . isDev ? '\n' : '' )
866
+ mod . jsSourceMap = ''
867
+ mod . hash = ( new Sha1 ) . update ( mod . jsContent ) . hex ( )
856
868
} else {
857
- if ( mod . sourceType === 'md' || mod . sourceType === 'mdx' ) {
858
- const { __content, ...props } = loadFront ( sourceContent )
859
- const html = marked . parse ( __content )
860
- sourceContent = [
861
- `import React from 'https://esm.sh/react';` ,
862
- `export const pageProps = ${ JSON . stringify ( props , undefined , 4 ) } ;` ,
863
- `export default function Marked() {` ,
864
- ` return React.createElement('div', ${ JSON . stringify ( { className : 'marked' , dangerouslySetInnerHTML : { __html : html } } , undefined , 4 ) } );` ,
865
- `}`
866
- ] . join ( '\n' )
867
- }
868
869
const compileOptions = {
869
870
target : this . config . buildTarget ,
870
871
mode : this . mode ,
@@ -1088,8 +1089,8 @@ export default class Project {
1088
1089
const { renderPage, renderHead } = await import ( "file://" + this . #modules. get ( '//deno.land/x/aleph/renderer.js' ) ! . jsFile )
1089
1090
const { default : App } = appModule ? await import ( "file://" + appModule . jsFile ) : { } as any
1090
1091
const { default : Page } = await import ( "file://" + pageModule . jsFile )
1091
- const data = await this . getData ( )
1092
- const html = renderPage ( data , url , appModule ? App : undefined , Page )
1092
+ const data = await this . getStaticData ( )
1093
+ const html = renderPage ( url , data , appModule ? App : undefined , Page )
1093
1094
const head = await renderHead ( [
1094
1095
appModule ? this . _lookupStyleDeps ( appModule . id ) : [ ] ,
1095
1096
this . _lookupStyleDeps ( pageModule . id ) ,
0 commit comments