Skip to content

Commit

Permalink
Add docs and an e2e test for variables and bindings from wrangler.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiSpencer committed Feb 21, 2024
1 parent 828736f commit 66fb0d7
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 16 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,34 @@ You can also deploy it to Cloudflare Pages.
wrangler pages deploy ./dist
```

## Development

### Wrangler Bindings and Variables

If you use a `wrangler.toml` file to declare Bindings (Such as a D1 Database) and Variables , these can be used in your HonoX application during local development (with the `vite` command) with a small change to your vite.config.ts file.

Import the `getPlatformProxy` function from the `wrangler` package and use it to get the environment variables and dispose function. Then, pass the environment variables to your HonoX application devServer configuration.

```ts
// vite.config.ts
import { defineConfig } from 'vite'
import { getPlatformProxy } from 'wrangler'

export default defineConfig(async () => {
const { env, dispose } = await getPlatformProxy()
return {
plugins: [
honox({
devServer: {
env,
plugins: [{ onServerClose: dispose }],
},
}),
],
}
})
```

## Examples

- https://github.com/yusukebe/honox-examples
Expand All @@ -727,3 +755,4 @@ wrangler pages deploy ./dist
## License

MIT

Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vitest": "^1.2.1"
"vitest": "^1.2.1",
"wrangler":"^3.28.4"
},
"engines": {
"node": ">=18.14.1"
Expand Down
5 changes: 5 additions & 0 deletions test/hono-jsx/app/routes/env.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createRoute } from "../../../../src/factory";

export default createRoute((c) => {
return c.json({ env: c.env })
});
8 changes: 8 additions & 0 deletions test/hono-jsx/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ test('error-boundary failure', async ({ page }) => {
const div = await container.locator('div')
expect(await div.innerHTML()).toBe('<span class="error">Something went wrong</span>')
})

test('sets variables and bindings from wrangler.toml', async ({ page }) => {
const res = await page.goto('/env', { waitUntil: 'domcontentloaded' })
expect(res?.ok()).toBe(true)
const json = await res?.json()
expect(json).toBeTruthy()
expect(json.env).toStrictEqual({ DB: expect.any(Object), TEST_VARIABLE: 'TEST_VARIABLE_VALUE' })
})
2 changes: 2 additions & 0 deletions test/hono-jsx/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ describe('Basic', () => {
{ path: '/api', method: 'POST', handler: expect.any(Function) },
{ path: '/api', method: 'GET', handler: expect.any(Function) },
{ path: '/api', method: 'GET', handler: expect.any(Function) },
{ path: '/env', method: 'GET', handler: expect.any(Function) },
{ path: '/env', method: 'GET', handler: expect.any(Function) },
{ path: '/', method: 'GET', handler: expect.any(Function) },
{ path: '/', method: 'GET', handler: expect.any(Function) },
{
Expand Down
26 changes: 17 additions & 9 deletions test/hono-jsx/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import mdx from '@mdx-js/rollup'
import { defineConfig } from 'vite'
import { getPlatformProxy } from 'wrangler'
import honox from '../../src/vite'

export default defineConfig({
plugins: [
honox({
entry: './app/server.ts',
}),
mdx({
jsxImportSource: 'hono/jsx',
}),
],
export default defineConfig(async () => {
const { env, dispose } = await getPlatformProxy()
return {
plugins: [
honox({
entry: './app/server.ts',
devServer: {
env,
plugins: [{ onServerClose: dispose }],
},
}),
mdx({
jsxImportSource: 'hono/jsx',
}),
],
}
})
8 changes: 8 additions & 0 deletions test/hono-jsx/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[vars]
TEST_VARIABLE = "TEST_VARIABLE_VALUE"

[[d1_databases]]
binding = "DB"
database_name = "DB_NAME"
database_id = "DB_ID"

Loading

0 comments on commit 66fb0d7

Please sign in to comment.