2023-11-05 09:57:04 +08:00
|
|
|
import { Hono } from 'hono'
|
|
|
|
import { describe, it, expect } from 'vitest'
|
|
|
|
import { esbuildTranspiler } from '../src/transpilers/node'
|
|
|
|
|
|
|
|
const TS = 'function add(a: number, b: number) { return a + b; }'
|
|
|
|
const BAD = 'function { !!! !@#$ add(a: INT) return a + b + c; }'
|
|
|
|
const TSX = '<h1>Hello</h1>'
|
|
|
|
|
|
|
|
// No Whitespace
|
|
|
|
// Returns a code representation where every space chain has been collapsed
|
|
|
|
// Needed because different transpiler may produce different whitespace
|
|
|
|
function nw(code: string) {
|
|
|
|
return code.replace(/\s+/g, ' ').trim()
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('esbuild Transpiler middleware', () => {
|
|
|
|
const app = new Hono()
|
2024-04-14 08:04:06 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
2023-11-05 09:57:04 +08:00
|
|
|
|
|
|
|
it('Should transpile typescript', async () => {
|
|
|
|
// Request a Typescript page
|
2024-04-14 08:04:06 +08:00
|
|
|
const res = await app.request('http://localhost/static/file.ts')
|
2023-11-05 09:57:04 +08:00
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.status).toBe(200)
|
2024-04-14 08:04:06 +08:00
|
|
|
expect(res.headers.get('content-type')).toBe('text/javascript')
|
2023-11-05 09:57:04 +08:00
|
|
|
expect(nw(await res.text())).toBe('function add(a, b) { return a + b; }')
|
|
|
|
})
|
|
|
|
|
2024-04-14 08:04:06 +08:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2023-11-05 09:57:04 +08:00
|
|
|
it('Should not touch non TS content paths', async () => {
|
|
|
|
// Request a Typescript page
|
2024-04-14 08:04:06 +08:00
|
|
|
const res = await app.request('http://localhost/static/file.js')
|
2023-11-05 09:57:04 +08:00
|
|
|
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 () => {
|
2024-04-14 08:04:06 +08:00
|
|
|
const res = await app.request('http://localhost/static/bad.ts')
|
2023-11-05 09:57:04 +08:00
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.status).toBe(500)
|
2024-04-14 08:04:06 +08:00
|
|
|
expect(res.headers.get('content-type')).toBe('text/javascript')
|
2023-11-05 09:57:04 +08:00
|
|
|
expect(nw(await res.text())).toBe(BAD)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should transpile TSX', async () => {
|
2024-04-14 08:04:06 +08:00
|
|
|
const res = await app.request('http://localhost/static/file.tsx')
|
2023-11-05 09:57:04 +08:00
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(nw(await res.text())).toBe('/* @__PURE__ */ React.createElement("h1", null, "Hello");')
|
|
|
|
})
|
|
|
|
})
|