From 1b908f8ba3d526aaa582aa495cc2c34703e3440c Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Fri, 20 Feb 2026 16:05:01 -0800 Subject: [PATCH] Refactor client file serving to support .jsx extension and clean up extension resolution logic --- src/index.tsx | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 06645a4..a434cd4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -174,23 +174,24 @@ export class Hype< // serve transpiled js 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 - if (!path.endsWith('.js') && !path.endsWith('.ts')) path += '.ts' + // strip known extension to get base path + const base = reqPath.replace(/\.(js|jsx|ts|tsx)$/, '') - const ts = path.replace('.js', '.ts') - if (await Bun.file(ts).exists()) - return new Response(await transpile(ts), { headers: { 'Content-Type': 'text/javascript' } }) + // try TS extensions first (needs transpilation) + for (const ext of ['.ts', '.tsx', '.jsx']) { + 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()) - return new Response(await transpile(ts + 'x'), { headers: { 'Content-Type': 'text/javascript' } }) + // try plain .js (serve raw) + 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 new Response(Bun.file(path), { headers: { 'Content-Type': 'text/javascript' } }) - - else - return render404(c) + return render404(c) }) // file based routing