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

Commit 9f410fe

Browse files
author
Je
committed
refactor: fix _createMainModule method
refactor: improve _renderPage method
1 parent cbb84c3 commit 9f410fe

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

project.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,13 @@ export default class Project {
584584
baseUrl,
585585
defaultLocale,
586586
locales: [],
587-
routes: this.#routing.routes
587+
routes: this.#routing.routes,
588+
preloadModules: ['/404.js', '/app.js', '/data.js'].filter(id => this.#modules.has(id)).map(id => {
589+
return this._getRouteModule(this.#modules.get(id)!)
590+
})
588591
}
589592
const module = this._moduleFromURL('/main.js')
593+
const metaFile = path.join(this.buildDir, 'main.meta.json')
590594
const deps = [
591595
this.isDev && 'https://deno.land/x/aleph/hmr.ts',
592596
'https://deno.land/x/aleph/bootstrap.ts'
@@ -595,10 +599,6 @@ export default class Project {
595599
hash: this.#modules.get(String(url).replace(reHttp, '//').replace(reModuleExt, '.js'))?.hash || ''
596600
}))
597601

598-
config.preloadModules = ['/data.js', '/app.js', '/404.js'].filter(id => this.#modules.has(id)).map(id => {
599-
return this._getRouteModule(this.#modules.get(id)!)
600-
})
601-
602602
module.jsContent = [
603603
this.isDev && 'import "./-/deno.land/x/aleph/hmr.js";',
604604
'import bootstrap from "./-/deno.land/x/aleph/bootstrap.js";',
@@ -608,16 +608,24 @@ export default class Project {
608608
module.jsFile = path.join(this.buildDir, `main.${module.hash.slice(0, hashShort)}.js`)
609609
module.deps = deps
610610

611-
if (this.#modules.has(module.id)) {
612-
const prevHash = this.#modules.get(module.id)!.hash
613-
try {
611+
try {
612+
let prevHash = ''
613+
if (this.#modules.has(module.id)) {
614+
prevHash = this.#modules.get(module.id)!.hash
615+
} else if (existsFileSync(metaFile)) {
616+
const { hash } = JSON.parse(await Deno.readTextFile(metaFile))
617+
if (util.isNEString(hash)) {
618+
prevHash = hash
619+
}
620+
}
621+
if (prevHash !== '') {
614622
await Deno.remove(path.join(this.buildDir, `main.${prevHash.slice(0, hashShort)}.js`))
615-
} catch (e) { }
616-
}
623+
}
624+
} catch (e) { }
617625

618626
await Promise.all([
619627
writeTextFile(module.jsFile, module.jsContent),
620-
writeTextFile(path.join(this.buildDir, 'main.meta.json'), JSON.stringify({
628+
writeTextFile(metaFile, JSON.stringify({
621629
url: '/main.js',
622630
sourceHash: module.hash,
623631
hash: module.hash,
@@ -1036,6 +1044,7 @@ export default class Project {
10361044
})
10371045
try {
10381046
const appModule = this.#modules.get('/app.js')
1047+
const { E501Page } = await import('file://' + this.#modules.get('//deno.land/x/aleph/error.js')!.jsFile)
10391048
const { renderPage, renderHead } = await import('file://' + this.#modules.get('//deno.land/x/aleph/renderer.js')!.jsFile)
10401049
const { default: App } = appModule ? await import('file://' + appModule.jsFile) : {} as any
10411050
const staticData = await this.getStaticData()
@@ -1045,7 +1054,11 @@ export default class Project {
10451054
const { default: C } = await import('file://' + mod.jsFile)
10461055
const pc = pageComponentTree.find(pc => pc.id === mod.id)
10471056
if (pc) {
1048-
pc.Component = C
1057+
if (util.isLikelyReactComponent(C)) {
1058+
pc.Component = C
1059+
} else {
1060+
pc.Component = E501Page
1061+
}
10491062
}
10501063
})
10511064
await Promise.all(imports)
@@ -1084,7 +1097,6 @@ export default class Project {
10841097
})
10851098
return a
10861099
}
1087-
10881100
}
10891101

10901102
export function injectHmr({ id, sourceFilePath, jsContent }: Module): string {

types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ export interface APIHandle {
5050
(req: APIRequest, res: APIResponse): void
5151
}
5252

53-
54-
5553
export interface Route {
5654
path: string
5755
module: RouteModule

0 commit comments

Comments
 (0)