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
Provenance
- Commit
5f60a18- Tree
27ba14d- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
47f7577
11 files changed+141 added−94 removed
deleted:bun_test/index.test.ts-85 removed
1-import { describe, expect, it } from 'bun:test'2-import { Hono } from '../src/index'3-import { basicAuth } from '../src/middleware/basic-auth'4-import { jwt } from '../src/middleware/jwt'5-import { serveStatic } from '../src/middleware/serve-static/bun'6-7-// Test just only minimal patterns.8-// Because others are tested well in Cloudflare Workers environment already.9-10-describe('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- })17-18- 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-})27-28-describe('Basic Auth Middleware', () => {29- const app = new Hono()30-31- const username = 'hono-user-a'32- const password = 'hono-password-a'33- app.use(34- '/auth/*',35- basicAuth({36- username,37- password,38- })39- )40-41- app.get('/auth/*', () => new Response('auth'))42-43- 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- })49-50- 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-})59-60-describe('Serve Static Middleware', () => {61- const app = new Hono()62- app.all('/favicon.ico', serveStatic({ path: './bun_test/favicon.ico' }))63-64- 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-})71-72-// JWT is not available for Bun73-// It throw the Error74-describe('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
1+import { describe, expect, it } from 'bun:test'2+import { Hono } from '../src/index'3+import { basicAuth } from '../src/middleware/basic-auth'4+import { jwt } from '../src/middleware/jwt'5+import { serveStatic } from '../src/middleware/serve-static/bun'6+7+// Test just only minimal patterns.8+// Because others are tested well in Cloudflare Workers environment already.9+10+describe('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+ })17+18+ 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+})27+28+describe('Basic Auth Middleware', () => {29+ const app = new Hono()30+31+ const username = 'hono-user-a'32+ const password = 'hono-password-a'33+ app.use(34+ '/auth/*',35+ basicAuth({36+ username,37+ password,38+ })39+ )40+41+ app.get('/auth/*', () => new Response('auth'))42+43+ 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+ })49+50+ 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+})59+60+describe('Serve Static Middleware', () => {61+ const app = new Hono()62+ app.all('/favicon.ico', serveStatic({ path: './bun_test/favicon.ico' }))63+64+ 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+})71+72+// JWT is not available for Bun73+// It throw the Error74+describe('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+})86+87+// To enable JSX middleware,88+// set "jsxImportSource": "hono/jsx" in the tsconfig.json89+describe('JSX Middleware', () => {90+ const app = new Hono()91+ app.get('/', (c) => {92+ return c.html(<h1>Hello</h1>)93+ })94+95+ 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
26 unmodified lines27282930313132333435153 unmodified linesExpand all26 unmodified lines expect(template.toString()).toBe('<p><span>a</span><span>b</span></p>') })
it('Empty elements are rended withtout closing tag', () => { const template = (<input/>) const template = <input /> expect(template.toString()).toBe('<input/>') })
it('Props value is null', () => {153 unmodified linesExpand allmodified:deno_dist/middleware/jsx/index.ts+3 added-2 removed
26 unmodified lines27 'track',28 'wbr',29 ]30 31-export const jsx = (31+export { jsxFn as jsx }32+const jsxFn = (33 tag: string | Function,34 props: Record<string, any>,35 ...children: (string | HtmlEscapedString)[]36 ): HtmlEscapedString => {88 unmodified lines125 }) as FC<T>126 }127 128 export 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
12345678import type { HtmlEscapedString } from '../html/index.ts'import { jsx } from './index.ts'
export function jsxDEV(tag: string | Function, props: Record<string, any>): HtmlEscapedString { const children = props.children ?? [] delete props['children'] return jsx(tag, props, ...children)}added:deno_dist/middleware/jsx/jsx-runtime.ts+2 added
12export { jsxDEV as jsx } from './jsx-dev-runtime.ts'export { jsxDEV as jsxs } from './jsx-dev-runtime.ts'modified:src/middleware/jsx/index.test.tsx+2 added-2 removed
1 import { Hono } from '../../hono'2-import { jsx, memo, Fragment } from '.'2+import { jsx, memo, Fragment } from './index'3 4 describe('JSX middleware', () => {5 const app = new Hono()6 20 unmodified lines27 expect(template.toString()).toBe('<p><span>a</span><span>b</span></p>')28 })29 30 it('Empty elements are rended withtout closing tag', () => {31- const template = (<input/>)31+ const template = <input />32 expect(template.toString()).toBe('<input/>')33 })34 35 it('Props value is null', () => {153 unmodified linesmodified:src/middleware/jsx/index.ts+3 added-2 removed
26 unmodified lines27 'track',28 'wbr',29 ]30 31-export const jsx = (31+export { jsxFn as jsx }32+const jsxFn = (33 tag: string | Function,34 props: Record<string, any>,35 ...children: (string | HtmlEscapedString)[]36 ): HtmlEscapedString => {88 unmodified lines125 }) as FC<T>126 }127 128 export 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
12345678import type { HtmlEscapedString } from '../html'import { jsx } from '.'
export function jsxDEV(tag: string | Function, props: Record<string, any>): HtmlEscapedString { const children = props.children ?? [] delete props['children'] return jsx(tag, props, ...children)}added:src/middleware/jsx/jsx-runtime.ts+2 added
1+export { jsxDEV as jsx } from './jsx-dev-runtime'2+export { jsxDEV as jsxs } from './jsx-dev-runtime'modified:package.json+11 added-2 removed
8 unmodified lines9 ],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 lines29 "./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 lines69 ],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 lines164 },165 "engines": {166 "node": ">=11.0.0"167 }159-}168+}