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

Commit 82449b7

Browse files
author
Je
committed
refactor: cleanup
1 parent f022fe9 commit 82449b7

File tree

10 files changed

+199
-186
lines changed

10 files changed

+199
-186
lines changed

app.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import util, { hashShort } from './util.ts'
99
export function ALEPH({ initial }: {
1010
initial: {
1111
manifest: AppManifest
12-
pageModules: Record<string, { moduleId: string, hash: string }>
12+
pageModules: Record<string, Module>
1313
url: RouterURL
1414
data: Record<string, any>
1515
components: Record<string, ComponentType<any>>
@@ -80,16 +80,16 @@ export function ALEPH({ initial }: {
8080
console.log('[DATA]', data)
8181
setData(data)
8282
}
83-
const onAddModule = async ({ moduleId, hash }: Module) => {
84-
if (moduleId === '/404.js') {
85-
const { default: Component } = await import(getModuleImportUrl(baseUrl, { moduleId, hash }) + '?t=' + Date.now())
83+
const onAddModule = async (mod: Module) => {
84+
if (mod.id === '/404.js') {
85+
const { default: Component } = await import(getModuleImportUrl(baseUrl, mod) + '?t=' + Date.now())
8686
if (util.isLikelyReactComponent(Component)) {
8787
setE404({ Component })
8888
} else {
8989
setE404({ Component: E404Page })
9090
}
91-
} else if (moduleId === '/app.js') {
92-
const { default: Component } = await import(getModuleImportUrl(baseUrl, { moduleId, hash }) + '?t=' + Date.now())
91+
} else if (mod.id === '/app.js') {
92+
const { default: Component } = await import(getModuleImportUrl(baseUrl, mod) + '?t=' + Date.now())
9393
if (util.isLikelyReactComponent(Component)) {
9494
setApp({ Component })
9595
} else {
@@ -98,15 +98,15 @@ export function ALEPH({ initial }: {
9898
Component: E501App
9999
}))
100100
}
101-
} else if (moduleId === '/data.js' || moduleId === '/data/index.js') {
102-
const { default: data } = await import(getModuleImportUrl(baseUrl, { moduleId, hash }) + '?t=' + Date.now())
101+
} else if (mod.id === '/data.js' || mod.id === '/data/index.js') {
102+
const { default: data } = await import(getModuleImportUrl(baseUrl, mod) + '?t=' + Date.now())
103103
console.log('[DATA]', data)
104104
setData(data)
105-
} else if (moduleId.startsWith('/pages/')) {
106-
const pagePath = util.trimSuffix(moduleId, '.js').replace(/\s+/g, '-').replace(/\/?index$/i, '/')
105+
} else if (mod.id.startsWith('/pages/')) {
106+
const pagePath = util.trimSuffix(mod.id, '.js').replace(/\s+/g, '-').replace(/\/?index$/i, '/')
107107
setPageModules(pageModules => ({
108108
...pageModules,
109-
[pagePath]: { moduleId, hash }
109+
[pagePath]: mod
110110
}))
111111
}
112112
}
@@ -120,10 +120,10 @@ export function ALEPH({ initial }: {
120120
setData({})
121121
} else if (moduleId.startsWith('/pages/')) {
122122
setPageModules(pageModules => {
123-
const newPageModules: Record<string, { moduleId: string, hash: string }> = {}
123+
const newPageModules: Record<string, Module> = {}
124124
for (const pagePath in pageModules) {
125125
const mod = pageModules[pagePath]
126-
if (mod.moduleId !== moduleId) {
126+
if (mod.id !== moduleId) {
127127
newPageModules[pagePath] = mod
128128
}
129129
}
@@ -192,6 +192,6 @@ export async function redirect(url: string, replace: boolean) {
192192
events.emit('popstate', { type: 'popstate' })
193193
}
194194

195-
export function getModuleImportUrl(baseUrl: string, { moduleId, hash }: Module) {
196-
return util.cleanPath(baseUrl + '/_aleph/' + moduleId.replace(/\.js$/, `.${hash.slice(0, hashShort)}.js`))
195+
export function getModuleImportUrl(baseUrl: string, mod: Module) {
196+
return util.cleanPath(baseUrl + '/_aleph/' + mod.id.replace(/\.js$/, `.${mod.hash.slice(0, hashShort)}.js`))
197197
}

bootstrap.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export default async function bootstrap({
1010
baseUrl,
1111
defaultLocale,
1212
locales,
13-
keyModules,
13+
coreModules,
1414
pageModules
1515
}: AppManifest & {
16-
keyModules: Record<string, Module>
16+
coreModules: Record<string, Module>
1717
pageModules: Record<string, Module>
1818
}) {
1919
const { document } = window as any
@@ -46,9 +46,9 @@ export default async function bootstrap({
4646
{ default: E404 },
4747
{ default: Page }
4848
] = await Promise.all([
49-
keyModules.data ? import(getModuleImportUrl(baseUrl, keyModules.data)) : Promise.resolve({ default: {} }),
50-
keyModules.app ? import(getModuleImportUrl(baseUrl, keyModules.app)) : Promise.resolve({}),
51-
keyModules['404'] ? import(getModuleImportUrl(baseUrl, keyModules['404'])) : Promise.resolve({}),
49+
coreModules.data ? import(getModuleImportUrl(baseUrl, coreModules.data)) : Promise.resolve({ default: {} }),
50+
coreModules.app ? import(getModuleImportUrl(baseUrl, coreModules.app)) : Promise.resolve({}),
51+
coreModules['404'] ? import(getModuleImportUrl(baseUrl, coreModules['404'])) : Promise.resolve({}),
5252
pageModule ? import(getModuleImportUrl(baseUrl, pageModule)) : Promise.resolve({}),
5353
])
5454
const el = React.createElement(

cli.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createHtml } from './html.ts'
22
import log from './log.ts'
33
import { getContentType } from './server/mime.ts'
44
import { listenAndServe, path, ServerRequest, walk } from './std.ts'
5-
import util from './util.ts'
5+
import util, { existsDirSync, existsFileSync } from './util.ts'
66
import { version } from './version.ts'
77

88
const commands = ['init', 'fetch', 'dev', 'start', 'build']
@@ -89,7 +89,7 @@ async function main() {
8989
}
9090

9191
// proxy https://deno.land/x/aleph
92-
if (util.existsFile('./import_map.json')) {
92+
if (existsFileSync('./import_map.json')) {
9393
const { imports } = JSON.parse(Deno.readTextFileSync('./import_map.json'))
9494
Object.assign(globalThis, { ALEPH_IMPORT_MAP: { imports } })
9595
if (imports['https://deno.land/x/aleph/']) {
@@ -150,7 +150,7 @@ async function main() {
150150
const walkOptions = { includeDirs: false, exts: ['.js', '.jsx', '.mjs', '.ts', '.tsx'], skip: [/\.d\.ts$/i], dep: 1 }
151151
const pagesDir = path.join(path.resolve(args[0] || '.'), 'pages')
152152
let hasIndexPage = false
153-
if (util.existsDir(pagesDir)) {
153+
if (existsDirSync(pagesDir)) {
154154
for await (const { path: p } of walk(pagesDir, walkOptions)) {
155155
if (path.basename(p).split('.')[0] === 'index') {
156156
hasIndexPage = true
@@ -167,8 +167,8 @@ async function main() {
167167
const command = hasCommand ? args.shift() : 'dev'
168168
import(`./cli/${command}.ts`).then(({ default: cmd }) => {
169169
const appDir = path.resolve(args[0] || '.')
170-
if (command !== 'init' && !util.existsDir(appDir)) {
171-
log.fatal('No such app directory:', appDir)
170+
if (command !== 'init' && !existsDirSync(appDir)) {
171+
log.fatal('No such directory:', appDir)
172172
}
173173
cmd(appDir, argOptions)
174174
})

head.ts

Lines changed: 2 additions & 5 deletions
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 { resetImports } from './importor.ts'
2+
import { importAll } from './importor.ts'
33
import util from './util.ts'
44

55
const serverHeadElements: Array<{ type: string, props: Record<string, any> }> = []
@@ -28,10 +28,7 @@ export async function renderHead(styleModules?: string[]) {
2828
}
2929
}
3030
})
31-
await Promise.all(resetImports().map(p => {
32-
const { appDir, buildId } = (window as any).ALEPH_ENV as { appDir: string, buildId: string }
33-
return util.cleanPath(`${appDir}/.aleph/build-${buildId}/${p}`)
34-
}).map(p => import('file://' + p)))
31+
await importAll()
3532
styleModules?.filter(id => serverStyles.has(id)).forEach(id => {
3633
const { css, asLink } = serverStyles.get(id)!
3734
if (asLink) {

importor.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import util, { reStyleModuleExt } from './util.ts'
33

44
const serverImports: Set<string> = new Set()
55

6-
// reset imports and returns them
7-
export function resetImports() {
8-
const a = Array.from(serverImports)
6+
export async function importAll() {
7+
const { appDir, buildID } = (window as any).ALEPH_ENV as { appDir: string, buildID: string }
8+
for (const p of serverImports.values()) {
9+
await import('file://' + util.cleanPath(`${appDir}/.aleph/build-${buildID}/${p}`))
10+
}
911
serverImports.clear()
10-
return a
1112
}
1213

1314
interface ImportProps {

0 commit comments

Comments
 (0)