@hono/vite-dev-server
Advanced tools
Comparing version 0.7.1 to 0.8.0
@@ -14,2 +14,4 @@ const nullScript = "export default { fetch: () => new Response(null, { status: 404 }) };"; | ||
...await mf.getBindings(), | ||
...options.bindings, | ||
// Do not overwrite existing bindings. | ||
// `env.ASSETS.fetch()` function for Cloudflare Pages. | ||
@@ -16,0 +18,0 @@ ASSETS: { |
@@ -11,4 +11,43 @@ import { Plugin as Plugin$1 } from 'vite'; | ||
exclude?: (string | RegExp)[]; | ||
ignoreWatching?: (string | RegExp)[]; | ||
env?: Env | EnvFunc; | ||
plugins?: Plugin[]; | ||
/** | ||
* This can be used to inject environment variables into the worker from your wrangler.toml for example, | ||
* by making use of the helper function `getPlatformProxy` from `wrangler`. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* import { defineConfig } from 'vite' | ||
* import devServer from '@hono/vite-dev-server' | ||
* import getPlatformProxy from 'wrangler' | ||
* | ||
* export default defineConfig(async () => { | ||
* const { env, dispose } = await getPlatformProxy() | ||
* return { | ||
* plugins: [ | ||
* devServer({ | ||
* adapter: { | ||
* env, | ||
* onServerClose: dispose | ||
* }, | ||
* }), | ||
* ], | ||
* } | ||
* }) | ||
* ``` | ||
* | ||
* | ||
*/ | ||
adapter?: { | ||
/** | ||
* Environment variables to be injected into the worker | ||
*/ | ||
env?: Env; | ||
/** | ||
* Function called when the vite dev server is closed | ||
*/ | ||
onServerClose?: () => Promise<void>; | ||
}; | ||
} & { | ||
@@ -22,5 +61,5 @@ /** | ||
}; | ||
declare const defaultOptions: Required<Omit<DevServerOptions, 'env' | 'cf'>>; | ||
declare const defaultOptions: Required<Omit<DevServerOptions, 'env' | 'cf' | 'adapter'>>; | ||
declare function devServer(options?: DevServerOptions): Plugin$1; | ||
export { DevServerOptions, defaultOptions, devServer }; |
@@ -16,2 +16,3 @@ import { getRequestListener } from "@hono/node-server"; | ||
], | ||
ignoreWatching: [/\.wrangler/], | ||
plugins: [] | ||
@@ -54,16 +55,26 @@ }; | ||
if (typeof options.env === "function") { | ||
env = await options.env(); | ||
env = { ...env, ...await options.env() }; | ||
} else { | ||
env = options.env; | ||
env = { ...env, ...options.env }; | ||
} | ||
} else if (options?.cf) { | ||
env = await cloudflarePagesGetEnv(options.cf)(); | ||
} | ||
if (options?.cf) { | ||
env = { | ||
...env, | ||
...await cloudflarePagesGetEnv(options.cf)() | ||
}; | ||
} | ||
if (options?.plugins) { | ||
for (const plugin2 of options.plugins) { | ||
if (plugin2.env) { | ||
env = typeof plugin2.env === "function" ? await plugin2.env() : plugin2.env; | ||
env = { | ||
...env, | ||
...typeof plugin2.env === "function" ? await plugin2.env() : plugin2.env | ||
}; | ||
} | ||
} | ||
} | ||
if (options?.adapter?.env) { | ||
env = { ...env, ...options.adapter.env }; | ||
} | ||
const response = await app.fetch(request, env, { | ||
@@ -110,3 +121,15 @@ waitUntil: async (fn) => fn, | ||
} | ||
if (options?.adapter?.onServerClose) { | ||
await options.adapter.onServerClose(); | ||
} | ||
}); | ||
}, | ||
config: () => { | ||
return { | ||
server: { | ||
watch: { | ||
ignored: options?.ignoreWatching ?? defaultOptions.ignoreWatching | ||
} | ||
} | ||
}; | ||
} | ||
@@ -113,0 +136,0 @@ }; |
{ | ||
"name": "@hono/vite-dev-server", | ||
"description": "Vite dev-server plugin for Hono", | ||
"version": "0.7.1", | ||
"version": "0.8.0", | ||
"types": "dist/index.d.ts", | ||
@@ -67,3 +67,4 @@ "module": "dist/index.js", | ||
"vite": "^5.1.1", | ||
"vitest": "^0.34.6" | ||
"vitest": "^0.34.6", | ||
"wrangler": "^3.28.4" | ||
}, | ||
@@ -70,0 +71,0 @@ "peerDependencies": { |
@@ -105,4 +105,7 @@ # @hono/vite-dev-server | ||
exclude?: (string | RegExp)[] | ||
env?: Env | EnvFunc | ||
plugins?: Plugin[] | ||
ignoreWatching?: (string | RegExp)[] | ||
adapter?: { | ||
env?: Env | ||
onServerClose?: () => Promise<void> | ||
} | ||
} | ||
@@ -125,3 +128,3 @@ ``` | ||
], | ||
plugins: [], | ||
ignoreWatching: [/\.wrangler/], | ||
} | ||
@@ -153,57 +156,39 @@ ``` | ||
### `env` | ||
### `ignoreWatching` | ||
You can pass `ENV` variables to your application by setting the `env` option. | ||
`ENV` values can be accessed using `c.env` in your Hono application. | ||
You can add target directories for the server to watch. | ||
### `plugins` | ||
### `adapter` | ||
There are plugins for each platform to set up their own environment, etc. | ||
You can pass the `env` value of a specified environment to the application. | ||
## Plugins | ||
## Adapter | ||
### Cloudflare Pages | ||
### Cloudflare | ||
You can use Cloudflare Pages plugin, which allow you to access bindings such as variables, KV, D1, and others. | ||
You can pass the Bindings specified in `wrangler.toml` to your application by using Wrangler's `getPlatformProxy()` function. | ||
```ts | ||
import pages from '@hono/vite-dev-server/cloudflare-pages' | ||
import devServer from '@hono/vite-dev-server' | ||
import { defineConfig } from 'vite' | ||
import { getPlatformProxy } from 'wrangler' | ||
export default defineConfig({ | ||
plugins: [ | ||
devServer({ | ||
plugins: [ | ||
pages({ | ||
bindings: { | ||
NAME: 'Hono', | ||
}, | ||
kvNamespaces: ['MY_KV'], | ||
}), | ||
], | ||
}), | ||
], | ||
export default defineConfig(async () => { | ||
const { env, dispose } = await getPlatformProxy() | ||
return { | ||
plugins: [ | ||
devServer({ | ||
adapter: { | ||
env, | ||
onServerClose: dispose, | ||
}, | ||
}), | ||
], | ||
} | ||
}) | ||
``` | ||
These Bindings are emulated by Miniflare in the local. | ||
> [!NOTE] | ||
> The `wrangler.toml` is not used in the Cloudflate Pages production environment. Please configure Bindings from the dashboard. | ||
#### D1 | ||
When using D1, your app will read `.mf/d1/DB/db.sqlite` which is generated automatically with the following configuration: | ||
```ts | ||
export default defineConfig({ | ||
plugins: [ | ||
devServer({ | ||
plugins: [ | ||
pages({ | ||
d1Databases: ['DB'], | ||
d1Persist: true, | ||
}), | ||
], | ||
}), | ||
], | ||
}) | ||
``` | ||
## Client-side | ||
@@ -210,0 +195,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37533
30
789
10
271