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

Commit 8ced27c

Browse files
author
Je
committed
refactor: cleanup
1 parent c5d7b35 commit 8ced27c

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

head.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const serverHeadElements: Array<{ type: string, props: Record<string, any> }> =
66
const serverStyles: Map<string, { css: string, asLink: boolean }> = new Map()
77

88
export async function renderHead(styleModules?: { url: string, hash: string, async?: boolean }[]) {
9-
const { appRoot, buildID } = (window as any).ALEPH_ENV as AlephEnv
9+
const { build: { appRoot, buildID } } = (window as any).ALEPH_ENV as AlephEnv
1010
const tags: string[] = []
1111
serverHeadElements.forEach(({ type, props }) => {
1212
if (type === 'title') {

project.ts

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { createRouter } from './router.ts'
66
import { colors, ensureDir, path, Sha1, walk } from './std.ts'
77
import { compile } from './tsc/compile.ts'
88
import type { APIHandle, Config, Location, RouterURL } from './types.ts'
9-
import util, { existsDirSync, existsFileSync, hashShort, reHashJs, reHttp, reModuleExt, reStyleModuleExt } from './util.ts'
9+
import util, { existsDirSync, existsFileSync, hashShort, reHashJs, reHttp, reMDExt, reModuleExt, reStyleModuleExt } from './util.ts'
1010
import { cleanCSS, Document, less } from './vendor/mod.ts'
1111
import { version } from './version.ts'
1212

@@ -31,10 +31,13 @@ interface RenderResult {
3131
}
3232

3333
export interface AlephEnv {
34-
appRoot: string
35-
buildID: string
36-
config: Config
37-
mode: 'development' | 'production'
34+
version: string
35+
build: {
36+
mode: 'development' | 'production'
37+
buildID: string
38+
appRoot: string
39+
config: Config
40+
}
3841
}
3942

4043
export default class Project {
@@ -395,10 +398,13 @@ export default class Project {
395398

396399
Object.assign(globalThis, {
397400
ALEPH_ENV: {
398-
appRoot: this.appRoot,
399-
buildID: this.buildID,
400-
config: this.config,
401-
mode: this.mode,
401+
version,
402+
build: {
403+
appRoot: this.appRoot,
404+
buildID: this.buildID,
405+
config: this.config,
406+
mode: this.mode,
407+
}
402408
} as AlephEnv,
403409
document: new Document(),
404410
innerWidth: 1920,
@@ -468,39 +474,40 @@ export default class Project {
468474
log.info('Start watching code changes...')
469475
for await (const event of w) {
470476
for (const p of event.paths) {
471-
const path = util.trimPrefix(util.trimPrefix(p, this.appRoot), '/')
477+
const path = '/' + util.trimPrefix(util.trimPrefix(p, this.appRoot), '/')
472478
const validated = (() => {
473-
if (!reModuleExt.test(path) && !reStyleModuleExt.test(path)) {
479+
if (!reModuleExt.test(path) && !reStyleModuleExt.test(path) && !reMDExt.test(path)) {
474480
return false
475481
}
476482
// ignore '.aleph' and output directories
477-
if (path.startsWith('.aleph/') || path.startsWith(this.config.outputDir.slice(1))) {
483+
if (path.startsWith('/.aleph/') || path.startsWith(this.config.outputDir)) {
478484
return false
479485
}
480-
const moduleID = '/' + path.replace(reModuleExt, '.js')
481-
switch (moduleID) {
482-
case '/404.js':
483-
case '/app.js':
484-
case '/data.js': {
485-
return true
486-
}
487-
default: {
488-
if ((moduleID.startsWith('/pages/') || moduleID.startsWith('/api/')) && moduleID.endsWith('.js')) {
486+
if (reModuleExt.test(path)) {
487+
switch (path.replace(reModuleExt, '')) {
488+
case '/404':
489+
case '/app':
490+
case '/data': {
489491
return true
490492
}
491-
let isDep = false
492-
for (const { deps } of this.#modules.values()) {
493-
if (deps.findIndex(dep => dep.url === '/' + path) > -1) {
494-
isDep = true
495-
break
493+
default: {
494+
if (path.startsWith('/pages/') || path.startsWith('/api/')) {
495+
return true
496496
}
497497
}
498-
return isDep
499498
}
500499
}
500+
let isDep = false
501+
for (const { deps } of this.#modules.values()) {
502+
if (deps.findIndex(dep => dep.url === path) > -1) {
503+
isDep = true
504+
break
505+
}
506+
}
507+
return isDep
501508
})()
502509
if (validated) {
503-
const moduleID = '/' + path.replace(reModuleExt, '.js')
510+
const moduleID = path.replace(reModuleExt, '.js')
504511
util.debounceX(moduleID, () => {
505512
const removed = !existsFileSync(p)
506513
const cleanup = () => {
@@ -525,8 +532,8 @@ export default class Project {
525532
if (!this.#modules.has(moduleID)) {
526533
type = 'add'
527534
}
528-
log.info(type, '/' + path)
529-
this._compile('/' + path, { forceCompile: true }).then(({ hash }) => {
535+
log.info(type, path)
536+
this._compile(path, { forceCompile: true }).then(({ hash }) => {
530537
const hmrable = this.isHMRable(moduleID)
531538
if (hmrable) {
532539
if (type === 'add') {
@@ -536,7 +543,7 @@ export default class Project {
536543
}
537544
}
538545
cleanup()
539-
this._updateDependency('/' + path, hash, mod => {
546+
this._updateDependency(path, hash, mod => {
540547
if (!hmrable && this.isHMRable(mod.id)) {
541548
this.#fsWatchListeners.forEach(e => e.emit(mod.id, 'modify', mod.hash))
542549
}
@@ -545,15 +552,15 @@ export default class Project {
545552
}
546553
})
547554
}).catch(err => {
548-
log.error(`compile(/${path}):`, err.message)
555+
log.error(`compile(${path}):`, err.message)
549556
})
550557
} else if (this.#modules.has(moduleID)) {
551558
this.#modules.delete(moduleID)
552559
cleanup()
553560
if (this.isHMRable(moduleID)) {
554561
this.#fsWatchListeners.forEach(e => e.emit('remove', moduleID))
555562
}
556-
log.info('remove', '/' + path)
563+
log.info('remove', path)
557564
}
558565
}, 150)
559566
}

0 commit comments

Comments
 (0)