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

Commit 1c3f008

Browse files
author
Wenjie Xia
committed
refactor: clean up
1 parent 79829bd commit 1c3f008

File tree

10 files changed

+177
-159
lines changed

10 files changed

+177
-159
lines changed

framework/core/hmr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hashShort } from '../../shared/constants.ts'
1+
import util from '../../shared/util.ts'
22
import events from './events.ts'
33

44
interface Callback {
@@ -83,7 +83,7 @@ socket.addEventListener('message', ({ data: rawData }: { data?: string }) => {
8383
}
8484
break
8585
}
86-
console.log(`[HMR]${hash ? ' [' + hash.slice(0, hashShort) + ']' : ''} ${type} module '${url}'`)
86+
console.log(`[HMR]${hash ? ' [' + util.shortHash(hash) + ']' : ''} ${type} module '${url}'`)
8787
} catch (err) {
8888
console.warn(err)
8989
}

framework/core/routing.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { reModuleExt } from '../../shared/constants.ts'
1+
import { pageModuleExts } from '../../shared/constants.ts'
22
import util from '../../shared/util.ts'
33
import type { RouterURL } from '../../types.ts'
44
import events from './events.ts'
@@ -51,7 +51,7 @@ export class Routing {
5151
}
5252

5353
update(module: RouteModule) {
54-
const newRoute: Route = { path: getPagePath(module.url), module }
54+
const newRoute: Route = { path: getPagePathname(module.url), module }
5555
const dirtyRoutes: Set<Route[]> = new Set()
5656
let exists = false
5757
let targetRoutes = this._routes
@@ -170,11 +170,6 @@ export class Routing {
170170
}
171171
}
172172

173-
export function getPagePath(url: string): string {
174-
const pathname = url.replace(reModuleExt, '').toLowerCase().replace(/^\/pages\//, '/').replace(/\/?index$/, '/')
175-
return pathname.startsWith('/api/') ? pathname : pathname.replace(/\s+/g, '-')
176-
}
177-
178173
function matchPath(routePath: string, locPath: string): [Record<string, string>, boolean] {
179174
const params: Record<string, string> = {}
180175
const routeSegments = util.splitPath(routePath)
@@ -235,3 +230,17 @@ export function isHttpUrl(url: string) {
235230
return false
236231
}
237232
}
233+
234+
export function getPagePathname(url: string): string {
235+
const pathname = trimPageModuleExt(url).replace(/^\/pages\//, '/').replace(/\/?index$/, '/')
236+
return pathname
237+
}
238+
239+
export function trimPageModuleExt(url: string) {
240+
for (const ext of pageModuleExts) {
241+
if (url.endsWith('.' + ext)) {
242+
return url.slice(0, -(ext.length + 1))
243+
}
244+
}
245+
return url
246+
}

framework/react/bootstrap.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { ComponentType } from 'https://esm.sh/react'
22
import { createElement } from 'https://esm.sh/react'
33
import { hydrate, render } from 'https://esm.sh/react-dom'
4-
import { reModuleExt } from '../../shared/constants.ts'
5-
import { Route, RouteModule, Routing } from '../core/routing.ts'
4+
import { Route, RouteModule, Routing, trimPageModuleExt } from '../core/routing.ts'
65
import Router from './router.ts'
76
import { importModule } from './util.ts'
87

@@ -25,7 +24,7 @@ export default async function bootstrap({ baseUrl, defaultLocale, locales, route
2524

2625
await Promise.all([...sharedModules, ...pageModuleTree].map(async mod => {
2726
const { default: C } = await importModule(baseUrl, mod)
28-
switch (mod.url.replace(reModuleExt, '')) {
27+
switch (trimPageModuleExt(mod.url)) {
2928
case '/404':
3029
customComponents['E404'] = C
3130
break

framework/react/renderer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { ComponentType, ReactElement } from 'https://esm.sh/react'
22
import { createElement } from 'https://esm.sh/react'
33
import { renderToString } from 'https://esm.sh/react-dom/server'
4-
import { hashShort } from '../../shared/constants.ts'
54
import util from '../../shared/util.ts'
65
import type { RenderResult, RouterURL } from '../../types.ts'
76
import events from '../core/events.ts'
@@ -143,7 +142,7 @@ export async function render(
143142

144143
// get inline-styles
145144
const rets = await Promise.all(styles?.filter(({ url }) => !url.startsWith('#inline-style-')).map(({ url, hash }) => {
146-
const path = util.isLikelyURL(url) ? '/-/' + url.split('://')[1] : `${url}.${hash.slice(0, hashShort)}`
145+
const path = util.isLikelyHttpURL(url) ? '/-/' + url.split('://')[1] : `${url}.${util.shortHash(hash)}`
147146
return import('file://' + util.cleanPath(`${Deno.cwd()}/.aleph/${buildMode}/${path}.js`))
148147
}) || [])
149148
rets.forEach(({ default: def }) => util.isFunction(def) && def())

framework/react/util.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { ComponentType } from 'https://esm.sh/react'
2-
import { hashShort, reModuleExt } from '../../shared/constants.ts'
32
import util from '../../shared/util.ts'
43
import type { RouterURL } from '../../types.ts'
5-
import { RouteModule } from '../core/routing.ts'
4+
import { getPagePathname, RouteModule, trimPageModuleExt } from '../core/routing.ts'
65
import { E400MissingComponent } from './error.ts'
76

87
const symbolFor = typeof Symbol === 'function' && Symbol.for
@@ -17,7 +16,7 @@ export interface PageProps {
1716
export function importModule(baseUrl: string, mod: RouteModule, forceRefetch = false): Promise<any> {
1817
const { __ALEPH, document } = window as any
1918
if (!__ALEPH || mod.url.startsWith('/pages/')) {
20-
const src = util.cleanPath(baseUrl + '/_aleph/' + mod.url.replace(reModuleExt, '') + `.${mod.hash.slice(0, hashShort)}.js`) + (forceRefetch ? `?t=${Date.now()}` : '')
19+
const src = util.cleanPath(baseUrl + '/_aleph/' + trimPageModuleExt(mod.url) + `.${util.shortHash(mod.hash)}.js`) + (forceRefetch ? `?t=${Date.now()}` : '')
2120
if (__ALEPH) {
2221
return new Promise((resolve, reject) => {
2322
const script = document.createElement('script')
@@ -91,19 +90,19 @@ export function createPageProps(componentTree: { url: string, Component?: Compon
9190
pageProps: {}
9291
}
9392
if (componentTree.length > 0) {
94-
Object.assign(pageProps, _createPagePropsSegment(componentTree[0]))
93+
Object.assign(pageProps, createPagePropsSegment(componentTree[0]))
9594
}
9695
if (componentTree.length > 1) {
9796
componentTree.slice(1).reduce((p, seg) => {
98-
const c = _createPagePropsSegment(seg)
97+
const c = createPagePropsSegment(seg)
9998
p.pageProps = c
10099
return c
101100
}, pageProps)
102101
}
103102
return pageProps
104103
}
105104

106-
function _createPagePropsSegment(seg: { url: string, Component?: ComponentType<any> }): PageProps {
105+
function createPagePropsSegment(seg: { url: string, Component?: ComponentType<any> }): PageProps {
107106
const pageProps: PageProps = {
108107
Page: null,
109108
pageProps: {}
@@ -113,7 +112,7 @@ function _createPagePropsSegment(seg: { url: string, Component?: ComponentType<a
113112
pageProps.Page = seg.Component
114113
} else {
115114
pageProps.Page = E400MissingComponent
116-
pageProps.pageProps = { name: 'Page: ' + util.trimPrefix(seg.url, '/pages').replace(reModuleExt, '') }
115+
pageProps.pageProps = { name: 'Page: ' + getPagePathname(seg.url) }
117116
}
118117
}
119118
return pageProps

0 commit comments

Comments
 (0)