npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hono/vite-build

v1.1.0

Published

Vite plugin to build your Hono app

Downloads

8,937

Readme

@hono/vite-build

@hono/vite-build is a set of Vite plugins for building Hono applications with Vite. It supports multiple runtimes and platforms, allowing you to build a project that includes JavaScript files for these platforms from a Hono app.

Here are the modules included:

  • @hono/vite-build/base
  • @hono/vite-build/cloudflare-pages
  • @hono/vite-build/cloudflare-workers
  • @hono/vite-build/bun
  • @hono/vite-build/node

Usage

Install

You can install vite and @hono/vite-build via the npm.

npm i -D vite @hono/vite-build

Or you can install them with Bun.

bun add -D vite @hono/vite-build

Settings

Add "type": "module" to your package.json. Then, create vite.config.ts and edit it. The following is for Bun.

import { defineConfig } from 'vite'
import build from '@hono/vite-build/bun'
// import build from '@hono/vite-build/cloudflare-pages'
// import build from '@hono/vite-build/cloudflare-workers'
// import build from '@hono/vite-build/node'

export default defineConfig({
  plugins: [
    build({
      // Defaults are `src/index.ts`,`./src/index.tsx`,`./app/server.ts`
      entry: './src/index.tsx',
    }),
  ],
})

Build

Just run vite build.

npm exec vite build

Or

bunx --bun vite build

Run

Run with the command on your runtime. For examples:

Cloudflare Pages:

wrangler pages dev ./dist

Bun:

cd ./dist
bun run ./index.js

Node.js:

cd ./dist
node ./index.js

Common Options

type BuildOptions = {
  entry?: string | string[]
  output?: string
  outputDir?: string
  external?: string[]
  minify?: boolean
  emptyOutDir?: boolean
}

Default values:

export const defaultOptions = {
  entry: ['src/index.ts', './src/index.tsx', './app/server.ts'],
  output: 'index.js',
  outputDir: './dist',
  external: [],
  minify: true,
  emptyOutDir: false,
  staticPaths: [],
}

Platform specific things

Cloudflare Pages

This plugin generates _routes.json automatically. The automatic generation can be overridden by creating a public/_routes.json. See Create a _routes.json file on Cloudflare Docs for more details.

Example project

src/index.tsx:

import { Hono } from 'hono'

const app = new Hono()

app.get('/', (c) => {
  return c.html(
    <html>
      <head>
        <link href='/static/style.css' rel='stylesheet' />
      </head>
      <body>
        <h1>Hello!</h1>
      </body>
    </html>
  )
})

export default app

public/static/style.css:

h1 {
  font-family: Arial, Helvetica, sans-serif;
}

The project with those file will be built to the following files with @hono/vite-build/bun:

dist
├── index.js
└── static
 └── style.css

Build a client

If you also want to build a client-side script, you can configure it as follows.

export default defineConfig(({ mode }) => {
  if (mode === 'client') {
    return {
      build: {
        rollupOptions: {
          input: './src/client.ts',
          output: {
            dir: './dist/static',
            entryFileNames: 'client.js',
          },
        },
        copyPublicDir: false,
      },
    }
  } else {
    return {
      plugins: [build()],
    }
  }
})

The build command:

vite build --mode client && vite build

import.meta.env.PROD is helpful in detecting whether it is in development or production mode if you are using it on a Vite dev server.

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>Hello!</body>
    </html>
  )
})

Authors

License

MIT