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

Commit e6b2cc0

Browse files
committed
fix(server): compilation sideEffect should update routing
1 parent 940202b commit e6b2cc0

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

server/app.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export class Application {
2323
readonly ready: Promise<void>
2424

2525
#denoCacheDir = ''
26-
#mainJS = { code: '', hash: '' }
2726
#modules: Map<string, Module> = new Map()
2827
#pageRouting: Routing = new Routing()
2928
#apiRouting: Routing = new Routing()
@@ -645,19 +644,19 @@ export class Application {
645644
log.info('Start watching code changes...')
646645
for await (const event of w) {
647646
for (const p of event.paths) {
648-
const path = util.cleanPath(util.trimPrefix(p, this.srcDir))
647+
const url = util.cleanPath(util.trimPrefix(p, this.srcDir))
649648
const validated = () => {
650649
// ignore `.aleph` and output directories
651-
if (path.startsWith('/.aleph/') || path.startsWith(this.config.outputDir)) {
650+
if (url.startsWith('/.aleph/') || url.startsWith(this.config.outputDir)) {
652651
return false
653652
}
654653

655654
// is module
656-
if (isModuleURL(path)) {
657-
if (path.startsWith('/pages/') || path.startsWith('/api/')) {
655+
if (isModuleURL(url)) {
656+
if (url.startsWith('/pages/') || url.startsWith('/api/')) {
658657
return true
659658
}
660-
switch (trimModuleExt(path)) {
659+
switch (trimModuleExt(url)) {
661660
case '/404':
662661
case '/app':
663662
return true
@@ -666,65 +665,66 @@ export class Application {
666665

667666
// is dep
668667
for (const { deps } of this.#modules.values()) {
669-
if (deps.findIndex(dep => dep.url === path) > -1) {
668+
if (deps.findIndex(dep => dep.url === url) > -1) {
670669
return true
671670
}
672671
}
673672

674673
// is loaded by plugin
675-
return this.config.plugins.findIndex(p => p.type === 'loader' && p.test.test(path)) > -1
674+
return this.config.plugins.findIndex(p => p.type === 'loader' && p.test.test(url)) > -1
676675
}
677676

678677
if (validated()) {
679-
util.debounceX(path, () => {
678+
util.debounceX(url, () => {
680679
if (existsFileSync(p)) {
681680
let type = 'modify'
682-
if (!this.#modules.has(path)) {
681+
if (!this.#modules.has(url)) {
683682
type = 'add'
684683
}
685-
log.info(type, path)
686-
this.compile(path, { forceCompile: true }).then(mod => {
684+
log.info(type, url)
685+
this.compile(url, { forceCompile: true }).then(mod => {
687686
const hmrable = this.isHMRable(mod.url)
687+
const update = ({ url, hash }: Module) => {
688+
if (trimModuleExt(url) === '/app') {
689+
this.#renderCache.clear()
690+
} else if (url.startsWith('/pages/')) {
691+
this.#renderCache.delete(toPagePath(url))
692+
this.#pageRouting.update(this.getRouteModule({ url, hash }))
693+
} else if (url.startsWith('/api/')) {
694+
this.#apiRouting.update(this.getRouteModule({ url, hash }))
695+
}
696+
}
688697
if (hmrable) {
689698
if (type === 'add') {
690699
this.#fsWatchListeners.forEach(e => e.emit('add', this.getRouteModule(mod)))
691700
} else {
692701
this.#fsWatchListeners.forEach(e => e.emit('modify-' + mod.url, mod.hash))
693702
}
694703
}
695-
if (trimModuleExt(path) === '/app') {
696-
this.#renderCache.clear()
697-
} else if (path.startsWith('/pages/')) {
698-
this.#renderCache.delete(toPagePath(path))
699-
this.#pageRouting.update(this.getRouteModule(mod))
700-
} else if (path.startsWith('/api/')) {
701-
this.#apiRouting.update(this.getRouteModule(mod))
702-
}
703-
this.checkCompilationSideEffect(path, ({ url, hash }) => {
704-
if (url.startsWith('/pages/')) {
705-
this.#renderCache.delete(toPagePath(url))
706-
}
707-
if (!hmrable && this.isHMRable(url)) {
708-
this.#fsWatchListeners.forEach(w => w.emit('modify-' + url, hash))
704+
update(mod)
705+
this.checkCompilationSideEffect(url, (mod) => {
706+
update(mod)
707+
if (!hmrable && this.isHMRable(mod.url)) {
708+
this.#fsWatchListeners.forEach(w => w.emit('modify-' + mod.url, mod.hash))
709709
}
710710
})
711711
}).catch(err => {
712-
log.error(`compile(${path}):`, err.message)
712+
log.error(`compile(${url}):`, err.message)
713713
})
714-
} else if (this.#modules.has(path)) {
715-
if (trimModuleExt(path) === '/app') {
714+
} else if (this.#modules.has(url)) {
715+
if (trimModuleExt(url) === '/app') {
716716
this.#renderCache.clear()
717-
} else if (path.startsWith('/pages/')) {
718-
this.#renderCache.delete(toPagePath(path))
719-
this.#pageRouting.removeRoute(toPagePath(path))
720-
} else if (path.startsWith('/api/')) {
721-
this.#apiRouting.removeRoute(toPagePath(path))
717+
} else if (url.startsWith('/pages/')) {
718+
this.#renderCache.delete(toPagePath(url))
719+
this.#pageRouting.removeRoute(toPagePath(url))
720+
} else if (url.startsWith('/api/')) {
721+
this.#apiRouting.removeRoute(toPagePath(url))
722722
}
723-
this.#modules.delete(path)
724-
if (this.isHMRable(path)) {
725-
this.#fsWatchListeners.forEach(e => e.emit('remove', path))
723+
this.#modules.delete(url)
724+
if (this.isHMRable(url)) {
725+
this.#fsWatchListeners.forEach(e => e.emit('remove', url))
726726
}
727-
log.info('remove', path)
727+
log.info('remove', url)
728728
}
729729
}, 150)
730730
}
@@ -733,7 +733,7 @@ export class Application {
733733
}
734734

735735
/** returns the route module by given module. */
736-
private getRouteModule({ url, hash }: Module): RouteModule {
736+
private getRouteModule({ url, hash }: Pick<Module, 'url' | 'hash'>): RouteModule {
737737
const deps = this.lookupDeps(url).filter(({ isData, isStyle, isDynamic }) => !!(isData || (isStyle && isDynamic)))
738738
return { url, hash, asyncDeps: deps.length > 0 ? deps : undefined }
739739
}

0 commit comments

Comments
 (0)