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

Commit 5979edd

Browse files
committed
refactor: clean up
1 parent 4b467d1 commit 5979edd

File tree

7 files changed

+47
-33
lines changed

7 files changed

+47
-33
lines changed

cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async function main() {
101101
Deno.env.set(key, value.trim())
102102
}
103103
})
104-
log.debug('load env from', path.basename(p))
104+
log.info('load env from', path.basename(p))
105105
}
106106

107107
// proxy https://deno.land/x/aleph on localhost

framework/core/routing.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import util from '../../shared/util.ts'
2+
import { moduleExts } from '../../shared/constants.ts'
23
import type { RouterURL } from '../../types.ts'
34
import events from './events.ts'
45

@@ -291,7 +292,7 @@ export async function redirect(url: string, replace?: boolean) {
291292
}
292293

293294
export function isModuleURL(url: string) {
294-
for (const ext of ['tsx', 'jsx', 'ts', 'js', 'mjs']) {
295+
for (const ext of moduleExts) {
295296
if (url.endsWith('.' + ext)) {
296297
return true
297298
}
@@ -301,7 +302,7 @@ export function isModuleURL(url: string) {
301302

302303
export function toPagePath(url: string): string {
303304
let pathname = url
304-
for (const ext of ['tsx', 'jsx', 'ts', 'js', 'mjs']) {
305+
for (const ext of moduleExts) {
305306
if (url.endsWith('.' + ext)) {
306307
pathname = url.slice(0, -(ext.length + 1))
307308
break

framework/react/helper.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createContext } from 'https://esm.sh/react'
22
import util from '../../shared/util.ts'
3+
import { hashShortLength, moduleExts } from '../../shared/constants.ts'
34
import type { RouterURL } from '../../types.ts'
45

56
const symbolFor = typeof Symbol === 'function' && Symbol.for
@@ -42,7 +43,7 @@ export function isLikelyReactComponent(type: any): Boolean {
4243
}
4344

4445
export function trimModuleExt(url: string) {
45-
for (const ext of ['tsx', 'jsx', 'ts', 'js', 'mjs']) {
46+
for (const ext of moduleExts) {
4647
if (url.endsWith('.' + ext)) {
4748
return url.slice(0, -(ext.length + 1))
4849
}
@@ -58,7 +59,7 @@ export function importModule(baseUrl: string, mod: { url: string, hash: string }
5859
}
5960

6061
if (ALEPH && mod.url.startsWith('/pages/')) {
61-
const src = util.cleanPath(baseUrl + '/_aleph/' + trimModuleExt(mod.url) + `.bundle.${util.shortHash(mod.hash)}.js`)
62+
const src = util.cleanPath(baseUrl + '/_aleph/' + trimModuleExt(mod.url) + `.bundle.${mod.hash.slice(0, hashShortLength)}.js`)
6263
return new Promise((resolve, reject) => {
6364
const script = document.createElement('script')
6465
script.onload = () => {
@@ -72,7 +73,7 @@ export function importModule(baseUrl: string, mod: { url: string, hash: string }
7273
})
7374
}
7475

75-
const src = util.cleanPath(baseUrl + '/_aleph/' + trimModuleExt(mod.url) + `.${util.shortHash(mod.hash)}.js`) + (forceRefetch ? `?t=${Date.now()}` : '')
76+
const src = util.cleanPath(baseUrl + '/_aleph/' + trimModuleExt(mod.url) + `.${mod.hash.slice(0, hashShortLength)}.js`) + (forceRefetch ? `?t=${Date.now()}` : '')
7677
return import(src)
7778
}
7879

server/app.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { colors, createHash, ensureDir, minify, path, walk } from '../deps.ts'
44
import { EventEmitter } from '../framework/core/events.ts'
55
import type { RouteModule } from '../framework/core/routing.ts'
66
import { createBlankRouterURL, isModuleURL, Routing, toPagePath } from '../framework/core/routing.ts'
7-
import { defaultReactVersion, minDenoVersion, hashShortLength } from '../shared/constants.ts'
7+
import { defaultReactVersion, minDenoVersion, moduleExts, hashShortLength } from '../shared/constants.ts'
88
import { ensureTextFile, existsDirSync, existsFileSync } from '../shared/fs.ts'
99
import log from '../shared/log.ts'
1010
import util from '../shared/util.ts'
@@ -71,9 +71,8 @@ export class Application implements ServerApplication {
7171
private async init(reload: boolean) {
7272
const t = performance.now()
7373
const alephPkgUri = getAlephPkgUri()
74-
const mode = this.mode
7574
const { env, framework, plugins, ssr } = this.config
76-
const walkOptions = { includeDirs: false, exts: ['tsx', 'jsx', 'ts', 'js', 'mjs'], skip: [/^\./, /\.d\.ts$/i, /\.(test|spec|e2e)\.m?(j|t)sx?$/i] }
75+
const walkOptions = { includeDirs: false, skip: [/(^|\/|\\)\./, /\.d\.ts$/i, /(\.|_)(test|spec|e2e)\.(tsx?|jsx?|mjs)?$/i] }
7776
const apiDir = path.join(this.srcDir, 'api')
7877
const pagesDir = path.join(this.srcDir, 'pages')
7978

@@ -122,14 +121,12 @@ export class Application implements ServerApplication {
122121
for (const plugin of plugins) {
123122
if (plugin.type === 'server') {
124123
await plugin.onInit(this)
125-
Object.assign(this, { mode }) // to avoid plugin from changing the mode
126124
}
127125
}
128126

129127
// init framework
130128
const { init } = await import(`../framework/${framework}/init.ts`)
131129
await init(this)
132-
Object.assign(this, { mode }) // to avoid framework from changing the mode
133130

134131
if (!this.isDev) {
135132
log.info('Building...')
@@ -150,33 +147,46 @@ export class Application implements ServerApplication {
150147
this.#renderer = await import('file://' + jsFile)
151148
}
152149

153-
// check custom components
154-
for await (const { path: p, } of walk(this.srcDir, { ...walkOptions, maxDepth: 1 })) {
155-
const name = path.basename(p)
156-
switch (trimModuleExt(name)) {
157-
case 'app':
158-
case '404':
159-
case 'loading':
160-
await this.compile('/' + name)
150+
// compile custom components
151+
for (const name of ['app', '404', 'loading']) {
152+
for (const ext of moduleExts) {
153+
if (existsFileSync(path.join(this.srcDir, name + '.' + ext))) {
154+
await this.compile('/' + name + '.' + ext)
161155
break
156+
}
157+
}
158+
}
159+
160+
// update page routing
161+
this.#pageRouting.config(this.config)
162+
for await (const { path: p } of walk(pagesDir, walkOptions)) {
163+
const url = util.cleanPath('/pages/' + util.trimPrefix(p, pagesDir))
164+
let validated = moduleExts.some(ext => p.endsWith('.' + ext))
165+
if (!validated) {
166+
validated = this.config.plugins.some(p => p.type === 'loader' && p.test.test(url) && p.allowPage)
167+
}
168+
if (validated) {
169+
const mod = await this.compile(url)
170+
this.#pageRouting.update(this.getRouteModule(mod))
162171
}
163172
}
164173

165174
// update api routing
166175
if (existsDirSync(apiDir)) {
167-
for await (const { path: p } of walk(apiDir, walkOptions)) {
168-
const mod = await this.compile(util.cleanPath('/api/' + util.trimPrefix(p, apiDir)))
176+
for await (const { path: p } of walk(apiDir, { ...walkOptions, exts: moduleExts })) {
177+
const url = util.cleanPath('/api/' + util.trimPrefix(p, apiDir))
178+
let mod: Module
179+
if (this.isDev) {
180+
// in dev mode, we pre-compile the api code to support re-import the api module
181+
// when it is changed.
182+
mod = await this.compile(url)
183+
} else {
184+
mod = { url, hash: '', sourceHash: '', deps: [], jsFile: p }
185+
}
169186
this.#apiRouting.update(this.getRouteModule(mod))
170187
}
171188
}
172189

173-
// update page routing
174-
this.#pageRouting.config(this.config)
175-
for await (const { path: p } of walk(pagesDir, { ...walkOptions })) {
176-
const mod = await this.compile(util.cleanPath('/pages/' + util.trimPrefix(p, pagesDir)))
177-
this.#pageRouting.update(this.getRouteModule(mod))
178-
}
179-
180190
// pre-bundle
181191
if (!this.isDev) {
182192
await this.bundle()
@@ -221,13 +231,13 @@ export class Application implements ServerApplication {
221231

222232
// is dep
223233
for (const { deps } of this.#modules.values()) {
224-
if (deps.findIndex(dep => dep.url === url) > -1) {
234+
if (deps.some(dep => dep.url === url)) {
225235
return true
226236
}
227237
}
228238

229239
// is loaded by plugin
230-
return this.config.plugins.findIndex(p => p.type === 'loader' && p.test.test(url)) > -1
240+
return this.config.plugins.some(p => p.type === 'loader' && p.test.test(url))
231241
}
232242

233243
if (validated()) {
@@ -455,7 +465,7 @@ export class Application implements ServerApplication {
455465
if (!this.isDev) {
456466
return false
457467
}
458-
for (const ext of ['tsx', 'jsx', 'ts', 'js', 'mjs']) {
468+
for (const ext of moduleExts) {
459469
if (url.endsWith('.' + ext)) {
460470
return url.startsWith('/pages/') ||
461471
url.startsWith('/components/') ||

server/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const defaultConfig: Readonly<Required<Config>> = {
2626
/** load config from `aleph.config.(ts|js|json)` */
2727
export async function loadConfig(workingDir: string): Promise<[Config, ImportMap]> {
2828
let data: Config = {}
29-
for (const name of Array.from(['ts', 'js', 'json']).map(ext => 'aleph.config.' + ext)) {
29+
for (const name of ['ts', 'js', 'json'].map(ext => 'aleph.config.' + ext)) {
3030
const p = path.join(workingDir, name)
3131
if (existsFileSync(p)) {
3232
log.info('Aleph server config loaded from', name)

server/helper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { colors, createHash, path } from '../deps.ts'
22
import { existsDirSync } from '../shared/fs.ts'
3+
import { moduleExts } from '../shared/constants.ts'
34
import log from '../shared/log.ts'
45
import util from '../shared/util.ts'
56
import { VERSION } from '../version.ts'
@@ -61,7 +62,7 @@ export function getRelativePath(from: string, to: string): string {
6162
}
6263

6364
export function trimModuleExt(url: string) {
64-
for (const ext of ['tsx', 'jsx', 'ts', 'js', 'mjs']) {
65+
for (const ext of moduleExts) {
6566
if (url.endsWith('.' + ext)) {
6667
return url.slice(0, -(ext.length + 1))
6768
}

shared/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const minDenoVersion = '1.7.0'
22
export const defaultReactVersion = '17.0.1'
33
export const hashShortLength = 9
4+
export const moduleExts = ['tsx', 'jsx', 'ts', 'js', 'mjs']

0 commit comments

Comments
 (0)