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

Commit 2382be5

Browse files
author
Je
committed
refactor: cleanup
1 parent ffb3498 commit 2382be5

File tree

4 files changed

+34
-30
lines changed

4 files changed

+34
-30
lines changed

head.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React, { Children, createElement, isValidElement, PropsWithChildren, ReactElement, ReactNode, useEffect } from 'https://esm.sh/react'
2+
import type { AlephEnv } from './types.ts'
23
import util, { hashShort } from './util.ts'
34

45
const serverHeadElements: Array<{ type: string, props: Record<string, any> }> = []
56
const serverStyles: Map<string, { css: string, asLink: boolean }> = new Map()
67

78
export async function renderHead(styleModules?: { url: string, hash: string, async?: boolean }[]) {
8-
const { appDir, buildID } = (window as any).ALEPH_ENV as { appDir: string, buildID: string }
9+
const { appRoot, buildID } = (window as any).ALEPH_ENV as AlephEnv
910
const tags: string[] = []
1011
serverHeadElements.forEach(({ type, props }) => {
1112
if (type === 'title') {
@@ -29,7 +30,7 @@ export async function renderHead(styleModules?: { url: string, hash: string, asy
2930
}
3031
})
3132
await Promise.all(styleModules?.filter(({ async }) => !!async).map(({ url, hash }) => {
32-
return import('file://' + util.cleanPath(`${appDir}/.aleph/build-${buildID}/${url}.${hash.slice(0, hashShort)}.js`))
33+
return import('file://' + util.cleanPath(`${appRoot}/.aleph/build-${buildID}/${url}.${hash.slice(0, hashShort)}.js`))
3334
}) || [])
3435
styleModules?.forEach(({ url }) => {
3536
if (serverStyles.has(url)) {

project.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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 { APIHandle, Config, Location, RouterURL } from './types.ts'
8+
import type { AlephEnv, 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'
@@ -38,7 +38,7 @@ interface RenderResult {
3838

3939
export default class Project {
4040
readonly mode: 'development' | 'production'
41-
readonly appDir: string
41+
readonly appRoot: string
4242
readonly config: Config
4343
readonly ready: Promise<void>
4444

@@ -49,7 +49,7 @@ export default class Project {
4949

5050
constructor(dir: string, mode: 'development' | 'production') {
5151
this.mode = mode
52-
this.appDir = path.resolve(dir)
52+
this.appRoot = dir
5353
this.config = {
5454
srcDir: '/',
5555
outputDir: '/dist',
@@ -77,15 +77,15 @@ export default class Project {
7777
}
7878

7979
get srcDir() {
80-
return path.join(this.appDir, this.config.srcDir)
80+
return path.join(this.appRoot, this.config.srcDir)
8181
}
8282

8383
get buildID() {
8484
return this.#buildID
8585
}
8686

8787
get buildDir() {
88-
return path.join(this.appDir, '.aleph', 'build-' + this.buildID)
88+
return path.join(this.appRoot, '.aleph', 'build-' + this.buildID)
8989
}
9090

9191
get apiPaths() {
@@ -269,11 +269,10 @@ export default class Project {
269269
await Promise.all([outputDir, distDir].map(dir => ensureDir(dir)))
270270

271271
// copy public files
272-
const publicDir = path.join(this.appDir, 'public')
272+
const publicDir = path.join(this.appRoot, 'public')
273273
if (existsDirSync(publicDir)) {
274274
for await (const { path: p } of walk(publicDir, { includeDirs: false })) {
275-
const rp = path.resolve(util.trimPrefix(p, publicDir))
276-
await Deno.copyFile(p, path.join(outputDir, rp))
275+
await Deno.copyFile(p, path.join(outputDir, util.trimPrefix(p, publicDir)))
277276
}
278277
}
279278

@@ -320,7 +319,7 @@ export default class Project {
320319
Object.assign(this.config.importMap, { imports: Object.assign({}, this.config.importMap.imports, imports) })
321320
}
322321

323-
const importMapFile = path.join(this.appDir, 'import_map.json')
322+
const importMapFile = path.join(this.appRoot, 'import_map.json')
324323
if (existsFileSync(importMapFile)) {
325324
const { imports } = JSON.parse(await Deno.readTextFile(importMapFile))
326325
Object.assign(this.config.importMap, { imports: Object.assign({}, this.config.importMap.imports, imports) })
@@ -389,7 +388,7 @@ export default class Project {
389388
}
390389

391390
private async _init() {
392-
const walkOptions = { includeDirs: false, exts: ['.js', '.ts', '.mjs'], skip: [/\.d\.ts$/i] }
391+
const walkOptions = { includeDirs: false, exts: ['.js', '.ts', '.mjs'], skip: [/^\./, /\.d\.ts$/i, /\.(test|spec|e2e)\.m?(j|t)s$/i] }
393392
const dataDir = path.join(this.srcDir, 'data')
394393
const apiDir = path.join(this.srcDir, 'api')
395394
const pagesDir = path.join(this.srcDir, 'pages')
@@ -400,11 +399,11 @@ export default class Project {
400399

401400
Object.assign(globalThis, {
402401
ALEPH_ENV: {
403-
appDir: this.appDir,
402+
appRoot: this.appRoot,
404403
buildID: this.buildID,
405404
config: this.config,
406405
mode: this.mode,
407-
},
406+
} as AlephEnv,
408407
document: new Document(),
409408
innerWidth: 1920,
410409
innerHeight: 1080,
@@ -419,8 +418,7 @@ export default class Project {
419418
switch (name) {
420419
case 'api':
421420
for await (const { path: p } of walk(apiDir, walkOptions)) {
422-
const rp = path.resolve(util.trimPrefix(p, apiDir))
423-
await this._compile('/api/' + rp)
421+
await this._compile('/api' + util.trimPrefix(p, apiDir))
424422
}
425423
break
426424
case 'data':
@@ -444,7 +442,7 @@ export default class Project {
444442
}
445443

446444
for await (const { path: p } of walk(pagesDir, { ...walkOptions, exts: [...walkOptions.exts, '.jsx', '.tsx', '.md', '.mdx'] })) {
447-
const rp = path.resolve(util.trimPrefix(p, pagesDir)) || '/'
445+
const rp = util.trimPrefix(p, pagesDir)
448446
const pagePath = rp.replace(reModuleExt, '').replace(/\s+/g, '-').replace(/\/index$/i, '') || '/'
449447
const mod = await this._compile('/pages' + rp)
450448
this.#pageModules.set(pagePath, {
@@ -485,7 +483,7 @@ export default class Project {
485483
log.info('Start watching code changes...')
486484
for await (const event of w) {
487485
for (const p of event.paths) {
488-
const path = util.trimPrefix(util.trimPrefix(p, this.appDir), '/')
486+
const path = util.trimPrefix(util.trimPrefix(p, this.appRoot), '/')
489487
const validated = (() => {
490488
if (!reModuleExt.test(path) && !reStyleModuleExt.test(path)) {
491489
return false
@@ -634,9 +632,8 @@ export default class Project {
634632
}
635633
const module = this._moduleFromURL('/main.js')
636634
const deps = [
637-
'https://deno.land/x/aleph/tsc/tslib.js',
638-
'https://deno.land/x/aleph/app.ts',
639-
this.isDev && 'https://deno.land/x/aleph/hmr.ts'
635+
this.isDev && 'https://deno.land/x/aleph/hmr.ts',
636+
'https://deno.land/x/aleph/bootstrap.ts'
640637
].filter(Boolean).map(url => ({
641638
url: String(url),
642639
hash: this.#modules.get(String(url).replace(reHttp, '//').replace(reModuleExt, '.js'))?.hash || ''
@@ -674,7 +671,6 @@ export default class Project {
674671

675672
module.jsContent = [
676673
this.isDev && 'import "./-/deno.land/x/aleph/hmr.js";',
677-
'import "./-/deno.land/x/aleph/tsc/tslib.js";',
678674
'import bootstrap from "./-/deno.land/x/aleph/bootstrap.js";',
679675
`bootstrap(${JSON.stringify(config, undefined, this.isDev ? 4 : undefined)});`
680676
].filter(Boolean).join(this.isDev ? '\n' : '')
@@ -927,7 +923,7 @@ export default class Project {
927923
if (!reHttp.test(dep.url)) {
928924
const depImportPath = relativePath(
929925
path.dirname(url),
930-
path.resolve('/', dep.url.replace(reModuleExt, ''))
926+
dep.url.replace(reModuleExt, '')
931927
)
932928
mod.jsContent = mod.jsContent.replace(/(import|Import|export)([^'"]*)("|')([^'"]+)("|')(\)|;)?/g, (s, key, from, ql, importPath, qr, end) => {
933929
if (
@@ -969,8 +965,8 @@ export default class Project {
969965
mod.deps.forEach(dep => {
970966
if (dep.url === depPath && dep.hash !== depHash && !trace?.has(mod.id)) {
971967
const depImportPath = relativePath(
972-
path.dirname(path.resolve('/', mod.url)),
973-
path.resolve('/', dep.url.replace(reModuleExt, ''))
968+
path.dirname(mod.url),
969+
dep.url.replace(reModuleExt, '')
974970
)
975971
dep.hash = depHash
976972
if (mod.id === '/main.js') {
@@ -1016,12 +1012,12 @@ export default class Project {
10161012
if (reHttp.test(importPath)) {
10171013
if (mod.isRemote) {
10181014
rewrittenPath = relativePath(
1019-
path.dirname(path.resolve('/', mod.url.replace(reHttp, '-/').replace(/:(\d+)/, '/$1'))),
1015+
path.dirname(mod.url.replace(reHttp, '/-/').replace(/:(\d+)/, '/$1')),
10201016
renameImportUrl(importPath)
10211017
)
10221018
} else {
10231019
rewrittenPath = relativePath(
1024-
path.dirname(path.resolve('/', mod.url)),
1020+
path.dirname(mod.url),
10251021
renameImportUrl(importPath)
10261022
)
10271023
}
@@ -1052,7 +1048,7 @@ export default class Project {
10521048
}
10531049
mod.deps.push({ url: sourceUrl.protocol + '//' + sourceUrl.host + pathname, hash: '', async })
10541050
} else {
1055-
mod.deps.push({ url: path.resolve('/', path.dirname(mod.url), importPath), hash: '', async })
1051+
mod.deps.push({ url: path.join(path.dirname(mod.url), importPath), hash: '', async })
10561052
}
10571053
}
10581054

@@ -1186,7 +1182,7 @@ function relativePath(from: string, to: string): string {
11861182

11871183
function renameImportUrl(importUrl: string): string {
11881184
const isRemote = reHttp.test(importUrl)
1189-
const url = new URL(isRemote ? importUrl : 'file://' + path.resolve('/', importUrl))
1185+
const url = new URL(isRemote ? importUrl : 'file://' + importUrl)
11901186
const ext = path.extname(path.basename(url.pathname)) || '.js'
11911187
let pathname = util.trimSuffix(url.pathname, ext)
11921188
let search = Array.from(url.searchParams.entries()).map(([key, value]) => value ? `${key}=${value}` : key)

server/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export async function start(appDir: string, port: number, isDev = false) {
162162

163163
// serve public files
164164
try {
165-
const filePath = path.join(project.appDir, 'public', pathname)
165+
const filePath = path.join(project.appRoot, 'public', pathname)
166166
const info = await Deno.lstat(filePath)
167167
if (!info.isDirectory) {
168168
const body = await Deno.readFile(filePath)

types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ 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+
2027
export interface AppManifest {
2128
readonly baseUrl: string
2229
readonly defaultLocale: string

0 commit comments

Comments
 (0)