@hono/esbuild-transpiler
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

esbuild Transpiler Middleware

The esbuild Transpiler Middleware is a Hono Middleware designed to transpile content such as TypeScript or TSX. You can place your script written in TypeScript in a directory and serve it using serveStatic. When you apply this Middleware, the script will be transpiled into JavaScript code.

This Middleware uses esbuild. It works on Cloudflare Workers, Deno, Deno Deploy, or Node.js.

Usage

Usage differs depending on the platform.

Cloudflare Workers / Pages

Installation

npm i hono @hono/esbuild-transpiler esbuild-wasm

Example

import { Hono } from 'hono'
import { serveStatic } from 'hono/cloudflare-workers'
import { esbuildTranspiler } from '@hono/esbuild-transpiler/wasm'
// Specify the path of the esbuild wasm file.
import wasm from '../node_modules/esbuild-wasm/esbuild.wasm'

const app = new Hono()

app.get('/static/:scriptName{.+.tsx?}', esbuildTranspiler({ wasmModule: wasm }))
app.get('/static/*', serveStatic({ root: './' }))

export default app

global.d.ts:

declare module '*.wasm'

Deno / Deno Deploy

Example

import { Hono } from 'npm:hono'

import { serveStatic } from 'npm:hono/deno'
import { esbuildTranspiler } from 'npm:@hono/esbuild-transpiler'
import * as esbuild from 'https://deno.land/x/esbuild@v0.19.5/wasm.js'

const app = new Hono()

await esbuild.initialize({
  wasmURL: 'https://deno.land/x/esbuild@v0.19.5/esbuild.wasm',
  worker: false,
})

app.get('/static/*', esbuildTranspiler({ esbuild }))
app.get('/static/*', serveStatic())

Deno.serve(app.fetch)

Node.js

Installation

npm i hono @hono/node-server @hono/esbuild-transpiler esbuild

Example

import { Hono } from 'hono'
import { serve } from '@hono/node-server'
import { serveStatic } from '@hono/node-server/serve-static'
import { esbuildTranspiler } from '@hono/esbuild-transpiler/node'

const app = new Hono()

app.get('/static/:scriptName{.+.tsx?}', esbuildTranspiler())
app.get('/static/*', serveStatic({ root: './' }))

serve(app)

Notes

  • This middleware does not have a cache feature. If you want to cache the transpiled code, use Cache Middleware or your own custom middleware.
  • @hono/vite-dev-server does not support Wasm, so you can't use this Middleware with it. However, Vite can transpile them, so you might not need to use this.

Authors

Original idea and implementation for "Typescript Transpiler Middleware" is by Andres C. Rodriguez.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @hono/esbuild-transpiler

Weekly Downloads

6

Version

0.1.3

License

MIT

Unpacked Size

69.5 kB

Total Files

22

Last publish

Collaborators

  • yusukebe