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

Commit 070eed0

Browse files
author
Je
committed
refactor: rename getPageData to getSSRData
improve compilation cleanup
1 parent a5b0087 commit 070eed0

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

project.ts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export class Project {
211211
return null
212212
}
213213

214-
async getPageData(loc: { pathname: string, search?: string }): Promise<[number, any]> {
214+
async getSSRData(loc: { pathname: string, search?: string }): Promise<[number, any]> {
215215
if (!this.isSSRable(loc.pathname)) {
216216
return [404, null]
217217
}
@@ -220,10 +220,10 @@ export class Project {
220220
return [status, data]
221221
}
222222

223-
async getPageHtml(loc: { pathname: string, search?: string }): Promise<[number, string]> {
223+
async getPageHtml(loc: { pathname: string, search?: string }): Promise<[number, string, Record<string, string> | null]> {
224224
if (!this.isSSRable(loc.pathname)) {
225225
const [url] = this.#routing.createRouter(loc)
226-
return [url.pagePath === '' ? 404 : 200, await this.getSPAIndexHtml()]
226+
return [url.pagePath === '' ? 404 : 200, await this.getSPAIndexHtml(), null]
227227
}
228228

229229
const { baseUrl } = this.config
@@ -240,7 +240,7 @@ export class Project {
240240
body,
241241
minify: !this.isDev
242242
})
243-
return [status, html]
243+
return [status, html, data]
244244
}
245245

246246
async getSPAIndexHtml() {
@@ -305,13 +305,12 @@ export class Project {
305305
}
306306
await Promise.all(Array.from(paths).map(async pathname => {
307307
if (this.isSSRable(pathname)) {
308-
const [status, html] = await this.getPageHtml({ pathname })
308+
const [status, html, data] = await this.getPageHtml({ pathname })
309309
if (status == 200) {
310-
const [_, data] = await this.getPageData({ pathname })
311310
const htmlFile = path.join(outputDir, pathname, 'index.html')
312-
const dataFile = path.join(outputDir, '_aleph/data', pathname, 'data.js')
313311
await writeTextFile(htmlFile, html)
314312
if (data) {
313+
const dataFile = path.join(outputDir, '_aleph/data', pathname, 'data.js')
315314
await writeTextFile(dataFile, `export default ` + JSON.stringify(data))
316315
}
317316
log.info(' ○', pathname, colors.dim('• ' + util.bytesString(html.length)))
@@ -548,7 +547,6 @@ export class Project {
548547

549548
const precompileUrls = [
550549
'https://deno.land/x/aleph/bootstrap.ts',
551-
'https://deno.land/x/aleph/renderer.ts',
552550
'https://deno.land/x/aleph/nomodule.ts',
553551
'https://deno.land/x/aleph/tsc/tslib.js',
554552
]
@@ -558,9 +556,9 @@ export class Project {
558556
for (const url of precompileUrls) {
559557
await this._compile(url)
560558
}
559+
await this._compile('https://deno.land/x/aleph/renderer.ts', { forceTarget: 'es2020' })
561560
await this._createMainModule()
562561

563-
// ensure react in deno is same with browser one
564562
const { renderPage, renderHead } = await import('file://' + this.#modules.get('//deno.land/x/aleph/renderer.js')!.jsFile)
565563
this.#renderer = { renderPage, renderHead }
566564

@@ -773,21 +771,7 @@ export class Project {
773771
hash: this.#modules.get(String(url).replace(reHttp, '//').replace(reModuleExt, '.js'))?.hash || ''
774772
}))
775773

776-
try {
777-
let prevHash = ''
778-
if (this.#modules.has(module.id)) {
779-
prevHash = this.#modules.get(module.id)!.hash
780-
} else if (existsFileSync(metaFile)) {
781-
const { hash } = JSON.parse(await Deno.readTextFile(metaFile))
782-
if (util.isNEString(hash)) {
783-
prevHash = hash
784-
}
785-
}
786-
if (prevHash !== '') {
787-
await Deno.remove(path.join(this.buildDir, `main.${prevHash.slice(0, hashShort)}.js`))
788-
}
789-
} catch (e) { }
790-
774+
await cleanupCompilation(module.jsFile)
791775
await Promise.all([
792776
writeTextFile(module.jsFile, module.jsContent),
793777
writeTextFile(metaFile, JSON.stringify({
@@ -803,7 +787,7 @@ export class Project {
803787
}
804788

805789
// todo: force recompile remote modules which URL don't specify version
806-
private async _compile(url: string, options?: { sourceCode?: string, forceCompile?: boolean }) {
790+
private async _compile(url: string, options?: { sourceCode?: string, forceCompile?: boolean, forceTarget?: string }) {
807791
const mod = this._moduleFromURL(url)
808792
if (this.#modules.has(mod.id) && !options?.forceCompile) {
809793
return this.#modules.get(mod.id)!
@@ -1006,7 +990,7 @@ export class Project {
1006990
const useDenos: string[] = []
1007991
const compileOptions = {
1008992
mode: this.mode,
1009-
target: this.config.buildTarget,
993+
target: options?.forceTarget || this.config.buildTarget,
1010994
reactRefresh: this.isDev && !mod.isRemote,
1011995
rewriteImportPath: (path: string, async?: boolean) => this._rewriteImportPath(mod, path, async),
1012996
signUseDeno: (id: string) => {
@@ -1086,13 +1070,8 @@ export class Project {
10861070
}
10871071

10881072
if (fsync) {
1089-
if (mod.jsFile != '') {
1090-
try {
1091-
await Deno.remove(mod.jsFile)
1092-
await Deno.remove(mod.jsFile + '.map')
1093-
} catch (e) { }
1094-
}
10951073
mod.jsFile = path.join(saveDir, name + (mod.isRemote ? '' : `.${mod.hash.slice(0, hashShort)}`)) + '.js'
1074+
await cleanupCompilation(mod.jsFile)
10961075
await Promise.all([
10971076
writeTextFile(metaFile, JSON.stringify({
10981077
url,
@@ -1244,7 +1223,7 @@ export class Project {
12441223
if (this.isDev) {
12451224
log.warn(`page '${url.pathname}' not found`)
12461225
}
1247-
return this._render404Page(url)
1226+
return await this._render404Page(url)
12481227
}
12491228
try {
12501229
const appModule = this.#modules.get('/app.js')
@@ -1425,3 +1404,21 @@ async function writeTextFile(filepath: string, content: string) {
14251404
await ensureDir(dir)
14261405
await Deno.writeTextFile(filepath, content)
14271406
}
1407+
1408+
async function cleanupCompilation(jsFile: string) {
1409+
const dir = path.dirname(jsFile)
1410+
const jsFileName = path.basename(jsFile)
1411+
if (!reHashJs.test(jsFile) || !existsDirSync(dir)) {
1412+
return
1413+
}
1414+
const jsName = jsFileName.split('.').slice(0, -2).join('.') + '.js'
1415+
for await (const entry of Deno.readDir(dir)) {
1416+
if (entry.isFile && (entry.name.endsWith('.js') || entry.name.endsWith('.js.map'))) {
1417+
const _jsName = util.trimSuffix(entry.name, '.map').split('.').slice(0, -2).join('.') + '.js'
1418+
if (_jsName === jsName && jsFileName !== entry.name) {
1419+
log.debug('cleanup', jsFileName, '->', entry.name)
1420+
await Deno.remove(path.join(dir, entry.name))
1421+
}
1422+
}
1423+
}
1424+
}

server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export async function start(appDir: string, port: number, isDev = false, reload
7272
if (pathname.startsWith('/_aleph/')) {
7373
if (pathname.startsWith('/_aleph/data/') && pathname.endsWith('/data.js')) {
7474
const [p, s] = util.splitBy(util.trimSuffix(util.trimPrefix(pathname, '/_aleph/data'), '/data.js'), '@')
75-
const [status, data] = await project.getPageData({ pathname: p, search: s })
75+
const [status, data] = await project.getSSRData({ pathname: p, search: s })
7676
if (status === 200) {
7777
resp.send(`export default ` + JSON.stringify(data), 'application/javascript; charset=utf-8')
7878
} else {

0 commit comments

Comments
 (0)