export interface ProxyItem { from: string; to: string; /** Per-entry options forwarded to `@fastify/http-proxy`. Setting `undici` here overrides the global `httpProxy` for this entry. */ opts?: Record; } export interface ServerConfig { routerOptions?: { ignoreTrailingSlash?: boolean; [key: string]: unknown }; disableRequestLogging?: boolean; logger?: boolean | Record; https?: boolean; [key: string]: unknown; } export interface DevServerConfig { silent: boolean; force: boolean; proxy: ProxyItem[]; dirs: string[]; dirname?: string; spa: boolean | string; port: number; forcePort?: boolean; host: string; basePath?: string; httpProxy?: string; extend?: (server: any, config: DevServerConfig) => void | Promise; server: ServerConfig; onListen?: (server: any) => void; index?: string | boolean; } // Joi schema is built lazily to avoid calling Joi methods at module load time. // In the browser bundle, Joi is stubbed to an empty object by the nullModules // rollup plugin. Eager top-level calls like Joi.object() crash with // "TypeError: Joi.object is not a function". Deferring schema creation to // normalize() ensures it only runs in Node.js where Joi is available. let _joi: typeof import("joi") | undefined; let _schema: import("joi").Schema | undefined; function getJoi(): typeof import("joi") { if (!_joi) { _joi = require("joi") as typeof import("joi"); } return _joi; } function getSchema(): import("joi").Schema { if (!_schema) { const Joi = getJoi(); const proxyItem = Joi.object({ from: Joi.string().uri({ relativeOnly: true }), to: Joi.string().uri(), opts: Joi.object(), }); _schema = Joi.alternatives().try( Joi.string(), Joi.object({ silent: Joi.boolean(), force: Joi.boolean(), proxy: Joi.array().items(proxyItem), dirs: Joi.array().items(Joi.string()), dirname: Joi.string(), spa: [Joi.boolean(), Joi.string()], port: Joi.number().port(), forcePort: Joi.boolean(), host: [Joi.string().ip(), Joi.string().hostname()], basePath: Joi.string().uri({ relativeOnly: true }), httpProxy: Joi.string().uri(), extend: Joi.function(), server: Joi.object(), onListen: Joi.function(), }), ); } return _schema; } export const serverDefaults = Object.freeze({ routerOptions: { ignoreTrailingSlash: true }, disableRequestLogging: true, }); export const defaults: DevServerConfig = { proxy: [], dirs: ["."], port: 8080, host: "localhost", spa: false, silent: false, force: false, server: { ...serverDefaults, }, basePath: undefined, httpProxy: undefined, extend: undefined, dirname: undefined, onListen: undefined, }; export const normalize = ( rollupOptions: Partial | string = {}, ): DevServerConfig => { const parsed = getJoi().attempt(rollupOptions, getSchema()) as | string | Partial; const normalized = typeof parsed === "string" ? { dirs: [parsed] } : parsed; const serverConfig = Object.assign({}, defaults.server, normalized.server); const config = Object.assign({}, defaults, normalized) as DevServerConfig; config.server = serverConfig; if (config.silent) config.server.logger = false; return config; };