Hono Vite Plugins

repository·main·Indexed 43 days ago

https://github.com/honojs/vite-plugins

A collection of Vite plugins to optimize development, building, and static site generation (SSG) for Hono applications. Includes @hono/vite-dev-server for local development with Node.js, Bun, and Cloudflare adapters; @hono/vite-ssg for static site generation; and @hono/vite-cloudflare-pages for preparing applications for Cloudflare Pages deployment, including automatic _routes.json generation.

Tokens
7.9K
Snippets
39
Records
45
Agent score
49%

What's inside Hono Vite Plugins

  1. Available Vite Plugins for Hono

    main

    The honojs/vite-plugins monorepo provides several specialized Vite plugins to enhance the Hono development and deployment experience. The available plugins are:

    • @hono/vite-dev-server: For running a local development server.
    • @hono/vite-build: For building Hono applications.
    • @hono/vite-ssg: For Static Site Generation (SSG) with Hono.
  2. Configure @hono/vite-ssg in Vite

    main

    To use the plugin, ensure your package.json contains "type": "module". Then, add ssg() to the plugins array in your vite.config.ts file.

    import { defineConfig } from 'vite'
    import ssg from '@hono/vite-ssg'
    
    export default defineConfig({
      plugins: [ssg()],
    })
  3. Implement client-side scripts with HMR

    main

    To use client-side scripts with Hot Module Replacement (HMR), inject a module script into your HTML. Use import.meta.env.PROD to switch between the source file (for dev) and the built asset (for production).

    app.get('/', (c) => {
      return c.html(
        <html>
          <head>
            {import.meta.env.PROD ? (
              <script type='module' src='/static/client.js'></script>
            ) : (
              <script type='module' src='/src/client.ts'></script>
            )}
          </head>
          <body>
            <h1>Hello</h1>
          </body>
        </html>
      )
    })
  4. Configure @hono/vite-dev-server in vite.config.ts

    main

    To use the dev-server, ensure your package.json has "type": "module". Create a vite.config.ts file and add the devServer plugin, specifying the entry point of your application.

    import { defineConfig } from 'vite'
    import devServer from '@hono/vite-dev-server'
    
    export default defineConfig({
      plugins: [
        devServer({
          entry: 'src/index.ts', // The file path of your application.
        }),
      ],
    })
  5. Use Cloudflare Adapter for local development

    main

    To simulate Cloudflare Bindings (from wrangler.toml) in your local dev environment, use the Cloudflare adapter. You must install wrangler and miniflare first.

    npm i -D wrangler miniflare
    import devServer from '@hono/vite-dev-server'
    import cloudflareAdapter from '@hono/vite-dev-server/cloudflare'
    import { defineConfig } from 'vite'
    
    export default defineConfig(async () => {
      return {
        plugins: [
          devServer({
            adapter: cloudflareAdapter,
          }),
        ],
      }
    })