-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): Add netlify-functions adapter
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.
- Loading branch information
Showing
10 changed files
with
152 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
'@hono/vite-build': minor | ||
--- | ||
|
||
Added a new Netlify Functions build adapter. | ||
|
||
This adapter can be imported from `@hono/vite-build/netlify-functions` and will | ||
compile your Hono app to comply with the requirements of the Netlify Functions | ||
runtime. | ||
|
||
* The default export will have the `hono/netlify` adapter applied to it. | ||
* A `config` object will be exported, setting the function path to `'/*'` and | ||
`preferStatic` to `true`. | ||
|
||
Please note, this is for the Netlify Functions runtime, not the Netlify Edge | ||
Functions runtime. | ||
|
||
Example: | ||
|
||
```ts | ||
// 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" | ||
}) | ||
], | ||
}); | ||
``` | ||
|
||
If you also have a `public/publish` directory for your assets that should be | ||
published to the corresponding Netlify site, then after running a build, you | ||
would end up with a directory structure like: | ||
|
||
``` | ||
dist/ | ||
functions/ | ||
server/ | ||
index.js | ||
publish/ | ||
robots.txt | ||
.... | ||
``` | ||
|
||
then you can use a netlify.toml that looks like: | ||
|
||
```toml | ||
# https://ntl.fyi/file-based-build-config | ||
[build] | ||
command = "vite build" | ||
functions = "dist/functions" | ||
publish = "dist/publish" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { Plugin } from 'vite' | ||
import type { BuildOptions } from '../../base.js' | ||
import buildPlugin from '../../base.js' | ||
|
||
export type NetlifyFunctionsBuildOptions = BuildOptions | ||
|
||
export default function netlifyFunctionsBuildPlugin( | ||
pluginOptions?: NetlifyFunctionsBuildOptions | ||
): Plugin { | ||
return { | ||
...buildPlugin({ | ||
...{ | ||
entryContentBeforeHooks: [() => 'import { handle } from "hono/netlify"'], | ||
entryContentAfterHooks: [() => 'export const config = { path: "/*", preferStatic: true }'], | ||
entryContentDefaultExportHook: (appName) => `export default handle(${appName})`, | ||
}, | ||
...pluginOptions, | ||
}), | ||
name: '@hono/vite-build/netlify-functions', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters