Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit c5d7b35

Browse files
author
Je
committed
refactor: cleanup
1 parent c231ba4 commit c5d7b35

File tree

3 files changed

+35
-56
lines changed

3 files changed

+35
-56
lines changed

head.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Children, createElement, isValidElement, PropsWithChildren, ReactElement, ReactNode, useEffect } from 'https://esm.sh/react'
2-
import type { AlephEnv } from './types.ts'
2+
import type { AlephEnv } from './project.ts'
33
import util, { hashShort } from './util.ts'
44

55
const serverHeadElements: Array<{ type: string, props: Record<string, any> }> = []

project.ts

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@ import log from './log.ts'
55
import { createRouter } from './router.ts'
66
import { colors, ensureDir, path, Sha1, walk } from './std.ts'
77
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'
99
import util, { existsDirSync, existsFileSync, hashShort, reHashJs, reHttp, reModuleExt, reStyleModuleExt } from './util.ts'
1010
import { cleanCSS, Document, less } from './vendor/mod.ts'
1111
import { version } from './version.ts'
1212

13-
interface Dep {
14-
url: string
15-
hash: string
16-
async?: boolean
17-
}
18-
1913
interface Module {
2014
id: string
2115
url: string
2216
isRemote: boolean
23-
deps: Dep[]
17+
deps: { url: string, hash: string, async?: boolean }[]
2418
sourceFilePath: string
2519
sourceType: string
2620
sourceHash: string
@@ -36,6 +30,13 @@ interface RenderResult {
3630
body: string
3731
}
3832

33+
export interface AlephEnv {
34+
appRoot: string
35+
buildID: string
36+
config: Config
37+
mode: 'development' | 'production'
38+
}
39+
3940
export default class Project {
4041
readonly mode: 'development' | 'production'
4142
readonly appRoot: string
@@ -99,7 +100,6 @@ export default class Project {
99100
moduleID === '/404.js' ||
100101
moduleID === '/app.js' ||
101102
moduleID === '/data.js' ||
102-
(moduleID === '/data/index.js' && !this.#modules.has('/data.js')) ||
103103
moduleID.startsWith('/pages/') ||
104104
moduleID.startsWith('/components/') ||
105105
reStyleModuleExt.test(moduleID)
@@ -137,9 +137,6 @@ export default class Project {
137137
}
138138
}
139139
}
140-
if (!this.#modules.has(modId) && modId == '/data.js') {
141-
modId = '/data/index.js'
142-
}
143140
if (!this.#modules.has(modId)) {
144141
log.warn(`can't get the module by path '${pathname}(${modId})'`)
145142
}
@@ -220,7 +217,7 @@ export default class Project {
220217
}
221218

222219
async getData() {
223-
const mod = this.#modules.get('/data.js') || this.#modules.get('/data/index.js')
220+
const mod = this.#modules.get('/data.js')
224221
if (mod) {
225222
try {
226223
const { default: Data } = await import("file://" + mod.jsFile)
@@ -290,8 +287,8 @@ export default class Project {
290287
}))
291288

292289
// 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')!
295292
const data = this.getData()
296293
await writeTextFile(path.join(distDir, `data.${hash.slice(0, hashShort)}.js`), `export default ${JSON.stringify(data)}`)
297294
}
@@ -388,8 +385,7 @@ export default class Project {
388385
}
389386

390387
private async _init() {
391-
const walkOptions = { includeDirs: false, exts: ['.js', '.ts', '.mjs'], skip: [/^\./, /\.d\.ts$/i, /\.(test|spec|e2e)\.m?(j|t)s$/i] }
392-
const dataDir = path.join(this.srcDir, 'data')
388+
const walkOptions = { includeDirs: false, exts: ['.js', '.ts', '.mjs'], skip: [/^\./, /\.d\.ts$/i, /\.(test|spec|e2e)\.m?(j|t)sx?$/i] }
393389
const apiDir = path.join(this.srcDir, 'api')
394390
const pagesDir = path.join(this.srcDir, 'pages')
395391

@@ -412,32 +408,21 @@ export default class Project {
412408
$RefreshSig$: () => (type: any) => type,
413409
})
414410

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'] })) {
416412
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))
441426
}
442427
}
443428

@@ -496,8 +481,7 @@ export default class Project {
496481
switch (moduleID) {
497482
case '/404.js':
498483
case '/app.js':
499-
case '/data.js':
500-
case '/data/index.js': {
484+
case '/data.js': {
501485
return true
502486
}
503487
default: {
@@ -520,7 +504,7 @@ export default class Project {
520504
util.debounceX(moduleID, () => {
521505
const removed = !existsFileSync(p)
522506
const cleanup = () => {
523-
if (moduleID === '/app.js' || moduleID === '/data.js' || moduleID === '/data/index.js') {
507+
if (moduleID === '/app.js' || moduleID === '/data.js') {
524508
this._clearPageRenderCache()
525509
} else if (moduleID.startsWith('/pages/')) {
526510
if (removed) {
@@ -638,8 +622,8 @@ export default class Project {
638622
url: String(url),
639623
hash: this.#modules.get(String(url).replace(reHttp, '//').replace(reModuleExt, '.js'))?.hash || ''
640624
}))
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')!
643627
config.coreModules.data = { id, hash }
644628
deps.push({ url, hash })
645629
}
@@ -864,6 +848,8 @@ export default class Project {
864848
].join(this.isDev ? '\n' : '')
865849
mod.jsSourceMap = ''
866850
mod.hash = hash
851+
} else if (mod.sourceType === 'sass' || mod.sourceType === 'scss') {
852+
// todo: support sass
867853
} else if (mod.sourceType === 'md' || mod.sourceType === 'mdx') {
868854
mod.jsContent = `export default function MD() { return React.createElement('pre', null, ${JSON.stringify(sourceContent)})}`
869855
mod.jsSourceMap = ''
@@ -1112,7 +1098,7 @@ export default class Project {
11121098
return ret
11131099
}
11141100

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()) {
11161102
const mod = this.getModule(moduleID)
11171103
if (!mod) {
11181104
return a

types.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ export interface SSROptions {
1717
readonly exclude?: string[]
1818
}
1919

20-
export interface AlephEnv {
21-
appRoot: string
22-
buildID: string
23-
config: Config
24-
mode: 'development' | 'production'
25-
}
26-
2720
export interface AppManifest {
2821
readonly baseUrl: string
2922
readonly defaultLocale: string

0 commit comments

Comments
 (0)