Skip to content

Commit

Permalink
feat(dev-server): allow *Persist options (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Sep 18, 2023
1 parent 386e427 commit e563b6a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
43 changes: 40 additions & 3 deletions packages/dev-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ bunx --bun vite
The options are below. `WorkerOptions` imported from `miniflare` are used for Cloudflare Bindings.

```ts
import type { WorkerOptions } from 'miniflare'
import type { MiniflareOptions, WorkerOptions } from 'miniflare'

export type DevServerOptions = {
entry?: string
Expand All @@ -106,8 +106,13 @@ export type DevServerOptions = {
cf?: Partial<
Omit<
WorkerOptions,
// We can ignore these properties:
'name' | 'script' | 'scriptPath' | 'modules' | 'modulesRoot' | 'modulesRules'
>
> &
Pick<
MiniflareOptions,
'cachePersist' | 'd1Persist' | 'durableObjectsPersist' | 'kvPersist' | 'r2Persist'
>
>
}
```
Expand All @@ -130,6 +135,21 @@ If it's `true` and the response content-type is "HTML", inject the script that e

The paths which are not served by the dev-server.

If you have static files in `public/static/*` and want to return them, exclude `/static/*` as follows:

```ts
import devServer, { defaultOptions } from '@hono/vite-dev-server'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [
devServer({
exclude: ['^/static/.*', ...defaultOptions.exclude],
}),
],
})
```

## Cloudflare Bindings

You can use Cloudflare Bindings like variables, KV, D1, and others.
Expand All @@ -152,11 +172,28 @@ export default defineConfig({

These Bindings are emulated by Miniflare in the local.

### 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({
cf: {
d1Databases: ['DB'],
d1Persist: true,
},
}),
],
})
```

## Notes

### Depending on Miniflare

`@hono/vite-dev-server` depends on `miniflare` for certain platforms you may want to run on. For example, if you want to run your applications on Node.js, the `miniflare` is not needed. However, it's necessary for Cloudflare Workers/Pages, which are important platforms for Hono. And `miniflare` is needed just for development; it will not be bundled for production. We allow including `miniflare` in `@hono/vite-dev-server`.
`@hono/vite-dev-server` depends on `miniflare` for certain platforms you may want to run on. For example, if you want to run your applications on Node.js, the `miniflare` is not needed. However, it's necessary for Cloudflare Workers/Pages, which are important platforms for Hono. And `miniflare` is needed just for development; it will not be bundled for production.

### `cf` option with Bun

Expand Down
9 changes: 6 additions & 3 deletions packages/dev-server/src/dev-server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type http from 'http'
import { getRequestListener } from '@hono/node-server'
import type { Miniflare } from 'miniflare'
import type { WorkerOptions } from 'miniflare'
import type { Miniflare, MiniflareOptions, WorkerOptions } from 'miniflare'
import type { Plugin, ViteDevServer, Connect } from 'vite'

export type DevServerOptions = {
Expand All @@ -13,7 +12,11 @@ export type DevServerOptions = {
WorkerOptions,
// We can ignore these properties:
'name' | 'script' | 'scriptPath' | 'modules' | 'modulesRoot' | 'modulesRules'
>
> &
Pick<
MiniflareOptions,
'cachePersist' | 'd1Persist' | 'durableObjectsPersist' | 'kvPersist' | 'r2Persist'
>
>
}

Expand Down

0 comments on commit e563b6a

Please sign in to comment.