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

Commit a2033ac

Browse files
committed
refactor: clean up
1 parent 0647278 commit a2033ac

File tree

13 files changed

+126
-135
lines changed

13 files changed

+126
-135
lines changed

cli.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ async function main() {
8585
return
8686
}
8787

88+
// proxy https://deno.land/x/aleph on localhost
89+
const v = Deno.env.get('ALEPH_DEV_PORT')
90+
if (v !== undefined && /^\d+$/.test(v)) {
91+
localProxy(parseInt(v))
92+
}
93+
94+
// sets log level
95+
const l = options.L || options['log-level']
96+
if (util.isNEString(l)) {
97+
log.setLevel(l.toLowerCase() as LevelNames)
98+
}
99+
88100
// check working Dir
89101
const workingDir = path.resolve(String(args[0] || '.'))
90102
if (!existsDirSync(workingDir)) {
@@ -105,18 +117,6 @@ async function main() {
105117
log.info('load env from', path.basename(p))
106118
}
107119

108-
// proxy https://deno.land/x/aleph on localhost
109-
const v = Deno.env.get('ALEPH_DEV_PORT')
110-
if (v !== undefined && /^\d+$/.test(v)) {
111-
localProxy(parseInt(v))
112-
}
113-
114-
// sets log level
115-
const l = options.L || options['log-level']
116-
if (util.isNEString(l)) {
117-
log.setLevel(l.toLowerCase() as LevelNames)
118-
}
119-
120120
await cmd(workingDir, options)
121121
}
122122

framework/core/module.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import util from '../../shared/util.ts'
2+
3+
export const moduleExts = ['tsx', 'jsx', 'ts', 'js', 'mjs']
4+
5+
export function toPagePath(url: string): string {
6+
let pathname = trimModuleExt(url)
7+
if (pathname.startsWith('/pages/')) {
8+
pathname = util.trimPrefix(pathname, '/pages')
9+
}
10+
if (pathname.endsWith('/index')) {
11+
pathname = util.trimSuffix(pathname, 'index')
12+
}
13+
if (pathname === '') {
14+
pathname = '/'
15+
}
16+
return pathname
17+
}
18+
19+
export function trimModuleExt(url: string) {
20+
for (const ext of moduleExts) {
21+
if (url.endsWith('.' + ext)) {
22+
return url.slice(0, -(ext.length + 1))
23+
}
24+
}
25+
return url
26+
}
27+
28+
export function importModule(baseUrl: string, url: string, forceRefetch = false): Promise<any> {
29+
const { __ALEPH: ALEPH, document } = window as any
30+
31+
if (ALEPH && url in ALEPH.pack) {
32+
return Promise.resolve(ALEPH.pack[url])
33+
}
34+
35+
if (ALEPH && url.startsWith('/pages/')) {
36+
const src = util.cleanPath(baseUrl + '/_aleph/' + trimModuleExt(url) + '.js')
37+
return new Promise((resolve, reject) => {
38+
const script = document.createElement('script')
39+
script.onload = () => {
40+
resolve(ALEPH.pack[url])
41+
}
42+
script.onerror = (err: Error) => {
43+
reject(err)
44+
}
45+
script.src = src
46+
document.body.appendChild(script)
47+
})
48+
}
49+
50+
const src = util.cleanPath(baseUrl + '/_aleph/' + trimModuleExt(url) + `.js`) + (forceRefetch ? `?t=${Date.now()}` : '')
51+
return import(src)
52+
}

framework/core/routing.ts

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import util from '../../shared/util.ts'
2-
import { moduleExts } from '../../shared/constants.ts'
32
import type { RouterURL } from '../../types.ts'
43
import events from './events.ts'
4+
import { toPagePath } from './module.ts'
55

66
const ghostRoute: Route = { path: '', module: { url: '' } }
77

@@ -289,52 +289,3 @@ export async function redirect(url: string, replace?: boolean) {
289289
}
290290
events.emit('popstate', { type: 'popstate', resetScroll: true })
291291
}
292-
293-
export function toPagePath(url: string): string {
294-
let pathname = trimModuleExt(url)
295-
if (pathname.startsWith('/pages/')) {
296-
pathname = util.trimPrefix(pathname, '/pages')
297-
}
298-
if (pathname.endsWith('/index')) {
299-
pathname = util.trimSuffix(pathname, 'index')
300-
}
301-
if (pathname === '') {
302-
pathname = '/'
303-
}
304-
return pathname
305-
}
306-
307-
export function trimModuleExt(url: string) {
308-
for (const ext of moduleExts) {
309-
if (url.endsWith('.' + ext)) {
310-
return url.slice(0, -(ext.length + 1))
311-
}
312-
}
313-
return url
314-
}
315-
316-
export function importModule(baseUrl: string, url: string, forceRefetch = false): Promise<any> {
317-
const { __ALEPH: ALEPH, document } = window as any
318-
319-
if (ALEPH && url in ALEPH.pack) {
320-
return Promise.resolve(ALEPH.pack[url])
321-
}
322-
323-
if (ALEPH && url.startsWith('/pages/')) {
324-
const src = util.cleanPath(baseUrl + '/_aleph/' + trimModuleExt(url) + '.js')
325-
return new Promise((resolve, reject) => {
326-
const script = document.createElement('script')
327-
script.onload = () => {
328-
resolve(ALEPH.pack[url])
329-
}
330-
script.onerror = (err: Error) => {
331-
reject(err)
332-
}
333-
script.src = src
334-
document.body.appendChild(script)
335-
})
336-
}
337-
338-
const src = util.cleanPath(baseUrl + '/_aleph/' + trimModuleExt(url) + `.js`) + (forceRefetch ? `?t=${Date.now()}` : '')
339-
return import(src)
340-
}

framework/react/bootstrap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ComponentType, createElement } from 'https://esm.sh/react'
22
import { hydrate, render } from 'https://esm.sh/react-dom'
3-
import { importModule, RouteModule, Routing, RoutingOptions, trimModuleExt } from '../core/routing.ts'
3+
import { importModule, trimModuleExt } from '../core/module.ts'
4+
import { RouteModule, Routing, RoutingOptions } from '../core/routing.ts'
45
import { loadPageDataFromTag } from './helper.ts'
56
import { createPageProps, PageRoute } from './pageprops.ts'
67
import Router from './router.ts'

framework/react/hoc.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { ComponentType, createElement } from 'https://esm.sh/react'
2+
import { useDeno, useRouter } from './hooks.ts'
3+
4+
/**
5+
* `withRouter` allows you to use `useRouter` hook with class component.
6+
*
7+
* ```tsx
8+
* class MyComponent extends React.Component {
9+
* render() {
10+
* return <p>{this.props.router.pathname}</p>
11+
* }
12+
* }
13+
* export default withRouter(MyComponent)
14+
* ```
15+
*/
16+
export function withRouter<P>(Component: ComponentType<P>) {
17+
return function WithRouter(props: P) {
18+
const router = useRouter()
19+
return createElement(Component, { ...props, router })
20+
}
21+
}
22+
23+
/**
24+
* `withDeno` allows you to use `useDeno` hook with class component.
25+
*
26+
* ```tsx
27+
* class MyComponent extends React.Component {
28+
* render() {
29+
* return <p>{this.props.deno.version}</p>
30+
* }
31+
* }
32+
* export default withDeno(() => ({ version: Deno.version.deno }))(MyComponent)
33+
* ```
34+
*
35+
* @param {Function} callback - hook callback.
36+
* @param {number} revalidate - revalidate duration in seconds.
37+
*/
38+
export function withDeno<T>(callback: () => (T | Promise<T>), revalidate?: number) {
39+
return function <P extends T>(Component: ComponentType<P>): ComponentType<Exclude<P, keyof T>> {
40+
return function WithDeno(props: Exclude<P, keyof T>) {
41+
const deno = useDeno<T>(callback, revalidate)
42+
return createElement(Component, { ...props, deno })
43+
}
44+
}
45+
}

framework/react/hooks.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,6 @@ export function useRouter(): RouterURL {
1919
return useContext(RouterContext)
2020
}
2121

22-
/**
23-
* `withRouter` allows you to use `useRouter` hook with class component.
24-
*
25-
* ```tsx
26-
* class MyComponent extends React.Component {
27-
* render() {
28-
* return <p>{this.props.router.pathname}</p>
29-
* }
30-
* }
31-
* export default withRouter(MyComponent)
32-
* ```
33-
*/
34-
export function withRouter<P>(Component: ComponentType<P>) {
35-
return function WithRouter(props: P) {
36-
const router = useRouter()
37-
return createElement(Component, { ...props, router })
38-
}
39-
}
40-
4122
/**
4223
* `useDeno` allows you to use Deno runtime in build time(SSR).
4324
*
@@ -88,27 +69,3 @@ export function useDeno<T = any>(callback: () => (T | Promise<T>), revalidate?:
8869
return null
8970
}, [id, pathname])
9071
}
91-
92-
/**
93-
* `withDeno` allows you to use `useDeno` hook with class component.
94-
*
95-
* ```tsx
96-
* class MyComponent extends React.Component {
97-
* render() {
98-
* return <p>{this.props.deno.version}</p>
99-
* }
100-
* }
101-
* export default withDeno(() => ({ version: Deno.version.deno }))(MyComponent)
102-
* ```
103-
*
104-
* @param {Function} callback - hook callback.
105-
* @param {number} revalidate - revalidate duration in seconds.
106-
*/
107-
export function withDeno<T>(callback: () => (T | Promise<T>), revalidate?: number) {
108-
return function <P extends T>(Component: ComponentType<P>): ComponentType<Exclude<P, keyof T>> {
109-
return function WithDeno(props: Exclude<P, keyof T>) {
110-
const deno = useDeno<T>(callback, revalidate)
111-
return createElement(Component, { ...props, deno })
112-
}
113-
}
114-
}

framework/react/pageprops.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ComponentType } from 'https://esm.sh/react'
22
import type { RouterURL } from '../../types.ts'
3-
import { toPagePath } from '../core/routing.ts'
3+
import { toPagePath } from '../core/module.ts'
44
import { E400MissingComponent } from './error.ts'
55
import { isLikelyReactComponent } from './helper.ts'
66

framework/react/router.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ComponentType, createElement, useCallback, useEffect, useState } from 'https://esm.sh/react'
22
import events from '../core/events.ts'
3-
import { importModule, RouteModule, Routing } from '../core/routing.ts'
3+
import { importModule } from '../core/module.ts'
4+
import { RouteModule, Routing } from '../core/routing.ts'
45
import { RouterContext } from './context.ts'
56
import { E400MissingComponent, E404Page, ErrorBoundary } from './error.ts'
67
import { isLikelyReactComponent, loadPageData } from './helper.ts'

server/app.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { buildChecksum, ImportMap, SourceType, transform, TransformOptions } from '../compiler/mod.ts'
22
import { colors, createHash, ensureDir, path, walk } from '../deps.ts'
33
import { EventEmitter } from '../framework/core/events.ts'
4-
import type { RouteModule } from '../framework/core/routing.ts'
5-
import { Routing, toPagePath } from '../framework/core/routing.ts'
4+
import { moduleExts, toPagePath, trimModuleExt } from '../framework/core/module.ts'
5+
import { Routing, RouteModule } from '../framework/core/routing.ts'
66
import {
77
defaultReactVersion,
88
minDenoVersion,
9-
moduleExts
9+
1010
} from '../shared/constants.ts'
1111
import {
1212
ensureTextFile,
@@ -17,13 +17,10 @@ import log from '../shared/log.ts'
1717
import util from '../shared/util.ts'
1818
import type {
1919
Config,
20-
21-
22-
23-
DependencyDescriptor, LoaderPlugin,
20+
DependencyDescriptor,
21+
LoaderPlugin,
2422
LoaderTransformResult,
2523
Module,
26-
2724
RouterURL,
2825
ServerApplication,
2926
TransformFn
@@ -35,13 +32,11 @@ import {
3532
computeHash,
3633
formatBytesWithColor,
3734
getAlephPkgUri,
38-
39-
getDenoDir, getRelativePath,
40-
35+
getDenoDir,
36+
getRelativePath,
4137
isLoaderPlugin,
4238
reFullVersion,
43-
toLocalUrl,
44-
trimModuleExt
39+
toLocalUrl
4540
} from './helper.ts'
4641
import { Renderer } from './ssr.ts'
4742

server/bundler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ECMA, minify as terser } from 'https://esm.sh/[email protected]'
22
import { transform } from '../compiler/mod.ts'
33
import { colors, ensureDir, path } from '../deps.ts'
4+
import { trimModuleExt } from '../framework/core/module.ts'
45
import { defaultReactVersion } from '../shared/constants.ts'
56
import { ensureTextFile, existsFileSync, lazyRemove } from '../shared/fs.ts'
67
import log from '../shared/log.ts'
@@ -12,8 +13,7 @@ import {
1213
clearCompilation,
1314
computeHash,
1415
getAlephPkgUri,
15-
isLoaderPlugin,
16-
trimModuleExt
16+
isLoaderPlugin
1717
} from './helper.ts'
1818

1919
export const bundlerRuntimeCode = `

0 commit comments

Comments
 (0)