From 72e6a1851d4e3d852a9366e68e6e1299fcbee953 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sun, 14 Apr 2024 09:00:15 +0900 Subject: [PATCH] fix(esbuild-transpiler): set a content-type correctly --- packages/esbuild-transpiler/src/transpiler.ts | 12 +++--- packages/esbuild-transpiler/test/node.test.ts | 42 +++++++++++++++---- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/packages/esbuild-transpiler/src/transpiler.ts b/packages/esbuild-transpiler/src/transpiler.ts index 4add7bb5..6f289a5c 100644 --- a/packages/esbuild-transpiler/src/transpiler.ts +++ b/packages/esbuild-transpiler/src/transpiler.ts @@ -29,9 +29,7 @@ export const esbuildTranspiler = (options?: EsbuildTranspilerOptions) => { return } - const headers = { 'content-type': options?.contentType ?? 'text/javascript' } const script = await c.res.text() - const transformOptions: TransformOptions = options?.transformOptions ?? {} try { @@ -39,13 +37,15 @@ export const esbuildTranspiler = (options?: EsbuildTranspilerOptions) => { loader: 'tsx', ...transformOptions, }) - c.res = c.body(code, { - headers, - }) + c.res = c.body(code) + c.res.headers.set('content-type', options?.contentType ?? 'text/javascript') c.res.headers.delete('content-length') } catch (ex) { console.warn('Error transpiling ' + url.pathname + ': ' + ex) - c.res = new Response(script, { status: 500, headers }) + c.res = new Response(script, { + status: 500, + headers: { 'content-type': options?.contentType ?? 'text/javascript' }, + }) } } }) diff --git a/packages/esbuild-transpiler/test/node.test.ts b/packages/esbuild-transpiler/test/node.test.ts index 0953a633..df883cbe 100644 --- a/packages/esbuild-transpiler/test/node.test.ts +++ b/packages/esbuild-transpiler/test/node.test.ts @@ -15,37 +15,61 @@ function nw(code: string) { describe('esbuild Transpiler middleware', () => { const app = new Hono() - app.use('*', esbuildTranspiler()) - app.get('/file.ts', (c) => c.text(TS)) - app.get('/file.js', (c) => c.text(TS)) - app.get('/bad.ts', (c) => c.text(BAD)) - app.get('/file.tsx', (c) => c.text(TSX)) + + app.use('/static/*', esbuildTranspiler()) + app.get('/static/file.ts', (c) => + c.text(TS, 200, { + // Set a dummy content-type since Serve Static Middleware may set a unexpected content-type. + 'content-type': 'x-content-type', + }) + ) + app.get('/static/file.js', (c) => c.text(TS)) + app.get('/static/bad.ts', (c) => c.text(BAD)) + app.get('/static/file.tsx', (c) => c.text(TSX)) + + app.get( + '/static-custom-content-type.ts', + esbuildTranspiler({ + contentType: 'x-text/javascript', + }), + (c) => c.text(TS) + ) it('Should transpile typescript', async () => { // Request a Typescript page - const res = await app.request('http://localhost/file.ts') + const res = await app.request('http://localhost/static/file.ts') expect(res).not.toBeNull() expect(res.status).toBe(200) + expect(res.headers.get('content-type')).toBe('text/javascript') expect(nw(await res.text())).toBe('function add(a, b) { return a + b; }') }) + it('Should transpile typescript with a custom content-type', async () => { + // Request a Typescript page + const res = await app.request('http://localhost/static-custom-content-type.ts') + expect(res).not.toBeNull() + expect(res.status).toBe(200) + expect(res.headers.get('content-type')).toBe('x-text/javascript') + }) + it('Should not touch non TS content paths', async () => { // Request a Typescript page - const res = await app.request('http://localhost/file.js') + const res = await app.request('http://localhost/static/file.js') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(nw(await res.text())).toBe(TS) }) it('Should not meddle with with badly formed TS', async () => { - const res = await app.request('http://localhost/bad.ts') + const res = await app.request('http://localhost/static/bad.ts') expect(res).not.toBeNull() expect(res.status).toBe(500) + expect(res.headers.get('content-type')).toBe('text/javascript') expect(nw(await res.text())).toBe(BAD) }) it('Should transpile TSX', async () => { - const res = await app.request('http://localhost/file.tsx') + const res = await app.request('http://localhost/static/file.tsx') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(nw(await res.text())).toBe('/* @__PURE__ */ React.createElement("h1", null, "Hello");')