Skip to content
Myr

feat(jsx): support `jsxImportSource` (#420)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat(jsx): support `jsxImportSource` Support `jsxImportSource`. You can use this option on Bun and Deno (Wrangler does not support it). if you write tsconfig as below, JSX middleware will be enabled without `import { jsx } from 'hono/jsx'`. ```json { "compilerOptions": { "jsx": "react-jsx", "jsxFragmentFactory": "Fragment", "jsxImportSource": "hono/jsx" } } ``` * fix export path

authored by Yusuke Wada · committed by GitHub · parent 47f7577 · Browse files · 11 files+141 added−94 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

11 files+141 added−94 removed

  • tests:+104 added−88 removed
  • other:+37 added−6 removed

11 files+141 added−94 removed

deleted:bun_test/index.test.ts-85 removed
1import { describe, expect, it } from 'bun:test'2import { Hono } from '../src/index'3import { basicAuth } from '../src/middleware/basic-auth'4import { jwt } from '../src/middleware/jwt'5import { serveStatic } from '../src/middleware/serve-static/bun'67// Test just only minimal patterns.8// Because others are tested well in Cloudflare Workers environment already.910describe('Basic', () => {11 const app = new Hono()12 app.get('/a/:foo', (c) => {13 c.header('x-param', c.req.param('foo'))14 c.header('x-query', c.req.query('q'))15 return c.text('Hello Deno!')16 })1718 it('Should return 200 Response', async () => {19 const req = new Request('http://localhost/a/foo?q=bar')20 const res = await app.request(req)21 expect(res.status).toBe(200)22 expect(await res.text()).toBe('Hello Deno!')23 expect(res.headers.get('x-param')).toBe('foo')24 expect(res.headers.get('x-query')).toBe('bar')25 })26})2728describe('Basic Auth Middleware', () => {29 const app = new Hono()3031 const username = 'hono-user-a'32 const password = 'hono-password-a'33 app.use(34 '/auth/*',35 basicAuth({36 username,37 password,38 })39 )4041 app.get('/auth/*', () => new Response('auth'))4243 it('Should not authorize, return 401 Response', async () => {44 const req = new Request('http://localhost/auth/a')45 const res = await app.request(req)46 expect(res.status).toBe(401)47 expect(await res.text()).toBe('Unauthorized')48 })4950 it('Should authorize, return 200 Response', async () => {51 const credential = 'aG9uby11c2VyLWE6aG9uby1wYXNzd29yZC1h'52 const req = new Request('http://localhost/auth/a')53 req.headers.set('Authorization', `Basic ${credential}`)54 const res = await app.request(req)55 expect(res.status).toBe(200)56 expect(await res.text()).toBe('auth')57 })58})5960describe('Serve Static Middleware', () => {61 const app = new Hono()62 app.all('/favicon.ico', serveStatic({ path: './bun_test/favicon.ico' }))6364 it('Should return static file correctly', async () => {65 const res = await app.request(new Request('http://localhost/favicon.ico'))66 await res.arrayBuffer()67 expect(res.status).toBe(200)68 expect(res.headers.get('Content-Type')).toBe('image/vnd.microsoft.icon')69 })70})7172// JWT is not available for Bun73// It throw the Error74describe('JWT Middleware (Not supported yet)', () => {75 const app = new Hono()76 let t = false77 try {78 app.use('/jwt/*', jwt({ secret: 'a-secret' }))79 } catch {80 t = true81 }82 it('Throw the error', () => {83 expect(t).toBe(true)84 })85})
added:bun_test/index.test.tsx+101 added
1import { describe, expect, it } from 'bun:test'2import { Hono } from '../src/index'3import { basicAuth } from '../src/middleware/basic-auth'4import { jwt } from '../src/middleware/jwt'5import { serveStatic } from '../src/middleware/serve-static/bun'67// Test just only minimal patterns.8// Because others are tested well in Cloudflare Workers environment already.910describe('Basic', () => {11 const app = new Hono()12 app.get('/a/:foo', (c) => {13 c.header('x-param', c.req.param('foo'))14 c.header('x-query', c.req.query('q'))15 return c.text('Hello Deno!')16 })1718 it('Should return 200 Response', async () => {19 const req = new Request('http://localhost/a/foo?q=bar')20 const res = await app.request(req)21 expect(res.status).toBe(200)22 expect(await res.text()).toBe('Hello Deno!')23 expect(res.headers.get('x-param')).toBe('foo')24 expect(res.headers.get('x-query')).toBe('bar')25 })26})2728describe('Basic Auth Middleware', () => {29 const app = new Hono()3031 const username = 'hono-user-a'32 const password = 'hono-password-a'33 app.use(34 '/auth/*',35 basicAuth({36 username,37 password,38 })39 )4041 app.get('/auth/*', () => new Response('auth'))4243 it('Should not authorize, return 401 Response', async () => {44 const req = new Request('http://localhost/auth/a')45 const res = await app.request(req)46 expect(res.status).toBe(401)47 expect(await res.text()).toBe('Unauthorized')48 })4950 it('Should authorize, return 200 Response', async () => {51 const credential = 'aG9uby11c2VyLWE6aG9uby1wYXNzd29yZC1h'52 const req = new Request('http://localhost/auth/a')53 req.headers.set('Authorization', `Basic ${credential}`)54 const res = await app.request(req)55 expect(res.status).toBe(200)56 expect(await res.text()).toBe('auth')57 })58})5960describe('Serve Static Middleware', () => {61 const app = new Hono()62 app.all('/favicon.ico', serveStatic({ path: './bun_test/favicon.ico' }))6364 it('Should return static file correctly', async () => {65 const res = await app.request(new Request('http://localhost/favicon.ico'))66 await res.arrayBuffer()67 expect(res.status).toBe(200)68 expect(res.headers.get('Content-Type')).toBe('image/vnd.microsoft.icon')69 })70})7172// JWT is not available for Bun73// It throw the Error74describe('JWT Middleware (Not supported yet)', () => {75 const app = new Hono()76 let t = false77 try {78 app.use('/jwt/*', jwt({ secret: 'a-secret' }))79 } catch {80 t = true81 }82 it('Throw the error', () => {83 expect(t).toBe(true)84 })85})8687// To enable JSX middleware,88// set "jsxImportSource": "hono/jsx" in the tsconfig.json89describe('JSX Middleware', () => {90 const app = new Hono()91 app.get('/', (c) => {92 return c.html(<h1>Hello</h1>)93 })9495 it('Should return rendered HTML', async () => {96 const res = await app.request(new Request('http://localhost/'))97 expect(res.status).toBe(200)98 expect(res.headers.get('Content-Type')).toBe('text/html; charset=UTF-8')99 expect(await res.text()).toBe('<h1>Hello</h1>')100 })101})
modified:deno_dist/middleware/jsx/index.test.tsx+1 added-1 removed
modified:deno_dist/middleware/jsx/index.ts+3 added-2 removed
26 unmodified lines
27 'track',28 'wbr',29]3031export const jsx = (31export { jsxFn as jsx }32const jsxFn = (33 tag: string | Function,34 props: Record<string, any>,35 ...children: (string | HtmlEscapedString)[]36): HtmlEscapedString => {
88 unmodified lines
125 }) as FC<T>126}127128export const Fragment = (props: { key?: string; children?: any }): HtmlEscapedString => {128 return jsx('', {}, ...(props.children || []))129 return jsxFn('', {}, ...(props.children || []))130}
added:deno_dist/middleware/jsx/jsx-dev-runtime.ts+8 added
added:deno_dist/middleware/jsx/jsx-runtime.ts+2 added
modified:src/middleware/jsx/index.test.tsx+2 added-2 removed
1import { Hono } from '../../hono'2import { jsx, memo, Fragment } from '.'2import { jsx, memo, Fragment } from './index'34describe('JSX middleware', () => {5 const app = new Hono()6
20 unmodified lines
27 expect(template.toString()).toBe('<p><span>a</span><span>b</span></p>')28 })2930 it('Empty elements are rended withtout closing tag', () => {31 const template = (<input/>)31 const template = <input />32 expect(template.toString()).toBe('<input/>')33 })3435 it('Props value is null', () => {
153 unmodified lines
modified:src/middleware/jsx/index.ts+3 added-2 removed
26 unmodified lines
27 'track',28 'wbr',29]3031export const jsx = (31export { jsxFn as jsx }32const jsxFn = (33 tag: string | Function,34 props: Record<string, any>,35 ...children: (string | HtmlEscapedString)[]36): HtmlEscapedString => {
88 unmodified lines
125 }) as FC<T>126}127128export const Fragment = (props: { key?: string; children?: any }): HtmlEscapedString => {128 return jsx('', {}, ...(props.children || []))129 return jsxFn('', {}, ...(props.children || []))130}
added:src/middleware/jsx/jsx-dev-runtime.ts+8 added
added:src/middleware/jsx/jsx-runtime.ts+2 added
1export { jsxDEV as jsx } from './jsx-dev-runtime'2export { jsxDEV as jsxs } from './jsx-dev-runtime'
modified:package.json+11 added-2 removed
8 unmodified lines
9 ],10 "scripts": {11 "test": "jest",12 "test:deno": "deno test --allow-read deno_test",13 "test:bun": "bun wiptest bun_test/index.test.ts",13 "test:bun": "bun wiptest --jsx-import-source ../src/middleware/jsx/jsx-dev-runtime bun_test/index.test.tsx",14 "test:all": "yarn test && yarn test:deno && yarn test:bun",15 "lint": "eslint --ext js,ts src .eslintrc.js",16 "lint:fix": "eslint --ext js,ts src .eslintrc.js --fix",17 "denoify": "rimraf deno_dist && denoify && rimraf 'deno_dist/**/*.test.ts'",18 "build": "rimraf dist && tsc --project tsconfig.build.esm.json && tsc --project tsconfig.build.json",
10 unmodified lines
29 "./cors": "./dist/middleware/cors/index.js",30 "./etag": "./dist/middleware/etag/index.js",31 "./html": "./dist/middleware/html/index.js",32 "./jsx": "./dist/middleware/jsx/index.js",33 "./jsx/jsx-dev-runtime": "./dist/middleware/jsx/jsx-dev-runtime.js",34 "./jsx/jsx-runtime": "./dist/middleware/jsx/jsx-runtime.js",35 "./jwt": "./dist/middleware/jwt/index.js",36 "./logger": "./dist/middleware/logger/index.js",37 "./powered-by": "./dist/middleware/powered-by/index.js",38 "./pretty-json": "./dist/middleware/pretty-json/index.js",
30 unmodified lines
69 ],70 "jsx": [71 "./dist/middleware/jsx"72 ],73 "jsx-runtime": [74 "./dist/middleware/jsx/jsx-runtime.d.ts"75 ],76 "jsx-dev-runtime": [77 "./dist/middleware/jsx/jsx-dev-runtime.d.ts"78 ],79 "jwt": [80 "./dist/middleware/jwt"81 ],82 "logger": [
81 unmodified lines
164 },165 "engines": {166 "node": ">=11.0.0"167 }159}168}