Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): Add netlify-functions adapter #218

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chadxz
Copy link

@chadxz chadxz commented Jan 23, 2025

Why?

So we can build a bundle targetting the netlify-functions runtime.

How?

  • Extend EntryContentOptions with entryContentDefaultExportHook to allow customizing the way the default export of the bundle is built.

  • Alter getEntryContent to use the entryContentDefaultExportHook for defining the default export, defaulting to the previous behavior if not defined.

  • Add netlifyFunctionsBuildPlugin as a new exported adapter that wraps the hono app in the hono/netlify handle() adapter and defines a config export to make the hono app respond to the root path in Netlify.

Example

// vite.config.ts
import { defineConfig } from "vite";
import devServer from "@hono/vite-dev-server";
import build from "@hono/vite-build/netlify-functions";

export default defineConfig({
  plugins: [
    devServer({
      entry: "./src/index.ts",
    }),
    build({
      entry: "./src/index.ts",
      output: "functions/server/index.js"
    })
  ],
});

I also have a public/publish directory that I put any assets that should be published to my Netlify site. After running a build, I end up with:

dist/
  functions/
    server/
      index.js
  publish/
    robots.txt
    ....

and my netlify.toml looks like:

# https://ntl.fyi/file-based-build-config
[build]
command = "vite build"
functions = "dist/functions"
publish = "dist/publish"

Copy link

changeset-bot bot commented Jan 23, 2025

🦋 Changeset detected

Latest commit: a780f59

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/vite-build Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chadxz chadxz force-pushed the build-netlify branch 2 times, most recently from cd134e5 to 576c27e Compare January 23, 2025 04:23
Why?
====

So we can build a bundle targetting the netlify-functions runtime.

How?
====

* Extend `EntryContentOptions` with `entryContentDefaultExportHook`
  to allow customizing the way the default export of the bundle is
  built.

* Alter `getEntryContent` to use the `entryContentDefaultExportHook`
  for defining the default export, defaulting to the previous
  behavior if not defined.

* Add `netlifyFunctionsBuildPlugin` as a new exported adapter that
  wraps the hono app in the `hono/netlify` `handle()` adapter and
  defines a `config` export to make the hono app respond to the
  root path in Netlify.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant