Last active
February 2, 2025 06:33
-
-
Save reosablo/7e7e759e0cdc8eb01ed2a4da7ec17924 to your computer and use it in GitHub Desktop.
Vite plugin for Hono server on Google Cloud Run Functions
This file contains hidden or 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
| /** | |
| * @file Vite plugin for Hono server on Google Cloud Run Functions | |
| * @author reosablo | |
| * @license UNLICENSED | |
| */ | |
| import buildPlugin from "@hono/vite-build"; | |
| import type { Plugin } from "vite"; | |
| export type GoogleCloudRunFunctionsBuildPluginOptions = { | |
| /** | |
| * The entry point name of Google Cloud Run Functions. | |
| * | |
| * The name is used for `--entry-point` option of `gcloud functions deploy` | |
| * command and `--target` option of `functions-framework` command. | |
| * @default "function" | |
| */ | |
| functionsEntryPoint?: string; | |
| /** | |
| * The root directory to serve static files from. | |
| * @default "./" | |
| */ | |
| staticRoot?: string; | |
| } & Parameters<typeof buildPlugin>[0]; | |
| /** | |
| * A Vite plugin to build a Google Cloud Run Functions app. | |
| * @example | |
| * ```ts | |
| * // vite.config.ts for HonoX app | |
| * import adapter from "@hono/vite-dev-server/node"; | |
| * import honox from "honox/vite"; | |
| * import { defineConfig } from "vite"; | |
| * import build from "./vite-plugins/google-cloud-run-functions-build"; | |
| * | |
| * export default defineConfig({ | |
| * plugins: [ | |
| * honox({ devServer: { adapter } }), | |
| * build({ staticRoot: "dist" }), | |
| * ], | |
| * }); | |
| * ``` | |
| * | |
| * ```console | |
| * $ npm run build && npx functions-framework | |
| * ``` | |
| */ | |
| function googleCloudRunFunctionsBuildPlugin( | |
| options: GoogleCloudRunFunctionsBuildPluginOptions = {}, | |
| ): Plugin { | |
| const { staticRoot = "./", functionsEntryPoint = "function" } = options; | |
| return { | |
| ...buildPlugin({ | |
| entryContentBeforeHooks: [ | |
| (appName, options) => { | |
| const staticPaths = options?.staticPaths; | |
| if (staticPaths === undefined || staticPaths.length === 0) { | |
| return ""; | |
| } | |
| return ` | |
| import { serveStatic } from "@hono/node-server/serve-static"; | |
| for (const path of ${JSON.stringify(staticPaths)}) { | |
| ${appName}.use( | |
| path, | |
| serveStatic({ root: ${JSON.stringify(staticRoot)} }), | |
| ); | |
| } | |
| `; | |
| }, | |
| ], | |
| entryContentDefaultExportHook: (appName) => | |
| functionsEntryPoint === "default" | |
| ? ` | |
| import { getRequestListener } from "@hono/node-server"; | |
| export default getRequestListener(${appName}.fetch); | |
| ` | |
| : ` | |
| import { getRequestListener } from "@hono/node-server"; | |
| const ${appName}RequestListener = getRequestListener(${appName}.fetch); | |
| export { | |
| ${appName}RequestListener | |
| as ${JSON.stringify(functionsEntryPoint)}, | |
| }; | |
| `, | |
| ...options, | |
| }), | |
| name: "hono-vite-build-google-cloud-run-functions", | |
| }; | |
| } | |
| export default googleCloudRunFunctionsBuildPlugin; |
This file contains hidden or 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
| { | |
| "private": true, | |
| "author": "reosablo", | |
| "license": "UNLICENSED", | |
| "type": "module", | |
| "main": "dist/index.js", | |
| "engines": { | |
| "node": ">=22.11.0" | |
| }, | |
| "scripts": { | |
| "dev": "vite", | |
| "build": "vite build --mode client && vite build", | |
| "preview": "npm run build && functions-framework" | |
| }, | |
| "dependencies": { | |
| "@hono/node-server": "^1.13.8", | |
| "hono": "^4.6.20", | |
| "honox": "^0.1.33" | |
| }, | |
| "devDependencies": { | |
| "@google-cloud/functions-framework": "^3.4.5", | |
| "@hono/vite-build": "^1.3.0", | |
| "@hono/vite-dev-server": "^0.18.1", | |
| "vite": "^6.0.11" | |
| } | |
| } |
This file contains hidden or 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
| import adapter from "@hono/vite-dev-server/node"; | |
| import honox from "honox/vite"; | |
| import { defineConfig } from "vite"; | |
| import build from "./hono-vite-build-google-cloud-run-functions.ts"; | |
| export default defineConfig({ | |
| plugins: [ | |
| honox({ devServer: { adapter } }), | |
| build({ staticRoot: "dist" }), | |
| ], | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment