-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dev-server): introduce
env
(#30)
* feat(dev-server): introduce `env` * add `deprecated` comment * changeset
- Loading branch information
Showing
16 changed files
with
676 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hono/vite-dev-server': minor | ||
--- | ||
|
||
feat: introduce `env` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
packages/dev-server/test/vite.config.ts → packages/dev-server/e2e/vite.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import { defineConfig } from 'vite' | ||
import devServer, { defaultOptions } from '../src' | ||
import { getEnv } from '../src/cloudflare-pages' | ||
|
||
export default defineConfig({ | ||
plugins: [ | ||
devServer({ | ||
entry: './mock/worker.ts', | ||
exclude: [...defaultOptions.exclude, '/app/.*'], | ||
cf: { | ||
env: getEnv({ | ||
bindings: { | ||
NAME: 'Hono', | ||
}, | ||
assets: true, | ||
}, | ||
}), | ||
}), | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/dev-server/src/cloudflare-pages/cloudflare-pages.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getEnv } from './cloudflare-pages.js' | ||
|
||
describe('getEnv()', () => { | ||
const envFunc = getEnv({ | ||
bindings: { | ||
TOKEN: 'MY TOKEN', | ||
}, | ||
}) | ||
it('Should return the value for bindings', async () => { | ||
const env = await envFunc() | ||
expect(env['TOKEN']).toBe('MY TOKEN') | ||
}) | ||
it('Should contains ASSETS', async () => { | ||
const env = await envFunc() | ||
expect((env['ASSETS'] as { fetch: typeof fetch }).fetch).toBeDefined() | ||
}) | ||
}) |
45 changes: 45 additions & 0 deletions
45
packages/dev-server/src/cloudflare-pages/cloudflare-pages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { MiniflareOptions, WorkerOptions } from 'miniflare' | ||
import type { GetEnv } from '../types.js' | ||
|
||
type Options = Partial< | ||
Omit< | ||
WorkerOptions, | ||
// We can ignore these properties: | ||
'name' | 'script' | 'scriptPath' | 'modules' | 'modulesRoot' | 'modulesRules' | ||
> & | ||
Pick< | ||
MiniflareOptions, | ||
'cachePersist' | 'd1Persist' | 'durableObjectsPersist' | 'kvPersist' | 'r2Persist' | ||
> & { | ||
// Enable `env.ASSETS.fetch()` function for Cloudflare Pages. | ||
assets?: boolean | ||
} | ||
> | ||
|
||
const nullScript = 'export default { fetch: () => new Response(null, { status: 404 }) };' | ||
|
||
export const getEnv: GetEnv<Options> = (options) => async () => { | ||
// Dynamic import Miniflare for environments like Bun. | ||
const { Miniflare } = await import('miniflare') | ||
const mf = new Miniflare({ | ||
modules: true, | ||
script: nullScript, | ||
...options, | ||
}) | ||
|
||
const env = { | ||
...(await mf.getBindings()), | ||
// `env.ASSETS.fetch()` function for Cloudflare Pages. | ||
ASSETS: { | ||
async fetch(input: RequestInfo | URL, init?: RequestInit | undefined) { | ||
try { | ||
return await fetch(new Request(input, init)) | ||
} catch (e) { | ||
console.error('Failed to execute ASSETS.fetch: ', e) | ||
return new Response(null, { status: 500 }) | ||
} | ||
}, | ||
}, | ||
} | ||
return env | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { getEnv } from './cloudflare-pages.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export type Env = Record<string, unknown> | Promise<Record<string, unknown>> | ||
export type EnvFunc = () => Env | Promise<Env> | ||
export type GetEnv<Options> = (options: Options) => EnvFunc | ||
export interface ExecutionContext { | ||
waitUntil(promise: Promise<unknown>): void | ||
passThroughOnException(): void | ||
} | ||
export type Fetch = ( | ||
request: Request, | ||
env: {}, | ||
executionContext: ExecutionContext | ||
) => Promise<Response> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
"module": "ES2022", | ||
"target": "ES2022", | ||
"types": [ | ||
"vitest/globals", | ||
"vite/client" | ||
] | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineConfig, configDefaults } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
globals: true, | ||
exclude: [...configDefaults.exclude, 'e2e'], | ||
}, | ||
}) |
Oops, something went wrong.