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

Commit 3059b8a

Browse files
committed
refactor: improve react framework for bundle mode
1 parent eb2c3b8 commit 3059b8a

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

framework/react/bootstrap.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,19 @@ import { createElement } from 'https://esm.sh/react'
33
import { hydrate, render } from 'https://esm.sh/react-dom'
44
import { reModuleExt } from '../../shared/constants.ts'
55
import { Route, RouteModule, Routing } from '../core/routing.ts'
6-
import { AlephRoot, importModule } from './root.ts'
6+
import AlephAppRoot from './root.ts'
7+
import { importModule } from './util.ts'
78

8-
export default async function bootstrap({
9-
baseUrl,
10-
defaultLocale,
11-
locales,
12-
routes,
13-
preloadModules,
14-
renderMode
15-
}: {
9+
type BootstrapConfig = {
1610
baseUrl: string
1711
defaultLocale: string
1812
locales: string[]
1913
routes: Route[]
2014
preloadModules: RouteModule[],
2115
renderMode: 'ssr' | 'spa'
22-
}) {
16+
}
17+
18+
export default async function bootstrap({ baseUrl, defaultLocale, locales, routes, preloadModules, renderMode }: BootstrapConfig) {
2319
const { document } = window as any
2420
const ssrDataEl = document.querySelector('#ssr-data')
2521
const routing = new Routing(routes, baseUrl, defaultLocale, locales)
@@ -53,7 +49,7 @@ export default async function bootstrap({
5349
}
5450

5551
const rootEl = createElement(
56-
AlephRoot,
52+
AlephAppRoot,
5753
{
5854
url,
5955
routing,

framework/react/root.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import type { ComponentType } from 'https://esm.sh/react'
22
import { createElement, useCallback, useEffect, useState } from 'https://esm.sh/react'
3-
import { hashShort, reModuleExt } from '../../shared/constants.ts'
43
import util from '../../shared/util.ts'
54
import type { RouterURL } from '../../types.ts'
65
import events from '../core/events.ts'
76
import { RouteModule, Routing } from '../core/routing.ts'
87
import { RouterContext } from './context.ts'
98
import { E400MissingDefaultExportAsComponent, E404Page, ErrorBoundary } from './error.ts'
10-
import { createPageProps, isLikelyReactComponent } from './util.ts'
9+
import { createPageProps, importModule, isLikelyReactComponent } from './util.ts'
1110

12-
export function AlephRoot({
11+
export default function AlephAppRoot({
1312
url,
1413
routing,
1514
sysComponents,
@@ -192,23 +191,3 @@ export function AlephRoot({
192191
)
193192
)
194193
}
195-
196-
export function importModule(baseUrl: string, mod: RouteModule, forceRefetch = false): Promise<any> {
197-
const { __ALEPH, document } = window as any
198-
const src = util.cleanPath(baseUrl + '/_aleph/' + mod.url.replace(reModuleExt, '') + `.${mod.hash.slice(0, hashShort)}.js`) + (forceRefetch ? `?t=${Date.now()}` : '')
199-
if (__ALEPH) {
200-
return new Promise((resolve, reject) => {
201-
const script = document.createElement('src')
202-
script.src = src
203-
script.onload = () => {
204-
resolve(__ALEPH.pack[mod.url])
205-
}
206-
script.onerror = (err: Error) => {
207-
reject(err)
208-
}
209-
document.body.appendChild(script)
210-
})
211-
} else {
212-
return import(src)
213-
}
214-
}

framework/react/util.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ComponentType } from 'https://esm.sh/react'
2-
import { reModuleExt } from '../../shared/constants.ts'
2+
import { hashShort, reModuleExt } from '../../shared/constants.ts'
33
import util from '../../shared/util.ts'
4+
import { RouteModule } from '../core/routing.ts'
45
import { E400MissingDefaultExportAsComponent } from './error.ts'
56

67
const symbolFor = typeof Symbol === 'function' && Symbol.for
@@ -12,14 +13,40 @@ export interface PageProps {
1213
pageProps: Partial<PageProps> & { name?: string }
1314
}
1415

16+
export function importModule(baseUrl: string, mod: RouteModule, forceRefetch = false): Promise<any> {
17+
const { __ALEPH, document } = window as any
18+
if (!__ALEPH || mod.url.startsWith('/pages/')) {
19+
const src = util.cleanPath(baseUrl + '/_aleph/' + mod.url.replace(reModuleExt, '') + `.${mod.hash.slice(0, hashShort)}.js`) + (forceRefetch ? `?t=${Date.now()}` : '')
20+
if (__ALEPH) {
21+
return new Promise((resolve, reject) => {
22+
const script = document.createElement('script')
23+
script.onload = () => {
24+
resolve(__ALEPH.pack[mod.url])
25+
}
26+
script.onerror = (err: Error) => {
27+
reject(err)
28+
}
29+
script.src = src
30+
document.body.appendChild(script)
31+
})
32+
} else {
33+
return import(src)
34+
}
35+
} else if (__ALEPH && mod.url in __ALEPH.pack) {
36+
return Promise.resolve(__ALEPH.pack[mod.url])
37+
} else {
38+
return Promise.reject(new Error(`Module '${mod.url}' not found`))
39+
}
40+
}
41+
1542
export function isLikelyReactComponent(type: any): Boolean {
1643
switch (typeof type) {
1744
case 'function':
1845
if (type.prototype != null) {
1946
if (type.prototype.isReactComponent) {
2047
return true
2148
}
22-
const ownNames = Object.getOwnPropertyNames(type.prototype);
49+
const ownNames = Object.getOwnPropertyNames(type.prototype)
2350
if (ownNames.length > 1 || ownNames[0] !== 'constructor') {
2451
return false
2552
}

0 commit comments

Comments
 (0)