source: frontend/node_modules/tsconfig-paths/src/config-loader.ts

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.4 KB
Line 
1import * as TsConfigLoader2 from "./tsconfig-loader";
2import * as path from "path";
3import { options } from "./options";
4
5export interface ExplicitParams {
6 baseUrl: string;
7 paths: { [key: string]: Array<string> };
8 mainFields?: Array<string>;
9 addMatchAll?: boolean;
10}
11
12export type TsConfigLoader = (
13 params: TsConfigLoader2.TsConfigLoaderParams
14) => TsConfigLoader2.TsConfigLoaderResult;
15
16export interface ConfigLoaderParams {
17 cwd: string;
18 explicitParams?: ExplicitParams;
19 tsConfigLoader?: TsConfigLoader;
20}
21
22export interface ConfigLoaderSuccessResult {
23 resultType: "success";
24 configFileAbsolutePath: string;
25 baseUrl: string;
26 absoluteBaseUrl: string;
27 paths: { [key: string]: Array<string> };
28 mainFields?: Array<string>;
29 addMatchAll?: boolean;
30}
31
32export interface ConfigLoaderFailResult {
33 resultType: "failed";
34 message: string;
35}
36
37export type ConfigLoaderResult =
38 | ConfigLoaderSuccessResult
39 | ConfigLoaderFailResult;
40
41export function loadConfig(cwd: string = options.cwd): ConfigLoaderResult {
42 return configLoader({ cwd: cwd });
43}
44
45export function configLoader({
46 cwd,
47 explicitParams,
48 tsConfigLoader = TsConfigLoader2.tsConfigLoader,
49}: ConfigLoaderParams): ConfigLoaderResult {
50 if (explicitParams) {
51 // tslint:disable-next-line:no-shadowed-variable
52 const absoluteBaseUrl = path.isAbsolute(explicitParams.baseUrl)
53 ? explicitParams.baseUrl
54 : path.join(cwd, explicitParams.baseUrl);
55
56 return {
57 resultType: "success",
58 configFileAbsolutePath: "",
59 baseUrl: explicitParams.baseUrl,
60 absoluteBaseUrl,
61 paths: explicitParams.paths,
62 mainFields: explicitParams.mainFields,
63 addMatchAll: explicitParams.addMatchAll,
64 };
65 }
66
67 // Load tsconfig and create path matching function
68 const loadResult = tsConfigLoader({
69 cwd,
70 getEnv: (key: string) => process.env[key],
71 });
72
73 if (!loadResult.tsConfigPath) {
74 return {
75 resultType: "failed",
76 message: "Couldn't find tsconfig.json",
77 };
78 }
79
80 if (!loadResult.baseUrl) {
81 return {
82 resultType: "failed",
83 message: "Missing baseUrl in compilerOptions",
84 };
85 }
86
87 const tsConfigDir = path.dirname(loadResult.tsConfigPath);
88 const absoluteBaseUrl = path.join(tsConfigDir, loadResult.baseUrl);
89
90 return {
91 resultType: "success",
92 configFileAbsolutePath: loadResult.tsConfigPath,
93 baseUrl: loadResult.baseUrl,
94 absoluteBaseUrl,
95 paths: loadResult.paths || {},
96 };
97}
Note: See TracBrowser for help on using the repository browser.