Bun 支持直接将 HTML 文件导入服务器代码,实现全栈应用,包含服务器端和客户端代码。HTML 导入有两种模式:开发模式 (bun --hot): 资源在运行时按需打包,支持热模块替换(HMR),实现快速迭代开发。当你更改前端代码时,浏览器会自动更新,无需完全刷新页面。生产模式 (bun build): 使用 bun build --target=bun 构建时,import index from "./index.html" 会解析为预构建的清单对象,包含所有打包好的客户端资源。Bun.serve 使用此清单提供优化过的资源,零运行时打包开销,非常适合生产环境部署。
import myReactSinglePageApp from "./index.html";Bun.serve({ routes: { "/": myReactSinglePageApp, },});
HTML 导入不仅仅提供 HTML,它是一个完整的前端打包器、转译器和工具包,使用 Bun 的 bundler、JavaScript 转译器和 CSS 解析器构建。你可以用它搭建带有 React、TypeScript、Tailwind CSS 等的全功能前端。完整的使用 HTML 导入构建全栈应用指南,包括详细示例和最佳实践,请参见 /docs/bundler/fullstack。
interface Server extends Disposable { /** * Stop the server from accepting new connections. * @param closeActiveConnections If true, immediately terminate all connections * @returns Promise resolved when the server stops */ stop(closeActiveConnections?: boolean): Promise<void>; /** * Reload handlers without restarting the server. * Only fetch and error handlers can be updated. */ reload(options: Serve): void; /** * Make a request to the running server. * Used for testing or internal routing. */ fetch(request: Request | string): Response | Promise<Response>; /** * Upgrade an HTTP request to a WebSocket connection. * @returns true if the upgrade succeeds, false otherwise */ upgrade<T = undefined>( request: Request, options?: { headers?: Bun.HeadersInit; data?: T; }, ): boolean; /** * Publish a message to all WebSocket clients subscribed to a topic. * @returns Number of bytes sent, 0 if dropped, -1 if backpressure is applied */ publish( topic: string, data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer, compress?: boolean, ): ServerWebSocketSendStatus; /** * Get the number of WebSocket clients subscribed to a topic. */ subscriberCount(topic: string): number; /** * Get the client IP address and port. * @returns null for closed requests or Unix sockets */ requestIP(request: Request): SocketAddress | null; /** * Set a custom idle timeout for a request. * @param seconds Timeout in seconds, 0 to disable */ timeout(request: Request, seconds: number): void; /** * Keep the process alive while the server is running. */ ref(): void; /** * Allow the server to be the only running runtime process to exit. */ unref(): void; /** Current number of HTTP requests being processed */ readonly pendingRequests: number; /** Current number of active WebSocket connections */ readonly pendingWebSockets: number; /** Full server URL, including protocol, hostname, and port */ readonly url: URL; /** Port the server is listening on */ readonly port: number; /** Hostname the server is bound to */ readonly hostname: string; /** Whether the server is in development mode */ readonly development: boolean; /** Server instance ID */ readonly id: string;}interface WebSocketHandler<T = undefined> { /** Maximum WebSocket message size, in bytes */ maxPayloadLength?: number; /** Number of bytes to queue messages before applying backpressure */ backpressureLimit?: number; /** Whether to close the connection when the backpressure limit is reached */ closeOnBackpressureLimit?: boolean; /** Called when backpressure is relieved */ drain?(ws: ServerWebSocket<T>): void | Promise<void>; /** Idle timeout, in seconds */ idleTimeout?: number; /** Enable per-message deflate compression */ perMessageDeflate?: | boolean | { compress?: WebSocketCompressor | boolean; decompress?: WebSocketCompressor | boolean; }; /** Send Ping frames to keep the connection alive */ sendPings?: boolean; /** Whether the server will receive messages published to itself */ publishToSelf?: boolean; /** Called when the connection opens */ open?(ws: ServerWebSocket<T>): void | Promise<void>; /** Called when a message is received */ message(ws: ServerWebSocket<T>, message: string | Buffer): void | Promise<void>; /** Called when the connection closes */ close?(ws: ServerWebSocket<T>, code: number, reason: string): void | Promise<void>; /** Called when a Ping frame is received */ ping?(ws: ServerWebSocket<T>, data: Buffer): void | Promise<void>; /** Called when a Pong frame is received */ pong?(ws: ServerWebSocket<T>, data: Buffer): void | Promise<void>;}interface TLSOptions { /** Certificate authority chain */ ca?: string | Buffer | BunFile | Array<string | Buffer | BunFile>; /** Server certificate */ cert?: string | Buffer | BunFile | Array<string | Buffer | BunFile>; /** Path to the DH parameters file */ dhParamsFile?: string; /** Private key */ key?: string | Buffer | BunFile | Array<string | Buffer | BunFile>; /** Reduce TLS memory usage */ lowMemoryMode?: boolean; /** Private key passphrase */ passphrase?: string; /** OpenSSL option flags */ secureOptions?: number; /** SNI server name */ serverName?: string;}