mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
optimized vite adapter with better defaults
This commit is contained in:
@@ -166,7 +166,7 @@ function baseConfig(adapter: string): tsup.Options {
|
|||||||
minify,
|
minify,
|
||||||
sourcemap,
|
sourcemap,
|
||||||
watch,
|
watch,
|
||||||
entry: [`src/adapter/${adapter}`],
|
entry: [`src/adapter/${adapter}/index.ts`],
|
||||||
format: ["esm"],
|
format: ["esm"],
|
||||||
platform: "neutral",
|
platform: "neutral",
|
||||||
outDir: `dist/adapter/${adapter}`,
|
outDir: `dist/adapter/${adapter}`,
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { serveStatic } from "@hono/node-server/serve-static";
|
import { serveStatic } from "@hono/node-server/serve-static";
|
||||||
|
import { type DevServerOptions, default as honoViteDevServer } from "@hono/vite-dev-server";
|
||||||
import { type RuntimeBkndConfig, createRuntimeApp } from "adapter";
|
import { type RuntimeBkndConfig, createRuntimeApp } from "adapter";
|
||||||
import type { App } from "bknd";
|
import type { App } from "bknd";
|
||||||
|
|
||||||
export type ViteBkndConfig<Env = any> = RuntimeBkndConfig<Env> & {
|
export type ViteBkndConfig<Env = any> = RuntimeBkndConfig<Env> & {
|
||||||
setAdminHtml?: boolean;
|
setAdminHtml?: boolean;
|
||||||
forceDev?: boolean;
|
forceDev?: boolean | { mainPath: string };
|
||||||
html?: string;
|
html?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -24,20 +25,26 @@ ${addBkndContext ? "<!-- BKND_CONTEXT -->" : ""}
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createApp(config: ViteBkndConfig, env?: any) {
|
async function createApp(config: ViteBkndConfig = {}, env?: any) {
|
||||||
return await createRuntimeApp(
|
return await createRuntimeApp(
|
||||||
{
|
{
|
||||||
...config,
|
...config,
|
||||||
adminOptions: config.setAdminHtml
|
adminOptions:
|
||||||
? { html: config.html, forceDev: config.forceDev }
|
config.setAdminHtml === false
|
||||||
: undefined,
|
? undefined
|
||||||
|
: {
|
||||||
|
html: config.html,
|
||||||
|
forceDev: config.forceDev ?? {
|
||||||
|
mainPath: "/src/main.tsx"
|
||||||
|
}
|
||||||
|
},
|
||||||
serveStatic: ["/assets/*", serveStatic({ root: config.distPath ?? "./" })]
|
serveStatic: ["/assets/*", serveStatic({ root: config.distPath ?? "./" })]
|
||||||
},
|
},
|
||||||
env
|
env
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function serveFresh(config: ViteBkndConfig) {
|
export function serveFresh(config: ViteBkndConfig = {}) {
|
||||||
return {
|
return {
|
||||||
async fetch(request: Request, env: any, ctx: ExecutionContext) {
|
async fetch(request: Request, env: any, ctx: ExecutionContext) {
|
||||||
const app = await createApp(config, env);
|
const app = await createApp(config, env);
|
||||||
@@ -47,7 +54,7 @@ export async function serveFresh(config: ViteBkndConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let app: App;
|
let app: App;
|
||||||
export async function serveCached(config: ViteBkndConfig) {
|
export function serveCached(config: ViteBkndConfig = {}) {
|
||||||
return {
|
return {
|
||||||
async fetch(request: Request, env: any, ctx: ExecutionContext) {
|
async fetch(request: Request, env: any, ctx: ExecutionContext) {
|
||||||
if (!app) {
|
if (!app) {
|
||||||
@@ -58,3 +65,21 @@ export async function serveCached(config: ViteBkndConfig) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function devServer(options: DevServerOptions) {
|
||||||
|
return honoViteDevServer({
|
||||||
|
entry: "./server.ts",
|
||||||
|
exclude: [
|
||||||
|
/.*\.tsx?($|\?)/,
|
||||||
|
/^(?!.*\/__admin).*\.(s?css|less)($|\?)/,
|
||||||
|
// exclude except /api
|
||||||
|
/^(?!.*\/api).*\.(ico|mp4|jpg|jpeg|svg|png|vtt|mp3|js)($|\?)/,
|
||||||
|
/^\/@.+$/,
|
||||||
|
/\/components.*?\.json.*/, // @todo: improve
|
||||||
|
/^\/(public|assets|static)\/.+/,
|
||||||
|
/^\/node_modules\/.*/
|
||||||
|
],
|
||||||
|
injectClientScript: false,
|
||||||
|
...options
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user