@hono/vite-dev-server
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,3 +0,5 @@ | ||
import { WorkerOptions, MiniflareOptions } from 'miniflare'; | ||
import { Plugin } from 'vite'; | ||
import { getEnv } from './cloudflare-pages/cloudflare-pages.js'; | ||
import { Env, EnvFunc } from './types.js'; | ||
import 'miniflare'; | ||
@@ -8,9 +10,14 @@ type DevServerOptions = { | ||
exclude?: (string | RegExp)[]; | ||
cf?: Partial<Omit<WorkerOptions, 'name' | 'script' | 'scriptPath' | 'modules' | 'modulesRoot' | 'modulesRules'> & Pick<MiniflareOptions, 'cachePersist' | 'd1Persist' | 'durableObjectsPersist' | 'kvPersist' | 'r2Persist'> & { | ||
assets?: boolean; | ||
}>; | ||
env?: Env | EnvFunc; | ||
} & { | ||
/** | ||
* @deprecated | ||
* The `cf` option is maintained for backward compatibility, but it will be obsolete in the future. | ||
* Instead, use the `env` option. | ||
*/ | ||
cf?: Parameters<typeof getEnv>[0]; | ||
}; | ||
declare const defaultOptions: Required<Omit<DevServerOptions, 'cf'>>; | ||
declare const defaultOptions: Required<Omit<DevServerOptions, 'env' | 'cf'>>; | ||
declare function devServer(options?: DevServerOptions): Plugin; | ||
export { DevServerOptions, defaultOptions, devServer }; |
import { getRequestListener } from "@hono/node-server"; | ||
import { minimatch } from "minimatch"; | ||
import { getEnv as cloudflarePagesGetEnv } from "./cloudflare-pages/index.js"; | ||
const defaultOptions = { | ||
@@ -14,3 +16,2 @@ entry: "./src/index.ts", | ||
}; | ||
const nullScript = "export default { fetch: () => new Response(null, { status: 404 }) };"; | ||
function devServer(options) { | ||
@@ -21,11 +22,2 @@ const entry = options?.entry ?? defaultOptions.entry; | ||
configureServer: async (server) => { | ||
let mf = void 0; | ||
if (options?.cf) { | ||
const { Miniflare } = await import("miniflare"); | ||
mf = new Miniflare({ | ||
modules: true, | ||
script: nullScript, | ||
...options.cf | ||
}); | ||
} | ||
async function createMiddleware(server2) { | ||
@@ -35,5 +27,10 @@ return async function(req, res, next) { | ||
for (const pattern of exclude) { | ||
const regExp = pattern instanceof RegExp ? pattern : new RegExp(`^${pattern}$`); | ||
if (req.url && regExp.test(req.url)) { | ||
return next(); | ||
if (req.url) { | ||
if (pattern instanceof RegExp) { | ||
if (pattern.test(req.url)) { | ||
return next(); | ||
} | ||
} else if (minimatch(req.url?.toString(), pattern)) { | ||
return next(); | ||
} | ||
} | ||
@@ -49,20 +46,10 @@ } | ||
let env = {}; | ||
if (mf) { | ||
env = await mf.getBindings(); | ||
if (options?.cf?.assets) { | ||
env = { | ||
// `env.ASSETS.fetch()` function for Cloudflare Pages. | ||
ASSETS: { | ||
async fetch(input, init) { | ||
try { | ||
return await fetch(new Request(input, init)); | ||
} catch (e) { | ||
console.error("Failed to execute ASSETS.fetch: ", e); | ||
return new Response(null, { status: 500 }); | ||
} | ||
} | ||
}, | ||
...env | ||
}; | ||
if (options?.env) { | ||
if (typeof options.env === "function") { | ||
env = await options.env(); | ||
} else { | ||
env = options.env; | ||
} | ||
} else if (options?.cf) { | ||
env = await cloudflarePagesGetEnv(options.cf)(); | ||
} | ||
@@ -69,0 +56,0 @@ const response = await app.fetch(request, env, { |
import { devServer } from './dev-server.js'; | ||
export { DevServerOptions, defaultOptions } from './dev-server.js'; | ||
import 'vite'; | ||
import './cloudflare-pages/cloudflare-pages.js'; | ||
import 'miniflare'; | ||
import 'vite'; | ||
import './types.js'; | ||
@@ -6,0 +8,0 @@ |
{ | ||
"name": "@hono/vite-dev-server", | ||
"description": "Vite dev-server plugin for Hono", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"types": "dist/index.d.ts", | ||
@@ -9,7 +9,9 @@ "module": "dist/index.js", | ||
"scripts": { | ||
"test": "playwright test -c test/playwright.config.ts test/e2e.test.ts", | ||
"build": "rimraf dist && tsup && publint", | ||
"test:unit": "vitest --run", | ||
"test:e2e": "playwright test -c e2e/playwright.config.ts e2e/e2e.test.ts", | ||
"test": "yarn test:unit && yarn test:e2e", | ||
"build": "rimraf dist && tsup --format esm,cjs --dts && publint", | ||
"watch": "tsup --watch", | ||
"prepublishOnly": "yarn build && yarn test", | ||
"release": "bumpp --tag \"@hono/vite-dev-server@v%s\" --commit \"chore(dev-server): release v%s\" && npm publish" | ||
"prerelease": "yarn build && yarn test", | ||
"release": "yarn publish" | ||
}, | ||
@@ -21,4 +23,15 @@ "files": [ | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js" | ||
"require": { | ||
"types": "./dist/index.d.cts", | ||
"default": "./dist/index.cjs" | ||
}, | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"./cloudflare-pages": { | ||
"types": "./dist/cloudflare-pages/index.d.ts", | ||
"require": "./dist/cloudflare-pages/index.cjs", | ||
"import": "./dist/cloudflare-pages/index.js" | ||
} | ||
@@ -30,2 +43,5 @@ }, | ||
"./dist/types" | ||
], | ||
"cloudflare-pages": [ | ||
"./dist/cloudflare-pages/index.d.ts" | ||
] | ||
@@ -47,11 +63,9 @@ } | ||
"@playwright/test": "^1.37.1", | ||
"bumpp": "^9.2.0", | ||
"glob": "^10.3.4", | ||
"hono": "^3.5.8", | ||
"playwright": "^1.39.0", | ||
"publint": "^0.1.12", | ||
"publint": "^0.2.5", | ||
"rimraf": "^5.0.1", | ||
"tsup": "^7.2.0", | ||
"vite": "^4.4.9", | ||
"vitest": "^0.31.4" | ||
"vitest": "^0.34.6" | ||
}, | ||
@@ -63,4 +77,5 @@ "engines": { | ||
"@hono/node-server": "^1.2.0", | ||
"miniflare": "^3.20231016.0" | ||
"miniflare": "^3.20231016.0", | ||
"minimatch": "^9.0.3" | ||
} | ||
} |
@@ -100,4 +100,2 @@ # @hono/vite-dev-server | ||
```ts | ||
import type { MiniflareOptions, WorkerOptions } from 'miniflare' | ||
export type DevServerOptions = { | ||
@@ -107,16 +105,3 @@ entry?: string | ||
exclude?: (string | RegExp)[] | ||
cf?: 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 | ||
} | ||
> | ||
env?: Env | EnvFunc | ||
} | ||
@@ -165,12 +150,22 @@ ``` | ||
## Cloudflare Bindings | ||
### `env` | ||
You can use Cloudflare Bindings like variables, KV, D1, and others. | ||
You can pass `ENV` variables to your application by setting the `env` option. | ||
`ENV` values can be accessed using `c.env` in your Hono application. | ||
Presets are available to define the `ENV`. | ||
## `ENV` presets | ||
### Cloudflare Pages | ||
You can use Cloudflare Pages `ENV` presets, which allow you to access bindings such as variables, KV, D1, and others. | ||
```ts | ||
import { getEnv } from '@hono/vite-dev-server/cloudflare-pages' | ||
export default defineConfig({ | ||
plugins: [ | ||
devServer({ | ||
entry: 'src/index.ts', | ||
cf: { | ||
env: getEnv({ | ||
bindings: { | ||
@@ -180,3 +175,3 @@ NAME: 'Hono', | ||
kvNamespaces: ['MY_KV'], | ||
}, | ||
}), | ||
}), | ||
@@ -189,3 +184,3 @@ ], | ||
### D1 | ||
#### D1 | ||
@@ -281,12 +276,2 @@ When using D1, your app will read `.mf/d1/DB/db.sqlite` which is generated automatically with the following configuration: | ||
## 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. | ||
### `cf` option with Bun | ||
If properties are set in the `cf` option and it's running on Bun, an error will be thrown. | ||
## Authors | ||
@@ -293,0 +278,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
27187
8
26
505
3
278
3
+ Addedminimatch@^9.0.3
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedminimatch@9.0.5(transitive)