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

Commit 4e91a73

Browse files
committed
Clean up
1 parent 2e555a2 commit 4e91a73

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

server/aleph.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { delay } from 'https://deno.land/[email protected]/async/delay.ts'
12
import { dim } from 'https://deno.land/[email protected]/fmt/colors.ts'
23
import { indexOf, copy, equals } from 'https://deno.land/[email protected]/bytes/mod.ts'
34
import { ensureDir } from 'https://deno.land/[email protected]/fs/ensure_dir.ts'
@@ -915,8 +916,12 @@ export class Aleph implements IAleph {
915916
return [routePath, specifier, isIndex]
916917
}
917918

918-
async importModule<T = any>({ jsFile, hash, sourceHash }: Module): Promise<T> {
919-
return await import(`file://${join(this.#buildDir, jsFile)}#${(hash || sourceHash).slice(0, 6)}`)
919+
async importModule<T = any>({ specifier, jsFile, hash, sourceHash }: Module): Promise<T> {
920+
const path = join(this.#buildDir, jsFile)
921+
if (existsFile(path)) {
922+
return await import(`file://${path}#${(hash || sourceHash).slice(0, 6)}`)
923+
}
924+
throw new Error(`import ${specifier}: file not found: ${path}`)
920925
}
921926

922927
async getModuleJS(module: Module, injectHMRCode = false): Promise<Uint8Array | null> {
@@ -1287,23 +1292,7 @@ export class Aleph implements IAleph {
12871292

12881293
ms.stop(`transpile '${specifier}'`)
12891294

1290-
const cacheFp = join(this.#buildDir, jsFile)
1291-
const metaFp = cacheFp.slice(0, -3) + '.meta.json'
1292-
const metaJSON = JSON.stringify({
1293-
specifier,
1294-
sourceHash: module.sourceHash,
1295-
isStyle: module.isStyle,
1296-
ssrPropsFn: module.ssrPropsFn,
1297-
ssgPathsFn: module.ssgPathsFn,
1298-
denoHooks: module.denoHooks,
1299-
deps: module.deps,
1300-
}, undefined, 2)
1301-
await ensureDir(dirname(cacheFp))
1302-
await Promise.all([
1303-
Deno.writeFile(cacheFp, module.jsBuffer),
1304-
Deno.writeTextFile(metaFp, metaJSON),
1305-
sourceMap ? Deno.writeTextFile(`${cacheFp}.map`, sourceMap) : Promise.resolve(),
1306-
])
1295+
await this.cacheModule(module, sourceMap)
13071296
}
13081297

13091298
if (module.deps.length > 0) {
@@ -1401,7 +1390,7 @@ export class Aleph implements IAleph {
14011390
}
14021391
}
14031392

1404-
private async cacheModule(module: Module) {
1393+
private async cacheModule(module: Module, sourceMap?: string) {
14051394
const { specifier, jsBuffer, jsFile } = module
14061395
if (jsBuffer) {
14071396
const cacheFp = join(this.#buildDir, jsFile)
@@ -1418,6 +1407,7 @@ export class Aleph implements IAleph {
14181407
denoHooks: module.denoHooks,
14191408
deps: module.deps,
14201409
}, undefined, 2)),
1410+
sourceMap ? Deno.writeTextFile(`${cacheFp}.map`, sourceMap) : Promise.resolve(),
14211411
lazyRemove(cacheFp.slice(0, -3) + '.bundling.js'),
14221412
])
14231413
}

server/renderer.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ export class Renderer {
8989
const { default: App } = appModule ? await this.#aleph.importModule(appModule) : {} as any
9090
const nestedPageComponents = await Promise.all(nestedModules
9191
.map(async specifier => {
92-
const module = this.#aleph.getModule(specifier) || (await this.#aleph.compile(specifier))
92+
let module = this.#aleph.getModule(specifier)
93+
if (module === null) {
94+
module = await this.#aleph.compile(specifier)
95+
}
9396
const { default: Component, ssr } = await this.#aleph.importModule(module)
9497
let ssrProps = ssr?.props
9598
if (util.isFunction(ssrProps)) {
@@ -111,16 +114,16 @@ export class Renderer {
111114
nestedModules
112115
].flat())
113116

114-
// ensure working directory
115-
Deno.chdir(this.#aleph.workingDir)
116-
117117
const { head, body, data, scripts } = await this.#renderer.render(
118118
url,
119119
App,
120120
nestedPageComponents,
121121
styles
122122
)
123123

124+
// keep working directory
125+
Deno.chdir(this.#aleph.workingDir)
126+
124127
if (isDev) {
125128
log.info(`render '${url.toString()}' in ${Math.round(performance.now() - start)}ms`)
126129
}

0 commit comments

Comments
 (0)