All files / src/dev-server config.ts

95% Statements 19/20
88.88% Branches 8/9
100% Functions 3/3
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                                                                                    7x 1x   7x       6x 1x 1x           1x                                       6x     1x         1x                                   1x     6x     5x 5x 5x 5x 5x 5x    
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<string, unknown>;
}
 
export interface ServerConfig {
  routerOptions?: { ignoreTrailingSlash?: boolean; [key: string]: unknown };
  disableRequestLogging?: boolean;
  logger?: boolean | Record<string, unknown>;
  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<void>;
  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<DevServerConfig> | string = {},
): DevServerConfig => {
  const parsed = getJoi().attempt(rollupOptions, getSchema()) as
    | string
    | Partial<DevServerConfig>;
  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;
  Iif (config.silent) config.server.logger = false;
  return config;
};