1
+ import marked from 'https://esm.sh/marked'
1
2
import { minify } from 'https://esm.sh/terser'
3
+ import { loadFront } from 'https://esm.sh/yaml-front-matter'
2
4
import { EventEmitter } from './events.ts'
3
5
import { createHtml } from './html.ts'
4
6
import log from './log.ts'
@@ -130,14 +132,15 @@ export default class Project {
130
132
if ( ! reStyleModuleExt . test ( modId ) ) {
131
133
modId = modId + '.js'
132
134
}
133
- } else {
135
+ } else if ( modId . endsWith ( '.js' ) ) {
136
+ let id = modId . slice ( 0 , modId . length - 3 )
134
137
if ( reHashJs . test ( modId ) ) {
135
- const id = modId . slice ( 0 , modId . length - ( hashShort + 4 ) )
136
- if ( reStyleModuleExt . test ( id ) ) {
137
- modId = id
138
- } else {
139
- modId = id + '.js'
140
- }
138
+ id = modId . slice ( 0 , modId . length - ( 1 + hashShort + 3 ) )
139
+ }
140
+ if ( reMDExt . test ( id ) || reStyleModuleExt . test ( id ) ) {
141
+ modId = id
142
+ } else {
143
+ modId = id + '.js'
141
144
}
142
145
}
143
146
if ( ! this . #modules. has ( modId ) ) {
@@ -434,7 +437,7 @@ export default class Project {
434
437
435
438
for await ( const { path : p } of walk ( pagesDir , { ...walkOptions , exts : [ ...walkOptions . exts , '.jsx' , '.tsx' , '.md' , '.mdx' ] } ) ) {
436
439
const rp = util . trimPrefix ( p , pagesDir )
437
- const pagePath = rp . replace ( reModuleExt , '' ) . replace ( / \s + / g, '-' ) . replace ( / \/ i n d e x $ / i, '' ) || '/'
440
+ const pagePath = rp . replace ( reModuleExt , '' ) . replace ( reMDExt , '' ) . replace ( / \s + / g, '-' ) . replace ( / \/ i n d e x $ / i, '/' )
438
441
const mod = await this . _compile ( '/pages' + rp )
439
442
this . #pageModules. set ( pagePath , {
440
443
moduleID : mod . id ,
@@ -520,7 +523,7 @@ export default class Project {
520
523
if ( this . #pageModules. has ( moduleID ) ) {
521
524
this . _clearPageRenderCache ( moduleID )
522
525
} else {
523
- const pagePath = util . trimPrefix ( moduleID , '/pages' ) . replace ( reModuleExt , '' ) . replace ( / \s + / g, '-' ) . replace ( / \/ i n d e x $ / i, '' ) || '/'
526
+ const pagePath = util . trimPrefix ( moduleID , '/pages' ) . replace ( reModuleExt , '' ) . replace ( reMDExt , '' ) . replace ( / \s + / g, '-' ) . replace ( / \/ i n d e x $ / i, '/' )
524
527
this . #pageModules. set ( pagePath , { moduleID, rendered : new Map ( ) } )
525
528
}
526
529
}
@@ -618,8 +621,7 @@ export default class Project {
618
621
baseUrl,
619
622
defaultLocale,
620
623
locales : { } ,
621
- coreModules : { } ,
622
- pageModules : { }
624
+ routing : { }
623
625
}
624
626
const module = this . _moduleFromURL ( '/main.js' )
625
627
const deps = [
@@ -631,35 +633,29 @@ export default class Project {
631
633
} ) )
632
634
if ( this . #modules. has ( '/data.js' ) ) {
633
635
const { id, url, hash } = this . #modules. get ( '/data.js' ) !
634
- config . coreModules . data = { id, hash }
636
+ const asyncDeps = this . _lookupStyleDeps ( id ) . filter ( ( { async } ) => ! ! async ) . map ( ( { url, hash } ) => ( { url, hash } ) )
637
+ config . staticDataModule = { id, hash, asyncDeps }
635
638
deps . push ( { url, hash } )
636
639
}
637
640
if ( this . #modules. has ( '/app.js' ) ) {
638
- const { url, hash } = this . #modules. get ( '/app.js' ) !
639
- config . coreModules . app = { id : '/app.js' , hash }
641
+ const { id, url, hash } = this . #modules. get ( '/app.js' ) !
642
+ const asyncDeps = this . _lookupStyleDeps ( id ) . filter ( ( { async } ) => ! ! async ) . map ( ( { url, hash } ) => ( { url, hash } ) )
643
+ config . customAppModule = { id, hash, asyncDeps }
640
644
deps . push ( { url, hash } )
641
645
}
642
646
if ( this . #modules. has ( '/404.js' ) ) {
643
- const { url, hash } = this . #modules. get ( '/404.js' ) !
644
- config . coreModules [ '404' ] = { id : '/404.js' , hash }
647
+ const { id, url, hash } = this . #modules. get ( '/404.js' ) !
648
+ const asyncDeps = this . _lookupStyleDeps ( id ) . filter ( ( { async } ) => ! ! async ) . map ( ( { url, hash } ) => ( { url, hash } ) )
649
+ config . custom404Module = { id : '/404.js' , hash, asyncDeps }
645
650
deps . push ( { url, hash } )
646
651
}
647
652
this . #pageModules. forEach ( ( { moduleID } , pagePath ) => {
648
- const { url, hash } = this . #modules. get ( moduleID ) !
649
- config . pageModules [ pagePath ] = { id : moduleID , hash }
653
+ const { id, url, hash } = this . #modules. get ( moduleID ) !
654
+ const asyncDeps = this . _lookupStyleDeps ( id ) . filter ( ( { async } ) => ! ! async ) . map ( ( { url, hash } ) => ( { url, hash } ) )
655
+ config . routing [ pagePath ] = { id, hash, asyncDeps }
650
656
deps . push ( { url, hash } )
651
657
} )
652
658
653
- for ( const key in config . coreModules ) {
654
- const m = config . coreModules [ key ]
655
- m . asyncDeps = this . _lookupStyleDeps ( m . id ) . filter ( ( { async } ) => ! ! async ) . map ( ( { url, hash } ) => ( { url, hash } ) )
656
- }
657
-
658
- for ( const key in config . pageModules ) {
659
- const m = config . pageModules [ key ]
660
- m . asyncDeps = this . _lookupStyleDeps ( m . id ) . filter ( ( { async } ) => ! ! async ) . map ( ( { url, hash } ) => ( { url, hash } ) )
661
- }
662
-
663
659
module . jsContent = [
664
660
this . isDev && 'import "./-/deno.land/x/aleph/hmr.js";' ,
665
661
'import bootstrap from "./-/deno.land/x/aleph/bootstrap.js";' ,
@@ -857,11 +853,18 @@ export default class Project {
857
853
mod . hash = hash
858
854
} else if ( mod . sourceType === 'sass' || mod . sourceType === 'scss' ) {
859
855
// todo: support sass
860
- } else if ( mod . sourceType === 'md' || mod . sourceType === 'mdx' ) {
861
- mod . jsContent = `export default function MD() { return React.createElement('pre', null, ${ JSON . stringify ( sourceContent ) } )}`
862
- mod . jsSourceMap = ''
863
- mod . hash = mod . sourceHash
864
856
} 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
+ }
865
868
const compileOptions = {
866
869
target : this . config . buildTarget ,
867
870
mode : this . mode ,
@@ -870,12 +873,12 @@ export default class Project {
870
873
}
871
874
const { diagnostics, outputText, sourceMapText } = compile ( mod . sourceFilePath , sourceContent , compileOptions )
872
875
if ( diagnostics && diagnostics . length > 0 ) {
873
- throw new Error ( `compile ${ url } : ${ diagnostics . map ( d => d . messageText ) . join ( ' ' ) } ` )
876
+ throw new Error ( `compile ${ url } : ${ diagnostics . map ( d => d . messageText ) . join ( '\n ' ) } ` )
874
877
}
875
- const jsContent = outputText . replace ( / i m p o r t ( [ ^ ' " ] * ) ( " | ' ) t s l i b ( " | ' ) ( \) | ; ) ? / g, 'import$1 ' + JSON . stringify ( relativePath (
878
+ const jsContent = outputText . replace ( / i m p o r t \s * { ( [ ^ } ] + ) } \s * f r o m \s * ( " | ' ) t s l i b ( " | ' ) ; ? / g, 'import {$1} from ' + JSON . stringify ( relativePath (
876
879
path . dirname ( mod . sourceFilePath ) ,
877
880
'/-/deno.land/x/aleph/tsc/tslib.js'
878
- ) ) + '$4 ' )
881
+ ) ) + '; ' )
879
882
if ( this . isDev ) {
880
883
mod . jsContent = jsContent
881
884
mod . jsSourceMap = sourceMapText !
@@ -1114,7 +1117,7 @@ export default class Project {
1114
1117
return a
1115
1118
}
1116
1119
s . add ( moduleID )
1117
- a . push ( ...mod . deps . filter ( ( { url } ) => reStyleModuleExt . test ( url ) ) )
1120
+ a . push ( ...mod . deps . filter ( ( { url } ) => reStyleModuleExt . test ( url ) && a . findIndex ( i => i . url === url ) === - 1 ) )
1118
1121
mod . deps . forEach ( ( { url } ) => {
1119
1122
if ( reModuleExt . test ( url ) && ! reHttp . test ( url ) ) {
1120
1123
this . _lookupStyleDeps ( url . replace ( reModuleExt , '.js' ) , a , s )
@@ -1138,7 +1141,7 @@ export function injectHmr({ id, sourceFilePath, jsContent }: Module): string {
1138
1141
`import { createHotContext, RefreshRuntime, performReactRefresh } from ${ JSON . stringify ( hmrImportPath ) } ;` ,
1139
1142
`import.meta.hot = createHotContext(${ JSON . stringify ( id ) } );`
1140
1143
]
1141
- const reactRefresh = id . endsWith ( '.js' )
1144
+ const reactRefresh = id . endsWith ( '.js' ) || id . endsWith ( '.md' ) || id . endsWith ( '.mdx' )
1142
1145
if ( reactRefresh ) {
1143
1146
text . push ( '' )
1144
1147
text . push (
0 commit comments