@hono/vite-dev-server
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -7,6 +7,8 @@ import { WorkerOptions } from 'miniflare'; | ||
injectClientScript?: boolean; | ||
exclude?: (string | RegExp)[]; | ||
cf?: Partial<Omit<WorkerOptions, 'name' | 'script' | 'scriptPath' | 'modules' | 'modulesRoot' | 'modulesRules'>>; | ||
}; | ||
declare const defaultOptions: Required<Omit<DevServerOptions, 'cf'>>; | ||
declare function devServer(options?: DevServerOptions): Plugin; | ||
export { DevServerOptions, devServer }; | ||
export { DevServerOptions, defaultOptions, devServer }; |
@@ -1,1 +0,78 @@ | ||
import{getRequestListener as h}from"@hono/node-server";const v='addEventListener("fetch", (event) => event.respondWith(new Response(null, { status: 404 })));';function x(n){const i=n?.entry??"./src/index.ts";return{name:"@hono/vite-dev-server",config:()=>({build:{rollupOptions:{input:[i]}}}),configureServer:async a=>{let r;if(n?.cf){const{Miniflare:o}=await import("miniflare");r=new o({script:v,...n?.cf})}async function d(o){return async function(t,f,c){if(t.url?.endsWith(".ts")||t.url?.endsWith(".tsx")||t.url?.startsWith("/@")||t.url?.startsWith("/node_modules"))return c();const p=(await o.ssrLoadModule(i)).default;if(!p)return console.error(`Failed to find a named export "default" from ${i}`),c();h(async m=>{let u={};r&&(u=await r.getBindings());const e=await p.fetch(m,u,{waitUntil:async s=>s,passThroughOnException:()=>{throw new Error("`passThroughOnException` is not supported")}});if(n?.injectClientScript!==!1&&!e.headers.get("transfer-encoding")?.match("chunked")&&e.headers.get("content-type")?.match(/^text\/html/)){const s=await e.text()+'<script type="module" src="/@vite/client"></script>',l=new Headers(e.headers);return l.delete("content-length"),new Response(s,{status:e.status,headers:l})}return e})(t,f)}}a.middlewares.use(await d(a))}}}export{x as devServer}; | ||
import { getRequestListener } from "@hono/node-server"; | ||
const defaultOptions = { | ||
entry: "./src/index.ts", | ||
injectClientScript: true, | ||
exclude: [".*.ts", ".*.tsx", "/@.+", "/node_modules/.*"] | ||
}; | ||
const nullScript = 'addEventListener("fetch", (event) => event.respondWith(new Response(null, { status: 404 })));'; | ||
function devServer(options) { | ||
const entry = options?.entry ?? defaultOptions.entry; | ||
const plugin = { | ||
name: "@hono/vite-dev-server", | ||
config: () => { | ||
return { | ||
build: { | ||
rollupOptions: { | ||
input: [entry] | ||
} | ||
} | ||
}; | ||
}, | ||
configureServer: async (server) => { | ||
let mf = void 0; | ||
if (options?.cf) { | ||
const { Miniflare } = await import("miniflare"); | ||
mf = new Miniflare({ | ||
script: nullScript, | ||
...options.cf | ||
}); | ||
} | ||
async function createMiddleware(server2) { | ||
return async function(req, res, next) { | ||
const exclude = options?.exclude ?? defaultOptions.exclude; | ||
exclude.map((pattern) => { | ||
const regExp = new RegExp(`^${pattern}$`); | ||
if (req.url && regExp.test(req.url)) { | ||
return next(); | ||
} | ||
}); | ||
const appModule = await server2.ssrLoadModule(entry); | ||
const app = appModule["default"]; | ||
if (!app) { | ||
console.error(`Failed to find a named export "default" from ${entry}`); | ||
return next(); | ||
} | ||
getRequestListener(async (request) => { | ||
let bindings = {}; | ||
if (mf) { | ||
bindings = await mf.getBindings(); | ||
} | ||
const response = await app.fetch(request, bindings, { | ||
waitUntil: async (fn) => fn, | ||
passThroughOnException: () => { | ||
throw new Error("`passThroughOnException` is not supported"); | ||
} | ||
}); | ||
if (options?.injectClientScript !== false && // If the response is a streaming, it does not inject the script: | ||
!response.headers.get("transfer-encoding")?.match("chunked") && response.headers.get("content-type")?.match(/^text\/html/)) { | ||
const body = await response.text() + '<script type="module" src="/@vite/client"></script>'; | ||
const headers = new Headers(response.headers); | ||
headers.delete("content-length"); | ||
return new Response(body, { | ||
status: response.status, | ||
headers | ||
}); | ||
} | ||
return response; | ||
})(req, res); | ||
}; | ||
} | ||
server.middlewares.use(await createMiddleware(server)); | ||
} | ||
}; | ||
return plugin; | ||
} | ||
export { | ||
defaultOptions, | ||
devServer | ||
}; |
import { devServer } from './dev-server.js'; | ||
export { DevServerOptions } from './dev-server.js'; | ||
export { DevServerOptions, defaultOptions } from './dev-server.js'; | ||
import 'miniflare'; | ||
@@ -4,0 +4,0 @@ import 'vite'; |
@@ -1,1 +0,7 @@ | ||
import{devServer as e}from"./dev-server.js";var o=e;export{o as default}; | ||
import { devServer } from "./dev-server.js"; | ||
import { defaultOptions } from "./dev-server.js"; | ||
var src_default = devServer; | ||
export { | ||
src_default as default, | ||
defaultOptions | ||
}; |
{ | ||
"name": "@hono/vite-dev-server", | ||
"description": "Vite dev-server plugin for Hono", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"types": "dist/index.d.ts", | ||
@@ -6,0 +6,0 @@ "module": "dist/index.js", |
@@ -105,2 +105,3 @@ # @hono/vite-dev-server | ||
injectClientScript?: boolean | ||
exclude?: (string | RegExp)[] | ||
cf?: Partial< | ||
@@ -115,2 +116,12 @@ Omit< | ||
Default values: | ||
```ts | ||
export const defaultOptions: Required<Omit<DevServerOptions, 'cf'>> = { | ||
entry: './src/index.ts', | ||
injectClientScript: true, | ||
exclude: ['.*.ts', '.*.tsx', '/@.+', '/node_modules/.*'], | ||
} | ||
``` | ||
### `injectClientScript` | ||
@@ -120,2 +131,6 @@ | ||
### `exclude` | ||
The paths which are not served by the dev-server. | ||
## Cloudflare Bindings | ||
@@ -151,3 +166,3 @@ | ||
If properties are set in the `cf`` option and it's running on Bun, an error will be thrown. | ||
If properties are set in the `cf` option and it's running on Bun, an error will be thrown. | ||
@@ -154,0 +169,0 @@ ## Authors |
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
8733
101
172