Repository navigation
[autoconfig] Set up Hono projects for cf with Vite #17246
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
Merged
edmundhung
merged 10 commits into
autoconfig-setup-after-migration
from
autoconfig-hono-cf
Nov 30, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a88715b
[autoconfig] Set up Hono projects for cf
edmundhung 05cd5d2
[autoconfig] Mark Hono as supported on cf only, from Hono 4
edmundhung 8872c5b
[autoconfig] Set up Hono projects to build with Vite
edmundhung 8b0b35c
[autoconfig] Keep Wrangler's Vite config name and reword the HonoX re…
edmundhung c629066
[autoconfig] Only say that cf can't set up HonoX yet
edmundhung edf4229
[autoconfig] Drop the ES module Vite config name
edmundhung 0853327
[shared-ast-primitives] Parse JSX in .jsx files
edmundhung 6ea3708
[autoconfig] Refuse a Hono build that combines several entry files
edmundhung 8116354
[autoconfig] Refuse any Hono build given several entries
edmundhung 6e14cd5
[autoconfig] Leave Vite's install to setup and pass the target for Hono
edmundhung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@cloudflare/autoconfig": minor | ||
| --- | ||
|
|
||
| Set up Hono projects for cf | ||
|
|
||
| Previously, autoconfig refused every Hono project on cf. It now sets them up to build with Vite and the Cloudflare plugin, as in Hono's Workers + Vite starter: it adds Vite when the project doesn't use it, replaces Hono's own Vite dev server and build plugins, and uses the app's entry file as the Worker entry point. An entry file that doesn't export the app, such as one that starts a Node.js server, and HonoX projects are refused with a message. The Wrangler target is unchanged. |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| import { existsSync } from "node:fs"; | ||
| import { dirname, posix, relative, resolve, sep } from "node:path"; | ||
| import { parseFile } from "@cloudflare/shared-ast-primitives"; | ||
| import * as recast from "recast"; | ||
| import { AutoConfigFrameworkConfigurationError } from "../errors"; | ||
| import { Framework } from "./framework-class"; | ||
| import { readPackageJson } from "./utils/packages"; | ||
| import { | ||
| assertViteConfigCanBeTransformed, | ||
| getStaticPropertyName, | ||
| getViteConfigPath, | ||
| hasViteConfig, | ||
| transformViteConfig, | ||
| } from "./utils/vite-config"; | ||
| import { installCloudflareVitePlugin } from "./utils/vite-plugin"; | ||
| import { CF_VITE_PLUGIN_VERSION_RANGE } from "./utils/vite-plugin-version"; | ||
| import { createViteConfig } from "./vite"; | ||
| import type { WranglerConfig } from "../types"; | ||
| import type { | ||
| ConfigurationOptions, | ||
| ConfigurationResults, | ||
| } from "./framework-class"; | ||
|
|
||
| const t = recast.types.namedTypes; | ||
|
|
||
| /** The app's entry files that Hono's own Vite plugins use when none is configured */ | ||
| const DEFAULT_ENTRIES = ["src/index.ts", "src/index.tsx"]; | ||
|
|
||
| /** | ||
| * Sets a Hono app up to build with Vite and the Cloudflare plugin, as in Hono's Workers + Vite | ||
| * starter. Hono's own dev server and build plugins are replaced, since the Cloudflare plugin | ||
| * runs and builds the app itself. | ||
| */ | ||
| export class Hono extends Framework { | ||
| async configure({ | ||
|
edmundhung marked this conversation as resolved.
Resolved
|
||
| dryRun, | ||
| projectPath, | ||
| packageManager, | ||
| isWorkspaceRoot, | ||
| target, | ||
| wranglerConfig, | ||
| }: ConfigurationOptions): Promise<ConfigurationResults> { | ||
| const packageJson = readPackageJson(projectPath); | ||
| const dependencies = { | ||
| ...packageJson?.dependencies, | ||
| ...packageJson?.devDependencies, | ||
| }; | ||
| if ("honox" in dependencies) { | ||
| throw new AutoConfigFrameworkConfigurationError( | ||
| "cf can't set up HonoX projects automatically yet.", | ||
| { telemetryMessage: "autoconfig honox setup unsupported" } | ||
| ); | ||
| } | ||
|
|
||
| const viteConfigExists = hasViteConfig(projectPath); | ||
| if (viteConfigExists) { | ||
| assertViteConfigCanBeTransformed(projectPath); | ||
| } | ||
| const honoVitePlugins = viteConfigExists | ||
| ? readHonoVitePlugins(projectPath) | ||
| : { names: [], buildEntries: [], devServerEntries: [] }; | ||
| // Hono's build plugin combines the handlers of every entry file into one Worker, which a | ||
| // single entry point can't do | ||
| if (honoVitePlugins.buildEntries.length > 1) { | ||
| throw new AutoConfigFrameworkConfigurationError( | ||
| `cf can't set up a Hono build that combines several entry files automatically yet: ${honoVitePlugins.buildEntries | ||
| .map((file) => `\`${file}\``) | ||
| .join(", ")}.`, | ||
| { telemetryMessage: "autoconfig hono multiple build entries" } | ||
| ); | ||
| } | ||
| const entrypoint = findEntrypoint(projectPath, wranglerConfig, [ | ||
| ...honoVitePlugins.buildEntries, | ||
| ...honoVitePlugins.devServerEntries, | ||
| ]); | ||
|
|
||
| if (!dryRun) { | ||
| await installCloudflareVitePlugin({ | ||
| packageManager: packageManager.type, | ||
| isWorkspaceRoot, | ||
| projectPath, | ||
| version: CF_VITE_PLUGIN_VERSION_RANGE, | ||
| }); | ||
|
|
||
| if (viteConfigExists) { | ||
| transformViteConfig(projectPath, { | ||
| target, | ||
| incompatibleVitePlugins: honoVitePlugins.names, | ||
| }); | ||
|
edmundhung marked this conversation as resolved.
Resolved
edmundhung marked this conversation as resolved.
Resolved
|
||
| } else { | ||
| createViteConfig(projectPath, target); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| buildTool: "vite", | ||
| workerConfig: { entrypoint: `./${entrypoint}` }, | ||
| }; | ||
|
edmundhung marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Reads the plugins from Hono's `@hono/vite-dev-server` and `@hono/vite-build` packages in the | ||
| * project's Vite config. | ||
| * | ||
| * @param projectPath The project's path | ||
| * @returns The plugins' local names, and the app entries given to each | ||
| */ | ||
| function readHonoVitePlugins(projectPath: string): { | ||
| names: string[]; | ||
| buildEntries: string[]; | ||
| devServerEntries: string[]; | ||
| } { | ||
| const ast = parseFile(getViteConfigPath(projectPath)); | ||
| const devServerNames = new Set<string>(); | ||
| const buildNames = new Set<string>(); | ||
| const devServerEntries: string[] = []; | ||
| const buildEntries: string[] = []; | ||
| if (!ast) { | ||
| return { names: [], buildEntries: [], devServerEntries: [] }; | ||
| } | ||
|
|
||
| for (const statement of ast.body) { | ||
| if (!t.ImportDeclaration.check(statement)) { | ||
| continue; | ||
| } | ||
| const source = String(statement.source.value); | ||
| const names = | ||
| source === "@hono/vite-dev-server" | ||
|
edmundhung marked this conversation as resolved.
Resolved
|
||
| ? devServerNames | ||
| : source === "@hono/vite-build" || | ||
| source.startsWith("@hono/vite-build/") | ||
| ? buildNames | ||
| : undefined; | ||
| for (const specifier of statement.specifiers ?? []) { | ||
| if ( | ||
| names && | ||
| t.ImportDefaultSpecifier.check(specifier) && | ||
| specifier.local | ||
| ) { | ||
| names.add(String(specifier.local.name)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| recast.visit(ast, { | ||
| visitCallExpression(path) { | ||
| const { callee, arguments: [options] = [] } = path.node; | ||
| const entries = !t.Identifier.check(callee) | ||
| ? undefined | ||
| : devServerNames.has(callee.name) | ||
| ? devServerEntries | ||
| : buildNames.has(callee.name) | ||
| ? buildEntries | ||
| : undefined; | ||
| if (entries && t.ObjectExpression.check(options)) { | ||
| const entry = options.properties.find( | ||
| (property) => getStaticPropertyName(property) === "entry" | ||
| ); | ||
| if (t.Property.check(entry) || t.ObjectProperty.check(entry)) { | ||
| const values = t.ArrayExpression.check(entry.value) | ||
| ? entry.value.elements | ||
| : [entry.value]; | ||
| for (const value of values) { | ||
| if ( | ||
| (t.StringLiteral.check(value) || t.Literal.check(value)) && | ||
| typeof value.value === "string" | ||
| ) { | ||
| entries.push(value.value); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| this.traverse(path); | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| names: [...devServerNames, ...buildNames], | ||
| buildEntries, | ||
| devServerEntries, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Finds the file that exports the Hono app: the Worker entry point in the project's Wrangler | ||
| * config, else the entry given to Hono's Vite plugins, else one of their default entries. | ||
| * | ||
| * @param projectPath The project's path | ||
| * @param wranglerConfig The project's Wrangler config, if any | ||
| * @param vitePluginEntries The entries given to Hono's Vite plugins | ||
| * @returns The entry file, relative to the project | ||
| * @throws {AutoConfigFrameworkConfigurationError} If there is no such file, or it doesn't export the app | ||
| */ | ||
| function findEntrypoint( | ||
| projectPath: string, | ||
| wranglerConfig: WranglerConfig | undefined, | ||
| vitePluginEntries: string[] | ||
| ): string { | ||
| const wranglerEntries = | ||
| wranglerConfig?.main === undefined | ||
| ? [] | ||
| : [ | ||
| resolve( | ||
| wranglerConfig.configPath | ||
| ? dirname(wranglerConfig.configPath) | ||
| : projectPath, | ||
| wranglerConfig.main | ||
| ), | ||
| ]; | ||
| const entry = [...wranglerEntries, ...vitePluginEntries, ...DEFAULT_ENTRIES] | ||
| .map((file) => resolve(projectPath, file)) | ||
| .find((file) => existsSync(file)); | ||
|
edmundhung marked this conversation as resolved.
Resolved
|
||
| if (entry === undefined) { | ||
| throw new AutoConfigFrameworkConfigurationError( | ||
| `Could not find the Hono app's entry file. cf looks for the \`entry\` given to Hono's Vite plugins, or else ${DEFAULT_ENTRIES.map((file) => `\`${file}\``).join(" or ")}.`, | ||
| { telemetryMessage: "autoconfig hono entry missing" } | ||
| ); | ||
| } | ||
|
|
||
| const entrypoint = relative(projectPath, entry).split(sep).join(posix.sep); | ||
| if (!hasDefaultExport(entry)) { | ||
| throw new AutoConfigFrameworkConfigurationError( | ||
| `\`${entrypoint}\` doesn't export the Hono app, so it can't run as a Worker. Export the app with \`export default app\`, and start any Node.js server from a separate file.`, | ||
| { telemetryMessage: "autoconfig hono entry without default export" } | ||
| ); | ||
| } | ||
|
|
||
| return entrypoint; | ||
| } | ||
|
|
||
| function hasDefaultExport(file: string): boolean { | ||
| return ( | ||
| parseFile(file)?.body.some( | ||
| (statement) => | ||
| t.ExportDefaultDeclaration.check(statement) || | ||
| (t.ExportNamedDeclaration.check(statement) && | ||
| (statement.specifiers ?? []).some( | ||
| (specifier) => | ||
| t.Identifier.check(specifier.exported) && | ||
| specifier.exported.name === "default" | ||
| )) | ||
| ) ?? false | ||
| ); | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,23 @@ | ||
| import { join } from "node:path"; | ||
| import { parsePackageJSON, readFileSync } from "@cloudflare/workers-utils"; | ||
| import type { PackageJSON } from "@cloudflare/workers-utils"; | ||
|
|
||
| export { | ||
| isPackageInstalled, | ||
| getInstalledPackageVersion, | ||
| } from "@cloudflare/workers-utils"; | ||
|
|
||
| /** | ||
| * Reads a project's `package.json`. | ||
| * | ||
| * @param projectPath The project's path | ||
| * @returns The project's `package.json`, or undefined if it has none | ||
| */ | ||
| export function readPackageJson(projectPath: string): PackageJSON | undefined { | ||
| const path = join(projectPath, "package.json"); | ||
| try { | ||
| return parsePackageJSON(readFileSync(path), path); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.