All files options.ts

93.33% Statements 14/15
0% Branches 0/1
100% Functions 3/3
92.86% Lines 13/14

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                              1x                             1x 2x   2x   2x                   2x 2x 5x   5x 5x                   2x                             2x         2x    
export type LogLevel = "INFO" | "WARN" | "ERROR";
 
export interface Options {
  readonly configFile: string;
  readonly extensions: ReadonlyArray<string>;
  readonly baseUrl: string | undefined;
  readonly silent: boolean;
  readonly logLevel: LogLevel;
  readonly logInfoToStdOut: boolean;
  readonly context: string | undefined;
  readonly colors: boolean;
  readonly mainFields: string[];
}
 
type ValidOptions = keyof Options;
const validOptions: ReadonlyArray<ValidOptions> = [
  "configFile",
  "extensions",
  "baseUrl",
  "silent",
  "logLevel",
  "logInfoToStdOut",
  "context",
  "mainFields",
];
 
/**
 * Takes raw options from the webpack config,
 * validates them and adds defaults for missing options
 */
export function getOptions(rawOptions: {}): Options {
  validateOptions(rawOptions);
 
  const options = makeOptions(rawOptions);
 
  return options;
}
 
/**
 * Validate the supplied loader options.
 * At present this validates the option names only; in future we may look at validating the values too
 *
 * @param rawOptions
 */
function validateOptions(rawOptions: {}): void {
  const loaderOptionKeys = Object.keys(rawOptions);
  for (let i = 0; i < loaderOptionKeys.length; i++) {
    const option = loaderOptionKeys[i];
    const isUnexpectedOption =
      (validOptions as ReadonlyArray<string>).indexOf(option) === -1;
    Iif (isUnexpectedOption) {
      throw new Error(`tsconfig-paths-webpack-plugin was supplied with an unexpected loader option: ${option}
Please take a look at the options you are supplying; the following are valid options:
${validOptions.join(" / ")}
`);
    }
  }
}
 
function makeOptions(rawOptions: Partial<Options>): Options {
  const options: Options = {
    ...({
      configFile: "tsconfig.json",
      extensions: [".ts", ".tsx"],
      baseUrl: undefined,
      silent: false,
      logLevel: "WARN",
      logInfoToStdOut: false,
      context: undefined,
      colors: true,
      mainFields: ["main"],
    } as Options),
    ...rawOptions,
  };
 
  const options2: Options = {
    ...options,
    logLevel: options.logLevel.toUpperCase() as LogLevel,
  };
 
  return options2;
}