Refactor client file serving to support .jsx extension and clean up extension resolution logic
This commit is contained in:
parent
8785cfe43c
commit
1b908f8ba3
|
|
@ -174,23 +174,24 @@ export class Hype<
|
||||||
|
|
||||||
// serve transpiled js
|
// serve transpiled js
|
||||||
this.on('GET', ['/client/:path{.+}', '/shared/:path{.+}'], async c => {
|
this.on('GET', ['/client/:path{.+}', '/shared/:path{.+}'], async c => {
|
||||||
let path = './src/' + c.req.path.replace('..', '.')
|
const reqPath = './src/' + c.req.path.replace('..', '.')
|
||||||
|
|
||||||
// path must end in .js or .ts
|
// strip known extension to get base path
|
||||||
if (!path.endsWith('.js') && !path.endsWith('.ts')) path += '.ts'
|
const base = reqPath.replace(/\.(js|jsx|ts|tsx)$/, '')
|
||||||
|
|
||||||
const ts = path.replace('.js', '.ts')
|
// try TS extensions first (needs transpilation)
|
||||||
if (await Bun.file(ts).exists())
|
for (const ext of ['.ts', '.tsx', '.jsx']) {
|
||||||
return new Response(await transpile(ts), { headers: { 'Content-Type': 'text/javascript' } })
|
const file = base + ext
|
||||||
|
if (await Bun.file(file).exists())
|
||||||
|
return new Response(await transpile(file), { headers: { 'Content-Type': 'text/javascript' } })
|
||||||
|
}
|
||||||
|
|
||||||
else if (await Bun.file(ts + 'x').exists())
|
// try plain .js (serve raw)
|
||||||
return new Response(await transpile(ts + 'x'), { headers: { 'Content-Type': 'text/javascript' } })
|
const jsFile = base + '.js'
|
||||||
|
if (await Bun.file(jsFile).exists())
|
||||||
|
return new Response(Bun.file(jsFile), { headers: { 'Content-Type': 'text/javascript' } })
|
||||||
|
|
||||||
else if (await Bun.file(path).exists())
|
return render404(c)
|
||||||
return new Response(Bun.file(path), { headers: { 'Content-Type': 'text/javascript' } })
|
|
||||||
|
|
||||||
else
|
|
||||||
return render404(c)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// file based routing
|
// file based routing
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user