@hono/vite-dev-server
Advanced tools
Comparing version 0.3.5 to 0.4.0
import { WorkerOptions, MiniflareOptions } from 'miniflare'; | ||
import { GetEnv } from '../types.js'; | ||
import { GetEnv, Plugin } from '../types.js'; | ||
@@ -8,3 +8,5 @@ type Options = Partial<Omit<WorkerOptions, 'name' | 'script' | 'scriptPath' | 'modules' | 'modulesRoot' | 'modulesRules'> & Pick<MiniflareOptions, 'cachePersist' | 'd1Persist' | 'durableObjectsPersist' | 'kvPersist' | 'r2Persist'> & { | ||
declare const getEnv: GetEnv<Options>; | ||
declare const disposeMf: () => Promise<void>; | ||
declare const plugin: (options?: Options) => Plugin; | ||
export { getEnv }; | ||
export { plugin as default, disposeMf, getEnv }; |
const nullScript = "export default { fetch: () => new Response(null, { status: 404 }) };"; | ||
let mf = void 0; | ||
const getEnv = (options) => async () => { | ||
const { Miniflare } = await import("miniflare"); | ||
const mf = new Miniflare({ | ||
mf = new Miniflare({ | ||
modules: true, | ||
@@ -25,4 +26,19 @@ script: nullScript, | ||
}; | ||
const disposeMf = async () => { | ||
mf?.dispose(); | ||
}; | ||
const plugin = (options) => { | ||
const env = getEnv(options ?? {}); | ||
return { | ||
env, | ||
onServerClose: async () => { | ||
await disposeMf(); | ||
} | ||
}; | ||
}; | ||
var cloudflare_pages_default = plugin; | ||
export { | ||
cloudflare_pages_default as default, | ||
disposeMf, | ||
getEnv | ||
}; |
@@ -1,8 +0,14 @@ | ||
import { getEnv } from "./cloudflare-pages.js"; | ||
import { getEnv, disposeMf } from "./cloudflare-pages.js"; | ||
describe("getEnv()", () => { | ||
const envFunc = getEnv({ | ||
bindings: { | ||
TOKEN: "MY TOKEN" | ||
} | ||
let envFunc; | ||
beforeEach(() => { | ||
envFunc = getEnv({ | ||
bindings: { | ||
TOKEN: "MY TOKEN" | ||
} | ||
}); | ||
}); | ||
afterEach(() => { | ||
disposeMf(); | ||
}); | ||
it("Should return the value for bindings", async () => { | ||
@@ -9,0 +15,0 @@ const env = await envFunc(); |
@@ -0,3 +1,8 @@ | ||
import plugin from './cloudflare-pages.js'; | ||
export { getEnv } from './cloudflare-pages.js'; | ||
import 'miniflare'; | ||
import '../types.js'; | ||
export { plugin as default }; |
import { getEnv } from "./cloudflare-pages.js"; | ||
import plugin from "./cloudflare-pages.js"; | ||
var cloudflare_pages_default = plugin; | ||
export { | ||
cloudflare_pages_default as default, | ||
getEnv | ||
}; |
@@ -1,4 +0,4 @@ | ||
import { Plugin } from 'vite'; | ||
import { Plugin as Plugin$1 } from 'vite'; | ||
import { getEnv } from './cloudflare-pages/cloudflare-pages.js'; | ||
import { Env, EnvFunc } from './types.js'; | ||
import { Env, EnvFunc, Plugin } from './types.js'; | ||
import 'miniflare'; | ||
@@ -11,2 +11,3 @@ | ||
env?: Env | EnvFunc; | ||
plugins?: Plugin[]; | ||
} & { | ||
@@ -21,4 +22,4 @@ /** | ||
declare const defaultOptions: Required<Omit<DevServerOptions, 'env' | 'cf'>>; | ||
declare function devServer(options?: DevServerOptions): Plugin; | ||
declare function devServer(options?: DevServerOptions): Plugin$1; | ||
export { DevServerOptions, defaultOptions, devServer }; |
@@ -14,3 +14,4 @@ import { getRequestListener } from "@hono/node-server"; | ||
/^\/node_modules\/.*/ | ||
] | ||
], | ||
plugins: [] | ||
}; | ||
@@ -57,2 +58,9 @@ function devServer(options) { | ||
} | ||
if (options?.plugins) { | ||
for (const plugin2 of options.plugins) { | ||
if (plugin2.env) { | ||
env = typeof plugin2.env === "function" ? await plugin2.env() : plugin2.env; | ||
} | ||
} | ||
} | ||
let response = await app.fetch(request, env, { | ||
@@ -78,2 +86,11 @@ waitUntil: async (fn) => fn, | ||
server.middlewares.use(await createMiddleware(server)); | ||
server.httpServer?.on("close", async () => { | ||
if (options?.plugins) { | ||
for (const plugin2 of options.plugins) { | ||
if (plugin2.onServerClose) { | ||
await plugin2.onServerClose(); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
@@ -80,0 +97,0 @@ }; |
@@ -9,3 +9,7 @@ type Env = Record<string, unknown> | Promise<Record<string, unknown>>; | ||
type Fetch = (request: Request, env: {}, executionContext: ExecutionContext) => Promise<Response>; | ||
interface Plugin { | ||
env?: EnvFunc; | ||
onServerClose?: () => void | Promise<void>; | ||
} | ||
export { Env, EnvFunc, ExecutionContext, Fetch, GetEnv }; | ||
export { Env, EnvFunc, ExecutionContext, Fetch, GetEnv, Plugin }; |
{ | ||
"name": "@hono/vite-dev-server", | ||
"description": "Vite dev-server plugin for Hono", | ||
"version": "0.3.5", | ||
"version": "0.4.0", | ||
"types": "dist/index.d.ts", | ||
@@ -61,3 +61,3 @@ "module": "dist/index.js", | ||
"glob": "^10.3.10", | ||
"hono": "^3.11.7", | ||
"hono": "^3.12.6", | ||
"playwright": "^1.39.0", | ||
@@ -67,3 +67,3 @@ "publint": "^0.2.5", | ||
"tsup": "^7.2.0", | ||
"vite": "^4.4.9", | ||
"vite": "^5.0.12", | ||
"vitest": "^0.34.6" | ||
@@ -79,5 +79,5 @@ }, | ||
"@hono/node-server": "^1.4.1", | ||
"miniflare": "^3.20231030.4", | ||
"miniflare": "^3.20231218.2", | ||
"minimatch": "^9.0.3" | ||
} | ||
} |
@@ -12,3 +12,3 @@ # @hono/vite-dev-server | ||
- HMR (Only for the Client-side. [Currently, Vite doesn't support HMR for SSR](https://github.com/vitejs/vite/issues/7887)). | ||
- Supporting Cloudflare Bindings. | ||
- Plugins are available, e.g., Cloudflare Pages. | ||
- Also runs on Bun. | ||
@@ -98,3 +98,3 @@ | ||
The options are below. `WorkerOptions` imported from `miniflare` are used for Cloudflare Bindings. | ||
The options are below. | ||
@@ -107,2 +107,3 @@ ```ts | ||
env?: Env | EnvFunc | ||
plugins?: Plugin[] | ||
} | ||
@@ -125,2 +126,3 @@ ``` | ||
], | ||
plugins: [], | ||
} | ||
@@ -157,12 +159,14 @@ ``` | ||
Presets are available to define the `ENV`. | ||
### `plugins` | ||
## `ENV` presets | ||
There are plugins for each platform to set up their own environment, etc. | ||
## Plugins | ||
### Cloudflare Pages | ||
You can use Cloudflare Pages `ENV` presets, which allow you to access bindings such as variables, KV, D1, and others. | ||
You can use Cloudflare Pages plugin, which allow you to access bindings such as variables, KV, D1, and others. | ||
```ts | ||
import { getEnv } from '@hono/vite-dev-server/cloudflare-pages' | ||
import pages from '@hono/vite-dev-server/cloudflare-pages' | ||
@@ -172,8 +176,10 @@ export default defineConfig({ | ||
devServer({ | ||
env: getEnv({ | ||
bindings: { | ||
NAME: 'Hono', | ||
}, | ||
kvNamespaces: ['MY_KV'], | ||
}), | ||
plugins: plugins: [ | ||
pages({ | ||
bindings: { | ||
NAME: 'Hono', | ||
}, | ||
kvNamespaces: ['MY_KV'], | ||
}), | ||
], | ||
}), | ||
@@ -194,6 +200,8 @@ ], | ||
devServer({ | ||
cf: { | ||
d1Databases: ['DB'], | ||
d1Persist: true, | ||
}, | ||
plugins: [ | ||
pages({ | ||
d1Databases: ['DB'], | ||
d1Persist: true, | ||
}), | ||
], | ||
}), | ||
@@ -216,9 +224,5 @@ ], | ||
{import.meta.env.PROD ? ( | ||
<> | ||
<script type='module' src='/static/client.js'></script> | ||
</> | ||
<script type='module' src='/static/client.js'></script> | ||
) : ( | ||
<> | ||
<script type='module' src='/src/client.ts'></script> | ||
</> | ||
<script type='module' src='/src/client.ts'></script> | ||
)} | ||
@@ -239,3 +243,2 @@ </head> | ||
import devServer from '@hono/vite-dev-server' | ||
import pages from '@hono/vite-cloudflare-pages' | ||
@@ -246,11 +249,8 @@ export default defineConfig(({ mode }) => { | ||
build: { | ||
lib: { | ||
entry: './src/client.ts', | ||
formats: ['es'], | ||
fileName: 'client', | ||
name: 'client', | ||
}, | ||
rollupOptions: { | ||
input: ['./app/client.ts'], | ||
output: { | ||
dir: './dist/static', | ||
entryFileNames: 'static/client.js', | ||
chunkFileNames: 'static/assets/[name]-[hash].js', | ||
assetFileNames: 'static/assets/[name].[ext]', | ||
}, | ||
@@ -264,6 +264,13 @@ }, | ||
return { | ||
build: { | ||
minify: true, | ||
rollupOptions: { | ||
output: { | ||
entryFileNames: '_worker.js', | ||
}, | ||
}, | ||
}, | ||
plugins: [ | ||
pages(), | ||
devServer({ | ||
entry: 'src/index.tsx', | ||
entry: './app/server.ts', | ||
}), | ||
@@ -270,0 +277,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
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
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
32547
655
285
Updatedminiflare@^3.20231218.2