@@ -7,25 +7,23 @@ import { hashShort, reFullVersion, reHashJs, reHashResolve, reHttp, reLocaleID,
7
7
import { ensureTextFile , existsDirSync , existsFileSync } from '../shared/fs.ts'
8
8
import log from '../shared/log.ts'
9
9
import util from '../shared/util.ts'
10
- import type { APIHandler , Config , RouterURL , ServerRequest } from '../types.ts'
10
+ import type { Config , RouterURL , ServerRequest } from '../types.ts'
11
11
import { VERSION } from '../version.ts'
12
12
import { Request } from './api.ts'
13
- import type { DependencyDescriptor , ImportMap , Module , RenderResult } from './types.ts'
13
+ import type { AppliactionOptions , DependencyDescriptor , ImportMap , Module , RenderResult } from './types.ts'
14
14
import { AlephRuntimeCode , cleanupCompilation , createHtml , fixImportMap , fixImportUrl , formatBytesWithColor , getAlephPkgUrl , getRelativePath , newModule , respondError } from './util.ts'
15
15
16
+ const defaultOptions : Required < AppliactionOptions > = {
17
+ workingDir : '.' ,
18
+ mode : 'production' ,
19
+ reload : false
20
+ }
21
+
16
22
/**
17
- * A Project to manage the Aleph.js appliaction.
18
- * core functions include:
19
- * - manage deps
20
- * - compile & bundle
21
- * - apply plugins
22
- * - map page/API routes
23
- * - watch file changes
24
- * - call APIs
25
- * - SSR/SSG
23
+ * The Alep Appliaction class.
26
24
*/
27
- export class Project {
28
- readonly appRoot : string
25
+ export class Appliaction {
26
+ readonly workingDir : string
29
27
readonly mode : 'development' | 'production'
30
28
readonly config : Readonly < Required < Config > >
31
29
readonly importMap : Readonly < { imports : ImportMap , scopes : Record < string , ImportMap > } >
@@ -44,12 +42,13 @@ export class Project {
44
42
#postcssReady: Promise < void [ ] > | null = null
45
43
#reloading = false
46
44
47
- constructor ( appDir : string , mode : 'development' | 'production' , reload = false ) {
48
- this . appRoot = path . resolve ( appDir )
45
+ constructor ( options : AppliactionOptions = defaultOptions ) {
46
+ const { workingDir, mode, reload } = Object . assign ( { } , defaultOptions , options )
47
+ this . workingDir = path . resolve ( workingDir )
49
48
this . mode = mode
50
49
this . config = {
51
50
framework : 'react' ,
52
- srcDir : existsDirSync ( path . join ( this . appRoot , '/src/pages' ) ) ? '/src' : '/' ,
51
+ srcDir : existsDirSync ( path . join ( this . workingDir , '/src/pages' ) ) ? '/src' : '/' ,
53
52
outputDir : '/dist' ,
54
53
baseUrl : '/' ,
55
54
defaultLocale : 'en' ,
@@ -74,15 +73,15 @@ export class Project {
74
73
}
75
74
76
75
get srcDir ( ) {
77
- return path . join ( this . appRoot , this . config . srcDir )
76
+ return path . join ( this . workingDir , this . config . srcDir )
78
77
}
79
78
80
79
get buildDir ( ) {
81
- return path . join ( this . appRoot , '.aleph' , this . mode )
80
+ return path . join ( this . workingDir , '.aleph' , this . mode )
82
81
}
83
82
84
83
get outputDir ( ) {
85
- return path . join ( this . appRoot , this . config . outputDir )
84
+ return path . join ( this . workingDir , this . config . outputDir )
86
85
}
87
86
88
87
isHMRable ( url : string ) {
@@ -149,7 +148,7 @@ export class Project {
149
148
}
150
149
}
151
150
152
- async callAPI ( req : ServerRequest , loc : { pathname : string , search ?: string } ) : Promise < APIHandler | null > {
151
+ async callAPI ( req : ServerRequest , loc : { pathname : string , search ?: string } ) {
153
152
const [ url , chain ] = this . #apiRouting. createRouter ( {
154
153
...loc ,
155
154
pathname : decodeURI ( loc . pathname )
@@ -172,7 +171,6 @@ export class Project {
172
171
} else {
173
172
respondError ( req , 404 , 'page not found' )
174
173
}
175
- return null
176
174
}
177
175
178
176
async getSSRData ( loc : { pathname : string , search ?: string } ) : Promise < [ number , any ] > {
@@ -245,7 +243,7 @@ export class Project {
245
243
await this . ssg ( )
246
244
247
245
// copy public assets
248
- const publicDir = path . join ( this . appRoot , 'public' )
246
+ const publicDir = path . join ( this . workingDir , 'public' )
249
247
if ( existsDirSync ( publicDir ) ) {
250
248
let n = 0
251
249
for await ( const { path : p } of walk ( publicDir , { includeDirs : false , skip : [ / ( ^ | \/ ) \. D S _ S t o r e $ / ] } ) ) {
@@ -332,7 +330,7 @@ export class Project {
332
330
333
331
/** load config from `aleph.config.(json|mjs|js|ts)` */
334
332
private async loadConfig ( ) {
335
- const importMapFile = path . join ( this . appRoot , 'import_map.json' )
333
+ const importMapFile = path . join ( this . workingDir , 'import_map.json' )
336
334
if ( existsFileSync ( importMapFile ) ) {
337
335
const importMap = JSON . parse ( await Deno . readTextFile ( importMapFile ) )
338
336
const imports : ImportMap = fixImportMap ( importMap . imports )
@@ -348,7 +346,7 @@ export class Project {
348
346
const config : Record < string , any > = { }
349
347
350
348
for ( const name of Array . from ( [ 'ts' , 'js' , 'mjs' , 'json' ] ) . map ( ext => `aleph.config.${ ext } ` ) ) {
351
- const p = path . join ( this . appRoot , name )
349
+ const p = path . join ( this . workingDir , name )
352
350
if ( existsFileSync ( p ) ) {
353
351
log . info ( ' ✓' , name )
354
352
if ( name . endsWith ( '.json' ) ) {
@@ -425,7 +423,7 @@ export class Project {
425
423
Object . assign ( this . config , { postcss } )
426
424
} else {
427
425
for ( const name of Array . from ( [ 'ts' , 'js' , 'mjs' , 'json' ] ) . map ( ext => `postcss.config.${ ext } ` ) ) {
428
- const p = path . join ( this . appRoot , name )
426
+ const p = path . join ( this . workingDir , name )
429
427
if ( existsFileSync ( p ) ) {
430
428
log . info ( ' ✓' , name )
431
429
if ( name . endsWith ( '.json' ) ) {
@@ -506,7 +504,7 @@ export class Project {
506
504
await this . loadConfig ( )
507
505
508
506
// change current work dir to appDoot
509
- Deno . chdir ( this . appRoot )
507
+ Deno . chdir ( this . workingDir )
510
508
511
509
// inject env variables
512
510
Object . entries ( this . config . env ) . forEach ( ( [ key , value ] ) => Deno . env . set ( key , value ) )
0 commit comments