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

Commit 901ca70

Browse files
committed
refactor: better compilation error handle
1 parent 74e5b99 commit 901ca70

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

server/app.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ export class Application implements ServerApplication {
221221
}
222222
if (validated) {
223223
await this.compile(url)
224-
this.#pageRouting.update(...this.createRouteUpdate(url))
224+
if (this.#modules.has(url)) {
225+
this.#pageRouting.update(...this.createRouteUpdate(url))
226+
}
225227
}
226228
}
227229
}
@@ -232,7 +234,9 @@ export class Application implements ServerApplication {
232234
for await (const { path: p } of walk(apiDir, { ...walkOptions, exts: moduleExts })) {
233235
const url = util.cleanPath('/api/' + util.trimPrefix(p, apiDir))
234236
await this.compile(url)
235-
this.#apiRouting.update(...this.createRouteUpdate(url))
237+
if (this.#modules.has(url)) {
238+
this.#apiRouting.update(...this.createRouteUpdate(url))
239+
}
236240
}
237241
}
238242

@@ -772,7 +776,7 @@ export class Application implements ServerApplication {
772776
const v = plugin.resolve(url)
773777
let content: Uint8Array
774778
if (v instanceof Promise) {
775-
content = (await v)
779+
content = await v
776780
} else {
777781
content = v
778782
}
@@ -784,8 +788,12 @@ export class Application implements ServerApplication {
784788

785789
if (!util.isLikelyHttpURL(url)) {
786790
const filepath = join(this.srcDir, util.trimPrefix(url, 'file://'))
787-
const content = await Deno.readFile(filepath)
788-
return { content, contentType: null }
791+
if (existsFileSync(filepath)) {
792+
const content = await Deno.readFile(filepath)
793+
return { content, contentType: null }
794+
} else {
795+
return Promise.reject(new Error(`No such file`))
796+
}
789797
}
790798

791799
const u = new URL(url)
@@ -826,7 +834,7 @@ export class Application implements ServerApplication {
826834

827835
// download dep when deno cache failed
828836
let err = new Error('Unknown')
829-
for (let i = 0; i < 15; i++) {
837+
for (let i = 0; i < 10; i++) {
830838
if (i === 0) {
831839
if (!isLocalhost) {
832840
log.info('Download', url)
@@ -978,8 +986,8 @@ export class Application implements ServerApplication {
978986
if (!once) {
979987
this.#modules.set(url, mod)
980988
}
981-
try {
982-
if (existsFileSync(metaFile)) {
989+
if (existsFileSync(metaFile)) {
990+
try {
983991
const { url: __url, sourceHash, deps } = JSON.parse(await Deno.readTextFile(metaFile))
984992
if (__url === url && util.isNEString(sourceHash) && util.isArray(deps)) {
985993
mod.sourceHash = sourceHash
@@ -988,8 +996,8 @@ export class Application implements ServerApplication {
988996
log.warn(`removing invalid metadata '${name}.meta.json'`)
989997
Deno.remove(metaFile)
990998
}
991-
}
992-
} catch (e) { }
999+
} catch (e) { }
1000+
}
9931001
}
9941002

9951003
let sourceContent = new Uint8Array()
@@ -1020,13 +1028,19 @@ export class Application implements ServerApplication {
10201028
}
10211029
}
10221030
if (shouldFetch) {
1023-
const { content, contentType: ctype } = await this.fetchModule(url)
1024-
const sourceHash = computeHash(content)
1025-
sourceContent = content
1026-
contentType = ctype
1027-
if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) {
1028-
mod.sourceHash = sourceHash
1029-
shouldCompile = true
1031+
try {
1032+
const { content, contentType: ctype } = await this.fetchModule(url)
1033+
const sourceHash = computeHash(content)
1034+
sourceContent = content
1035+
contentType = ctype
1036+
if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) {
1037+
mod.sourceHash = sourceHash
1038+
shouldCompile = true
1039+
}
1040+
} catch (err) {
1041+
log.error(`Fetch module '${url}':`, err.message)
1042+
this.#modules.delete(url)
1043+
return mod
10301044
}
10311045
}
10321046
}
@@ -1035,13 +1049,12 @@ export class Application implements ServerApplication {
10351049
if (shouldCompile) {
10361050
const source = await this.precompile(url, sourceContent, contentType)
10371051
if (source === null) {
1038-
log.warn(`Unsupported module '${url}'`)
1052+
log.error(`Unsupported module '${url}'`)
10391053
this.#modules.delete(url)
10401054
return mod
10411055
}
10421056

10431057
const t = performance.now()
1044-
10451058
const { code, deps, starExports, map } = await transform(url, source.code, {
10461059
...this.defaultCompileOptions,
10471060
swcOptions: {

0 commit comments

Comments
 (0)