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

Commit 947b416

Browse files
committed
fix: fix importing module from esm.sh (#119)
1 parent a6c0953 commit 947b416

File tree

1 file changed

+53
-24
lines changed

1 file changed

+53
-24
lines changed

server/app.ts

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,26 @@ export class Application {
406406
})
407407
}
408408

409+
// inject browser navigator polyfill
410+
Object.assign(globalThis, {
411+
navigator: {
412+
connection: {
413+
downlink: 10,
414+
effectiveType: "4g",
415+
onchange: null,
416+
rtt: 50,
417+
saveData: false,
418+
},
419+
cookieEnabled: false,
420+
deviceMemory: 8,
421+
hardwareConcurrency: 4,
422+
language: 'en',
423+
onLine: true,
424+
userAgent: `Deno/${Deno.version.deno}`,
425+
vendor: 'Deno Land',
426+
}
427+
})
428+
409429
if (!this.isDev) {
410430
log.info('Building...')
411431
}
@@ -618,21 +638,21 @@ export class Application {
618638
const url = new URL(importUrl)
619639
let pathname = url.pathname
620640
let ok = [...moduleExts, 'css', 'pcss'].includes(path.extname(pathname).slice(1))
621-
if (ok) {
641+
if (!ok) {
622642
for (const plugin of this.config.plugins) {
623643
if (plugin.type === 'loader' && plugin.test.test(pathname)) {
624644
ok = true
625645
break
626646
}
627647
}
628648
}
629-
if (!ok) {
630-
pathname += '.js'
631-
}
632649
let search = Array.from(url.searchParams.entries()).map(([key, value]) => value ? `${key}=${value}` : key)
633650
if (search.length > 0) {
634651
pathname += '_' + search.join(',')
635652
}
653+
if (!ok) {
654+
pathname += '.js'
655+
}
636656
return [
637657
'/-/',
638658
(url.protocol === 'http:' ? 'http_' : ''),
@@ -777,17 +797,11 @@ export class Application {
777797
} else if (isRemote) {
778798
const isLocalhost = /^https?:\/\/localhost(:\d+)?\//.test(url)
779799
if (['js', 'ts', 'jsx', 'tsx'].includes(mod.loader) && !isLocalhost) {
780-
try {
781-
sourceContent = await this.fetchDependency(url)
782-
const sourceHash = computeHash(sourceContent)
783-
if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) {
784-
mod.sourceHash = sourceHash
785-
changed = true
786-
}
787-
} catch (err) {
788-
log.error(`dependency '${url}' not found`)
789-
mod.error = err
790-
return mod
800+
sourceContent = await this.fetchDependency(url)
801+
const sourceHash = computeHash(sourceContent)
802+
if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) {
803+
mod.sourceHash = sourceHash
804+
changed = true
791805
}
792806
} else {
793807
// todo: cache non-localhost file to local drive
@@ -1112,16 +1126,31 @@ export class Application {
11121126
}
11131127

11141128
// download dep when deno cache failed
1115-
log.info('Download', url)
1116-
const buffer = await fetch(u.toString()).then(resp => {
1117-
if (resp.status !== 200) {
1118-
return Promise.reject(new Error(resp.statusText))
1129+
const retryTimes = 10
1130+
let err = new Error('Unknown')
1131+
for (let i = 0; i < retryTimes; i++) {
1132+
if (i === 0) {
1133+
log.info('Download', url)
1134+
} else {
1135+
log.debug('Download error:', err)
1136+
log.warn(`Download ${url} failed, retrying...`)
11191137
}
1120-
return resp.arrayBuffer()
1121-
})
1122-
const content = await Deno.readAll(new Deno.Buffer(buffer))
1123-
await Deno.writeFile(cacheFilename, content)
1124-
return content
1138+
try {
1139+
const buffer = await fetch(u.toString()).then(resp => {
1140+
if (resp.status !== 200) {
1141+
return Promise.reject(new Error(resp.statusText))
1142+
}
1143+
return resp.arrayBuffer()
1144+
})
1145+
const content = await Deno.readAll(new Deno.Buffer(buffer))
1146+
await Deno.writeFile(cacheFilename, content)
1147+
return content
1148+
} catch (e) {
1149+
err = e
1150+
}
1151+
}
1152+
1153+
return Promise.reject(err)
11251154
}
11261155

11271156
/** bundle modules for production. */

0 commit comments

Comments
 (0)