@@ -5,22 +5,16 @@ import log from './log.ts'
5
5
import { createRouter } from './router.ts'
6
6
import { colors , ensureDir , path , Sha1 , walk } from './std.ts'
7
7
import { compile } from './tsc/compile.ts'
8
- import type { AlephEnv , APIHandle , Config , Location , RouterURL } from './types.ts'
8
+ import type { APIHandle , Config , Location , RouterURL } from './types.ts'
9
9
import util , { existsDirSync , existsFileSync , hashShort , reHashJs , reHttp , reModuleExt , reStyleModuleExt } from './util.ts'
10
10
import { cleanCSS , Document , less } from './vendor/mod.ts'
11
11
import { version } from './version.ts'
12
12
13
- interface Dep {
14
- url : string
15
- hash : string
16
- async ?: boolean
17
- }
18
-
19
13
interface Module {
20
14
id : string
21
15
url : string
22
16
isRemote : boolean
23
- deps : Dep [ ]
17
+ deps : { url : string , hash : string , async ?: boolean } [ ]
24
18
sourceFilePath : string
25
19
sourceType : string
26
20
sourceHash : string
@@ -36,6 +30,13 @@ interface RenderResult {
36
30
body : string
37
31
}
38
32
33
+ export interface AlephEnv {
34
+ appRoot : string
35
+ buildID : string
36
+ config : Config
37
+ mode : 'development' | 'production'
38
+ }
39
+
39
40
export default class Project {
40
41
readonly mode : 'development' | 'production'
41
42
readonly appRoot : string
@@ -99,7 +100,6 @@ export default class Project {
99
100
moduleID === '/404.js' ||
100
101
moduleID === '/app.js' ||
101
102
moduleID === '/data.js' ||
102
- ( moduleID === '/data/index.js' && ! this . #modules. has ( '/data.js' ) ) ||
103
103
moduleID . startsWith ( '/pages/' ) ||
104
104
moduleID . startsWith ( '/components/' ) ||
105
105
reStyleModuleExt . test ( moduleID )
@@ -137,9 +137,6 @@ export default class Project {
137
137
}
138
138
}
139
139
}
140
- if ( ! this . #modules. has ( modId ) && modId == '/data.js' ) {
141
- modId = '/data/index.js'
142
- }
143
140
if ( ! this . #modules. has ( modId ) ) {
144
141
log . warn ( `can't get the module by path '${ pathname } (${ modId } )'` )
145
142
}
@@ -220,7 +217,7 @@ export default class Project {
220
217
}
221
218
222
219
async getData ( ) {
223
- const mod = this . #modules. get ( '/data.js' ) || this . #modules . get ( '/data/index.js' )
220
+ const mod = this . #modules. get ( '/data.js' )
224
221
if ( mod ) {
225
222
try {
226
223
const { default : Data } = await import ( "file://" + mod . jsFile )
@@ -290,8 +287,8 @@ export default class Project {
290
287
} ) )
291
288
292
289
// write static data
293
- if ( this . #modules. has ( '/data.js' ) || this . #modules . has ( '/data/index.js' ) ) {
294
- const { hash } = this . #modules. get ( '/data.js' ) || this . #modules . get ( '/data/index.js' ) !
290
+ if ( this . #modules. has ( '/data.js' ) ) {
291
+ const { hash } = this . #modules. get ( '/data.js' ) !
295
292
const data = this . getData ( )
296
293
await writeTextFile ( path . join ( distDir , `data.${ hash . slice ( 0 , hashShort ) } .js` ) , `export default ${ JSON . stringify ( data ) } ` )
297
294
}
@@ -388,8 +385,7 @@ export default class Project {
388
385
}
389
386
390
387
private async _init ( ) {
391
- const walkOptions = { includeDirs : false , exts : [ '.js' , '.ts' , '.mjs' ] , skip : [ / ^ \. / , / \. d \. t s $ / i, / \. ( t e s t | s p e c | e 2 e ) \. m ? ( j | t ) s $ / i] }
392
- const dataDir = path . join ( this . srcDir , 'data' )
388
+ const walkOptions = { includeDirs : false , exts : [ '.js' , '.ts' , '.mjs' ] , skip : [ / ^ \. / , / \. d \. t s $ / i, / \. ( t e s t | s p e c | e 2 e ) \. m ? ( j | t ) s x ? $ / i] }
393
389
const apiDir = path . join ( this . srcDir , 'api' )
394
390
const pagesDir = path . join ( this . srcDir , 'pages' )
395
391
@@ -412,32 +408,21 @@ export default class Project {
412
408
$RefreshSig$ : ( ) => ( type : any ) => type ,
413
409
} )
414
410
415
- for await ( const { path : p , isDirectory , isFile } of walk ( this . srcDir , { maxDepth : 1 } ) ) {
411
+ for await ( const { path : p , } of walk ( this . srcDir , { ... walkOptions , maxDepth : 1 , exts : [ ... walkOptions . exts , '.jsx' , '.tsx' ] } ) ) {
416
412
const name = path . basename ( p )
417
- if ( isDirectory && p !== this . srcDir ) {
418
- switch ( name ) {
419
- case 'api' :
420
- for await ( const { path : p } of walk ( apiDir , walkOptions ) ) {
421
- await this . _compile ( '/api' + util . trimPrefix ( p , apiDir ) )
422
- }
423
- break
424
- case 'data' :
425
- for await ( const { path : p } of walk ( dataDir , { ...walkOptions , maxDepth : 1 } ) ) {
426
- const name = path . basename ( p )
427
- if ( name . replace ( reModuleExt , '' ) === 'index' ) {
428
- await this . _compile ( '/data/' + name )
429
- }
430
- }
431
- break
432
- }
433
- } else if ( isFile && reModuleExt . test ( name ) ) {
434
- switch ( name . replace ( reModuleExt , '' ) ) {
435
- case 'app' :
436
- case 'data' :
437
- case '404' :
438
- await this . _compile ( '/' + name )
439
- break
440
- }
413
+ console . log ( name )
414
+ switch ( name . replace ( reModuleExt , '' ) ) {
415
+ case 'app' :
416
+ case 'data' :
417
+ case '404' :
418
+ await this . _compile ( '/' + name )
419
+ break
420
+ }
421
+ }
422
+
423
+ if ( existsDirSync ( apiDir ) ) {
424
+ for await ( const { path : p } of walk ( apiDir , walkOptions ) ) {
425
+ await this . _compile ( '/api' + util . trimPrefix ( p , apiDir ) )
441
426
}
442
427
}
443
428
@@ -496,8 +481,7 @@ export default class Project {
496
481
switch ( moduleID ) {
497
482
case '/404.js' :
498
483
case '/app.js' :
499
- case '/data.js' :
500
- case '/data/index.js' : {
484
+ case '/data.js' : {
501
485
return true
502
486
}
503
487
default : {
@@ -520,7 +504,7 @@ export default class Project {
520
504
util . debounceX ( moduleID , ( ) => {
521
505
const removed = ! existsFileSync ( p )
522
506
const cleanup = ( ) => {
523
- if ( moduleID === '/app.js' || moduleID === '/data.js' || moduleID === '/data/index.js' ) {
507
+ if ( moduleID === '/app.js' || moduleID === '/data.js' ) {
524
508
this . _clearPageRenderCache ( )
525
509
} else if ( moduleID . startsWith ( '/pages/' ) ) {
526
510
if ( removed ) {
@@ -638,8 +622,8 @@ export default class Project {
638
622
url : String ( url ) ,
639
623
hash : this . #modules. get ( String ( url ) . replace ( reHttp , '//' ) . replace ( reModuleExt , '.js' ) ) ?. hash || ''
640
624
} ) )
641
- if ( this . #modules. has ( '/data.js' ) || this . #modules . has ( '/data/index.js' ) ) {
642
- const { id, url, hash } = this . #modules. get ( '/data.js' ) || this . #modules . get ( '/data/index.js' ) !
625
+ if ( this . #modules. has ( '/data.js' ) ) {
626
+ const { id, url, hash } = this . #modules. get ( '/data.js' ) !
643
627
config . coreModules . data = { id, hash }
644
628
deps . push ( { url, hash } )
645
629
}
@@ -864,6 +848,8 @@ export default class Project {
864
848
] . join ( this . isDev ? '\n' : '' )
865
849
mod . jsSourceMap = ''
866
850
mod . hash = hash
851
+ } else if ( mod . sourceType === 'sass' || mod . sourceType === 'scss' ) {
852
+ // todo: support sass
867
853
} else if ( mod . sourceType === 'md' || mod . sourceType === 'mdx' ) {
868
854
mod . jsContent = `export default function MD() { return React.createElement('pre', null, ${ JSON . stringify ( sourceContent ) } )}`
869
855
mod . jsSourceMap = ''
@@ -1112,7 +1098,7 @@ export default class Project {
1112
1098
return ret
1113
1099
}
1114
1100
1115
- private _lookupStyleDeps ( moduleID : string , a : Dep [ ] = [ ] , s : Set < string > = new Set ( ) ) {
1101
+ private _lookupStyleDeps ( moduleID : string , a : { url : string , hash : string , async ?: boolean } [ ] = [ ] , s : Set < string > = new Set ( ) ) {
1116
1102
const mod = this . getModule ( moduleID )
1117
1103
if ( ! mod ) {
1118
1104
return a
0 commit comments