From e563b6a077413462b6bb1f9bd7b41284b34b37da Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Mon, 18 Sep 2023 23:16:10 +0900 Subject: [PATCH] feat(dev-server): allow `*Persist` options (#8) --- packages/dev-server/README.md | 43 +++++++++++++++++++++++++-- packages/dev-server/src/dev-server.ts | 9 ++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/packages/dev-server/README.md b/packages/dev-server/README.md index a006679..0d68dc3 100644 --- a/packages/dev-server/README.md +++ b/packages/dev-server/README.md @@ -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 @@ -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' + > > } ``` @@ -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. @@ -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 diff --git a/packages/dev-server/src/dev-server.ts b/packages/dev-server/src/dev-server.ts index 178a54a..2de1bc6 100644 --- a/packages/dev-server/src/dev-server.ts +++ b/packages/dev-server/src/dev-server.ts @@ -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 = { @@ -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' + > > }