@hono/vite-build 1.11.0 → 1.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/adapter/bun/{index.d.ts → index.d.mts} +6 -6
  2. package/dist/adapter/bun/index.mjs +47 -0
  3. package/dist/adapter/cloudflare-pages/{index.d.ts → index.d.mts} +5 -5
  4. package/dist/adapter/cloudflare-pages/index.mjs +41 -0
  5. package/dist/adapter/cloudflare-workers/{index.d.ts → index.d.mts} +6 -5
  6. package/dist/adapter/cloudflare-workers/index.mjs +33 -0
  7. package/dist/adapter/deno/index.d.mts +10 -0
  8. package/dist/adapter/deno/index.mjs +21 -0
  9. package/dist/adapter/netlify-functions/{index.d.ts → index.d.mts} +5 -5
  10. package/dist/adapter/netlify-functions/index.mjs +16 -0
  11. package/dist/adapter/node/index.d.mts +19 -0
  12. package/dist/adapter/node/index.mjs +37 -0
  13. package/dist/adapter/vercel/index.d.mts +22 -0
  14. package/dist/adapter/vercel/index.mjs +170 -0
  15. package/dist/adapter/vercel/types.d.mts +276 -0
  16. package/dist/adapter/vercel/types.mjs +1 -0
  17. package/dist/base.d.mts +30 -0
  18. package/dist/base.mjs +99 -0
  19. package/dist/entry/index.d.mts +27 -0
  20. package/dist/entry/index.mjs +63 -0
  21. package/dist/entry/{serve-static.d.ts → serve-static.d.mts} +5 -4
  22. package/dist/entry/serve-static.mjs +8 -0
  23. package/dist/index.d.mts +2 -0
  24. package/dist/index.mjs +5 -0
  25. package/package.json +21 -53
  26. package/dist/adapter/bun/index.js +0 -55
  27. package/dist/adapter/cloudflare-pages/index.js +0 -53
  28. package/dist/adapter/cloudflare-workers/index.js +0 -37
  29. package/dist/adapter/deno/index.d.ts +0 -10
  30. package/dist/adapter/deno/index.js +0 -28
  31. package/dist/adapter/netlify-functions/index.js +0 -18
  32. package/dist/adapter/node/index.d.ts +0 -19
  33. package/dist/adapter/node/index.js +0 -50
  34. package/dist/adapter/vercel/index.d.ts +0 -22
  35. package/dist/adapter/vercel/index.js +0 -213
  36. package/dist/adapter/vercel/types.d.ts +0 -368
  37. package/dist/adapter/vercel/types.js +0 -0
  38. package/dist/base.d.ts +0 -29
  39. package/dist/base.js +0 -112
  40. package/dist/entry/index.d.ts +0 -26
  41. package/dist/entry/index.js +0 -69
  42. package/dist/entry/serve-static.js +0 -11
  43. package/dist/index.d.ts +0 -8
  44. package/dist/index.js +0 -6
@@ -1,55 +0,0 @@
1
- import buildPlugin, { defaultOptions as baseDefaultOptions } from "../../base.js";
2
- import { serveStaticHook } from "../../entry/serve-static.js";
3
- const defaultOptions = {
4
- ...baseDefaultOptions,
5
- entryContentAfterHooks: [
6
- () => `
7
- let websocket
8
- for (const [, app] of Object.entries(modules)) {
9
- if (
10
- app &&
11
- typeof app === 'object' &&
12
- 'websocket' in app &&
13
- app.websocket !== undefined
14
- ) {
15
- if (websocket !== undefined) {
16
- throw new Error(
17
- \`Handler "websocket" is defined in multiple entry files. Please ensure each handler is defined only once.\`
18
- )
19
- }
20
- websocket = app.websocket
21
- }
22
- }
23
- `
24
- ],
25
- entryContentDefaultExportHook: (appName) => `export default websocket !== undefined ? { fetch: ${appName}.fetch.bind(${appName}), websocket } : ${appName}`
26
- };
27
- const bunBuildPlugin = (pluginOptions) => {
28
- return {
29
- ...buildPlugin({
30
- ssrTarget: "node",
31
- ...{
32
- entryContentBeforeHooks: [
33
- async (appName, options) => {
34
- let code = "import { serveStatic } from 'hono/bun'\n";
35
- code += serveStaticHook(appName, {
36
- filePaths: options?.staticPaths,
37
- root: pluginOptions?.staticRoot
38
- });
39
- return code;
40
- }
41
- ]
42
- },
43
- ...pluginOptions,
44
- external: ["bun", ...pluginOptions?.external ?? []],
45
- entryContentAfterHooks: pluginOptions?.entryContentAfterHooks ?? defaultOptions.entryContentAfterHooks,
46
- entryContentDefaultExportHook: pluginOptions?.entryContentDefaultExportHook ?? defaultOptions.entryContentDefaultExportHook
47
- }),
48
- name: "@hono/vite-build/bun"
49
- };
50
- };
51
- var bun_default = bunBuildPlugin;
52
- export {
53
- bun_default as default,
54
- defaultOptions
55
- };
@@ -1,53 +0,0 @@
1
- import { readdir, writeFile } from "node:fs/promises";
2
- import { resolve } from "node:path";
3
- import buildPlugin, { defaultOptions } from "../../base.js";
4
- const WORKER_JS_NAME = "_worker.js";
5
- const ROUTES_JSON_NAME = "_routes.json";
6
- const cloudflarePagesBuildPlugin = (pluginOptions) => {
7
- let config;
8
- const staticPaths = [];
9
- return {
10
- ...buildPlugin({
11
- ...pluginOptions,
12
- output: WORKER_JS_NAME
13
- }),
14
- configResolved: async (resolvedConfig) => {
15
- config = resolvedConfig;
16
- },
17
- writeBundle: async () => {
18
- const paths = await readdir(resolve(config.root, config.build.outDir), {
19
- withFileTypes: true
20
- });
21
- if (paths.some((p) => p.name === ROUTES_JSON_NAME)) {
22
- return;
23
- } else {
24
- paths.forEach((p) => {
25
- if (p.isDirectory()) {
26
- staticPaths.push(`/${p.name}/*`);
27
- } else {
28
- if (p.name === WORKER_JS_NAME) {
29
- return;
30
- }
31
- staticPaths.push(`/${p.name}`);
32
- }
33
- });
34
- const staticRoutes = {
35
- version: 1,
36
- include: ["/*"],
37
- exclude: staticPaths
38
- };
39
- const path = resolve(
40
- config.root,
41
- pluginOptions?.outputDir ?? defaultOptions.outputDir,
42
- "_routes.json"
43
- );
44
- await writeFile(path, JSON.stringify(staticRoutes));
45
- }
46
- },
47
- name: "@hono/vite-build/cloudflare-pages"
48
- };
49
- };
50
- var cloudflare_pages_default = cloudflarePagesBuildPlugin;
51
- export {
52
- cloudflare_pages_default as default
53
- };
@@ -1,37 +0,0 @@
1
- import buildPlugin, { defaultOptions as baseDefaultOptions } from "../../base.js";
2
- const defaultOptions = {
3
- ...baseDefaultOptions,
4
- entryContentAfterHooks: [
5
- () => `
6
- const merged = {}
7
- const definedHandlers = new Set()
8
- for (const [file, app] of Object.entries(modules)) {
9
- for (const [key, handler] of Object.entries(app)) {
10
- if (key !== 'fetch') {
11
- if (definedHandlers.has(key)) {
12
- throw new Error(\`Handler "\${key}" is defined in multiple entry files. Please ensure each handler (except fetch) is defined only once.\`);
13
- }
14
- definedHandlers.add(key)
15
- merged[key] = handler
16
- }
17
- }
18
- }
19
- `
20
- ],
21
- entryContentDefaultExportHook: (appName) => `export default { ...merged, fetch: ${appName}.fetch }`
22
- };
23
- const cloudflareWorkersBuildPlugin = (pluginOptions) => {
24
- return {
25
- ...buildPlugin({
26
- ...pluginOptions,
27
- entryContentAfterHooks: pluginOptions?.entryContentAfterHooks ?? defaultOptions.entryContentAfterHooks,
28
- entryContentDefaultExportHook: pluginOptions?.entryContentDefaultExportHook ?? defaultOptions.entryContentDefaultExportHook
29
- }),
30
- name: "@hono/vite-build/cloudflare-workers"
31
- };
32
- };
33
- var cloudflare_workers_default = cloudflareWorkersBuildPlugin;
34
- export {
35
- cloudflare_workers_default as default,
36
- defaultOptions
37
- };
@@ -1,10 +0,0 @@
1
- import { Plugin } from 'vite';
2
- import { BuildOptions } from '../../base.js';
3
- import '../../entry/index.js';
4
-
5
- type DenoBuildOptions = {
6
- staticRoot?: string | undefined;
7
- } & BuildOptions;
8
- declare const denoBuildPlugin: (pluginOptions?: DenoBuildOptions) => Plugin;
9
-
10
- export { DenoBuildOptions, denoBuildPlugin as default };
@@ -1,28 +0,0 @@
1
- import buildPlugin from "../../base.js";
2
- import { serveStaticHook } from "../../entry/serve-static.js";
3
- const denoBuildPlugin = (pluginOptions) => {
4
- return {
5
- ...buildPlugin({
6
- ...{
7
- entryContentBeforeHooks: [
8
- async (appName, options) => {
9
- const preset = pluginOptions?.preset ?? "hono";
10
- let code = `import { serveStatic } from '${preset}/deno'
11
- `;
12
- code += serveStaticHook(appName, {
13
- filePaths: options?.staticPaths,
14
- root: pluginOptions?.staticRoot
15
- });
16
- return code;
17
- }
18
- ]
19
- },
20
- ...pluginOptions
21
- }),
22
- name: "@hono/vite-build/deno"
23
- };
24
- };
25
- var deno_default = denoBuildPlugin;
26
- export {
27
- deno_default as default
28
- };
@@ -1,18 +0,0 @@
1
- import buildPlugin from "../../base.js";
2
- function netlifyFunctionsBuildPlugin(pluginOptions) {
3
- return {
4
- ...buildPlugin({
5
- ssrTarget: "node",
6
- ...{
7
- entryContentBeforeHooks: [() => 'import { handle } from "hono/netlify"'],
8
- entryContentAfterHooks: [() => 'export const config = { path: "/*", preferStatic: true }'],
9
- entryContentDefaultExportHook: (appName) => `export default handle(${appName})`
10
- },
11
- ...pluginOptions
12
- }),
13
- name: "@hono/vite-build/netlify-functions"
14
- };
15
- }
16
- export {
17
- netlifyFunctionsBuildPlugin as default
18
- };
@@ -1,19 +0,0 @@
1
- import { Plugin } from 'vite';
2
- import { BuildOptions } from '../../base.js';
3
- import '../../entry/index.js';
4
-
5
- type NodeBuildOptions = {
6
- staticRoot?: string | undefined;
7
- port?: number | undefined;
8
- /**
9
- * Enable graceful shutdown on SIGINT and SIGTERM signals.
10
- * Set to a number to specify the timeout in milliseconds before forcing shutdown.
11
- * Set to 0 to wait indefinitely for connections to close.
12
- * Leave undefined to disable graceful shutdown.
13
- * @default undefined
14
- */
15
- shutdownTimeoutMs?: number | undefined;
16
- } & BuildOptions;
17
- declare const nodeBuildPlugin: (pluginOptions?: NodeBuildOptions) => Plugin;
18
-
19
- export { NodeBuildOptions, nodeBuildPlugin as default };
@@ -1,50 +0,0 @@
1
- import buildPlugin from "../../base.js";
2
- import { serveStaticHook } from "../../entry/serve-static.js";
3
- const nodeBuildPlugin = (pluginOptions) => {
4
- const port = pluginOptions?.port ?? 3e3;
5
- const shutdownTimeoutMs = pluginOptions?.shutdownTimeoutMs;
6
- return {
7
- ...buildPlugin({
8
- ssrTarget: "node",
9
- ...{
10
- entryContentBeforeHooks: [
11
- async (appName, options) => {
12
- let code = "import { serveStatic } from '@hono/node-server/serve-static'\n";
13
- code += serveStaticHook(appName, {
14
- filePaths: options?.staticPaths,
15
- root: pluginOptions?.staticRoot
16
- });
17
- return code;
18
- }
19
- ],
20
- entryContentAfterHooks: [
21
- async (appName) => {
22
- let code = "import { serve } from '@hono/node-server'\n";
23
- if (shutdownTimeoutMs !== void 0) {
24
- code += `const server = serve({ fetch: ${appName}.fetch, port: ${port.toString()} })
25
- `;
26
- code += "const gracefulShutdown = () => {\n";
27
- code += " server.close(() => process.exit(0))\n";
28
- if (shutdownTimeoutMs > 0) {
29
- code += ` setTimeout(() => process.exit(1), ${shutdownTimeoutMs}).unref()
30
- `;
31
- }
32
- code += "}\n";
33
- code += "process.on('SIGINT', gracefulShutdown)\n";
34
- code += "process.on('SIGTERM', gracefulShutdown)";
35
- } else {
36
- code += `serve({ fetch: ${appName}.fetch, port: ${port.toString()} })`;
37
- }
38
- return code;
39
- }
40
- ]
41
- },
42
- ...pluginOptions
43
- }),
44
- name: "@hono/vite-build/node"
45
- };
46
- };
47
- var node_default = nodeBuildPlugin;
48
- export {
49
- node_default as default
50
- };
@@ -1,22 +0,0 @@
1
- import { Plugin } from 'vite';
2
- import { BuildOptions } from '../../base.js';
3
- import { VercelBuildConfigV3, VercelNodejsServerlessFunctionConfig } from './types.js';
4
- import '../../entry/index.js';
5
-
6
- type VercelSourceRoute = Extract<NonNullable<VercelBuildConfigV3['routes']>[number], {
7
- src: string;
8
- }>;
9
- type VercelRouteConfig = Array<Omit<VercelSourceRoute, 'dest'> & {
10
- dest?: string;
11
- }>;
12
- type VercelBuildOptions = {
13
- vercel?: {
14
- config?: VercelBuildConfigV3;
15
- function?: Partial<VercelNodejsServerlessFunctionConfig>;
16
- name?: string;
17
- routes?: VercelRouteConfig;
18
- };
19
- } & Omit<BuildOptions, 'output' | 'outputDir'>;
20
- declare const vercelBuildPlugin: (pluginOptions?: VercelBuildOptions) => Plugin;
21
-
22
- export { VercelBuildOptions, vercelBuildPlugin as default };
@@ -1,213 +0,0 @@
1
- import { builtinModules } from "module";
2
- import { existsSync, mkdirSync, readFileSync } from "node:fs";
3
- import { cp, writeFile } from "node:fs/promises";
4
- import { resolve } from "node:path";
5
- import { defaultOptions } from "../../base.js";
6
- import { getEntryContent } from "../../entry/index.js";
7
- const BUNDLE_NAME = "index.js";
8
- const DEFAULT_FUNCTION_NAME = "__hono";
9
- const VIRTUAL_ENTRY_PREFIX = "virtual:build-entry-module-vercel-";
10
- const functionEntryHooks = {
11
- entryContentAfterHooks: [
12
- // eslint-disable-next-line quotes
13
- () => "import { handle } from '@hono/node-server/vercel'"
14
- ],
15
- entryContentDefaultExportHook: (appName) => `export default handle(${appName})`
16
- };
17
- const configWriteQueues = /* @__PURE__ */ new Map();
18
- const writeJSON = (path, data) => {
19
- const dir = resolve(path, "..");
20
- if (!existsSync(dir)) {
21
- mkdirSync(dir, { recursive: true });
22
- }
23
- return writeFile(path, JSON.stringify(data));
24
- };
25
- const readJSON = (path) => {
26
- if (!existsSync(path)) {
27
- return;
28
- }
29
- return JSON.parse(readFileSync(path, "utf-8"));
30
- };
31
- const enqueueConfigWrite = async (key, writeTask) => {
32
- const previousTask = configWriteQueues.get(key);
33
- if (previousTask) {
34
- await previousTask;
35
- }
36
- const nextTask = writeTask();
37
- configWriteQueues.set(key, nextTask);
38
- try {
39
- await nextTask;
40
- } finally {
41
- if (configWriteQueues.get(key) === nextTask) {
42
- configWriteQueues.delete(key);
43
- }
44
- }
45
- };
46
- const getRuntimeVersion = () => {
47
- try {
48
- const systemNodeVersion = process.versions.node.split(".")[0];
49
- return `nodejs${Number(systemNodeVersion)}.x`;
50
- } catch {
51
- return "nodejs22.x";
52
- }
53
- };
54
- const escapeRouteSegment = (value) => {
55
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
56
- };
57
- const getDefaultRoutePattern = (functionName) => {
58
- return `^/${escapeRouteSegment(functionName)}(?:/.*)?$`;
59
- };
60
- const appendRouteIfMissing = (target, seen, route) => {
61
- const key = JSON.stringify(route);
62
- if (seen.has(key)) {
63
- return;
64
- }
65
- seen.add(key);
66
- target.push(route);
67
- };
68
- const getFunctionConfig = (config, functionConfig) => {
69
- return {
70
- ...functionConfig,
71
- runtime: getRuntimeVersion(),
72
- launcherType: "Nodejs",
73
- handler: BUNDLE_NAME,
74
- shouldAddHelpers: Boolean(functionConfig?.shouldAddHelpers),
75
- shouldAddSourcemapSupport: Boolean(config.build.sourcemap),
76
- supportsResponseStreaming: true
77
- };
78
- };
79
- const getRoutesForFunction = (functionName, configuredRoutes) => {
80
- if (configuredRoutes && configuredRoutes.length > 0) {
81
- return configuredRoutes;
82
- }
83
- if (functionName === DEFAULT_FUNCTION_NAME) {
84
- return [{ src: "/(.*)", dest: `/${DEFAULT_FUNCTION_NAME}` }];
85
- }
86
- return [{ src: getDefaultRoutePattern(functionName), dest: `/${functionName}` }];
87
- };
88
- const mergeVercelConfig = async (configPath, routesToAdd, configOverride, functionName) => {
89
- const existingConfig = readJSON(configPath);
90
- const mergedRoutes = [];
91
- const seenRoutes = /* @__PURE__ */ new Set();
92
- for (const route of existingConfig?.routes ?? []) {
93
- appendRouteIfMissing(mergedRoutes, seenRoutes, route);
94
- }
95
- for (const route of configOverride?.routes ?? []) {
96
- appendRouteIfMissing(mergedRoutes, seenRoutes, route);
97
- }
98
- appendRouteIfMissing(mergedRoutes, seenRoutes, { handle: "filesystem" });
99
- for (const route of routesToAdd) {
100
- appendRouteIfMissing(mergedRoutes, seenRoutes, {
101
- ...route,
102
- dest: route.dest ?? `/${functionName}`
103
- });
104
- }
105
- const buildConfig = {
106
- ...existingConfig,
107
- ...configOverride,
108
- version: 3,
109
- routes: mergedRoutes
110
- };
111
- await writeJSON(configPath, buildConfig);
112
- };
113
- const copyStaticFiles = async (publicDirPath, outputDir) => {
114
- if (!existsSync(publicDirPath)) {
115
- return;
116
- }
117
- try {
118
- await cp(publicDirPath, resolve(outputDir, "static"), {
119
- recursive: true,
120
- force: true
121
- });
122
- } catch (error) {
123
- if (error.code !== "EEXIST") {
124
- throw error;
125
- }
126
- }
127
- };
128
- const vercelBuildPlugin = (pluginOptions) => {
129
- let config;
130
- const functionName = pluginOptions?.vercel?.name ?? DEFAULT_FUNCTION_NAME;
131
- if (!functionName) {
132
- throw new Error("`vercel.name` is required and cannot be empty.");
133
- }
134
- const virtualEntryId = `${VIRTUAL_ENTRY_PREFIX}${functionName}`;
135
- const resolvedVirtualEntryId = `\0${virtualEntryId}`;
136
- return {
137
- name: "@hono/vite-build/vercel",
138
- apply: pluginOptions?.apply ?? defaultOptions.apply,
139
- resolveId(id) {
140
- if (id === virtualEntryId) {
141
- return resolvedVirtualEntryId;
142
- }
143
- },
144
- async load(id) {
145
- if (id !== resolvedVirtualEntryId) {
146
- return;
147
- }
148
- const entry = pluginOptions?.entry ?? defaultOptions.entry;
149
- return await getEntryContent({
150
- entry: Array.isArray(entry) ? entry : [entry],
151
- entryContentBeforeHooks: pluginOptions?.entryContentBeforeHooks,
152
- entryContentAfterHooks: pluginOptions?.entryContentAfterHooks ?? functionEntryHooks.entryContentAfterHooks,
153
- entryContentDefaultExportHook: pluginOptions?.entryContentDefaultExportHook ?? functionEntryHooks.entryContentDefaultExportHook,
154
- staticPaths: pluginOptions?.staticPaths,
155
- preset: pluginOptions?.preset
156
- });
157
- },
158
- configResolved: (resolvedConfig) => {
159
- config = resolvedConfig;
160
- },
161
- config: async () => {
162
- return {
163
- ssr: {
164
- external: pluginOptions?.external ?? defaultOptions.external,
165
- noExternal: true,
166
- target: "node"
167
- },
168
- build: {
169
- outDir: ".vercel/output",
170
- emptyOutDir: pluginOptions?.emptyOutDir ?? defaultOptions.emptyOutDir,
171
- minify: pluginOptions?.minify ?? defaultOptions.minify,
172
- ssr: true,
173
- rollupOptions: {
174
- external: [...builtinModules, /^node:/],
175
- input: {
176
- [functionName]: virtualEntryId
177
- },
178
- output: {
179
- entryFileNames: `functions/[name].func/${BUNDLE_NAME}`
180
- }
181
- }
182
- }
183
- };
184
- },
185
- writeBundle: async () => {
186
- const outputDir = resolve(config.root, config.build.outDir);
187
- const functionDir = resolve(outputDir, "functions", `${functionName}.func`);
188
- const configPath = resolve(outputDir, "config.json");
189
- const publicDirPath = resolve(config.root, config.publicDir);
190
- const routesToAdd = getRoutesForFunction(functionName, pluginOptions?.vercel?.routes);
191
- const functionConfig = getFunctionConfig(config, pluginOptions?.vercel?.function);
192
- await copyStaticFiles(publicDirPath, outputDir);
193
- await Promise.all([
194
- writeJSON(resolve(functionDir, ".vc-config.json"), functionConfig),
195
- writeJSON(resolve(functionDir, "package.json"), {
196
- type: "module"
197
- })
198
- ]);
199
- await enqueueConfigWrite(configPath, async () => {
200
- await mergeVercelConfig(
201
- configPath,
202
- routesToAdd,
203
- pluginOptions?.vercel?.config,
204
- functionName
205
- );
206
- });
207
- }
208
- };
209
- };
210
- var vercel_default = vercelBuildPlugin;
211
- export {
212
- vercel_default as default
213
- };