1 | # get-tsconfig [![Latest version](https://badgen.net/npm/v/get-tsconfig)](https://npm.im/get-tsconfig)
|
2 |
|
3 | Find and parse `tsconfig.json` files.
|
4 |
|
5 | ### Features
|
6 | - Zero dependency (not even TypeScript)
|
7 | - Tested against TypeScript for correctness
|
8 | - Supports comments & dangling commas in `tsconfig.json`
|
9 | - Resolves [`extends`](https://www.typescriptlang.org/tsconfig/#extends)
|
10 | - Fully typed `tsconfig.json`
|
11 | - Validates and throws parsing errors
|
12 | - Tiny! `3.6 kB` Minified + Gzipped
|
13 |
|
14 | ## 🚀 Install
|
15 |
|
16 | ```bash
|
17 | npm install get-tsconfig
|
18 | ```
|
19 |
|
20 | ## 🙋♀️ Why?
|
21 | For TypeScript related tooling to correctly parse `tsconfig.json` file without depending on TypeScript.
|
22 |
|
23 | ## ⚙️ API
|
24 |
|
25 | ### getTsconfig(searchPath?, configName?)
|
26 | Searches for a `tsconfig.json` file and parses it. Returns `null` if a config file cannot be found, or an object containing the path and parsed TSConfig object if found.
|
27 |
|
28 | Returns:
|
29 |
|
30 | ```ts
|
31 | type TsconfigResult = {
|
32 | /**
|
33 | * The path to the tsconfig.json file
|
34 | */
|
35 | path: string
|
36 |
|
37 | /**
|
38 | * The resolved tsconfig.json file
|
39 | */
|
40 | config: TsConfigJsonResolved
|
41 | }
|
42 | ```
|
43 |
|
44 | #### searchPath
|
45 | Type: `string`
|
46 |
|
47 | Default: `process.cwd()`
|
48 |
|
49 | Accepts a path to a file or directory to search up for a `tsconfig.json` file.
|
50 |
|
51 | #### configName
|
52 | Type: `string`
|
53 |
|
54 | Default: `tsconfig.json`
|
55 |
|
56 | The file name of the TypeScript config file.
|
57 |
|
58 | #### Example
|
59 |
|
60 | ```ts
|
61 | import { getTsconfig } from 'get-tsconfig'
|
62 |
|
63 | // Searches for tsconfig.json starting in the current directory
|
64 | console.log(getTsconfig())
|
65 |
|
66 | // Find tsconfig.json from a TypeScript file path
|
67 | console.log(getTsconfig('./path/to/index.ts'))
|
68 |
|
69 | // Find tsconfig.json from a directory file path
|
70 | console.log(getTsconfig('./path/to/directory'))
|
71 |
|
72 | // Explicitly pass in tsconfig.json path
|
73 | console.log(getTsconfig('./path/to/tsconfig.json'))
|
74 |
|
75 | // Search for jsconfig.json - https://code.visualstudio.com/docs/languages/jsconfig
|
76 | console.log(getTsconfig('.', 'jsconfig.json'))
|
77 | ```
|
78 |
|
79 | ---
|
80 |
|
81 | ### parseTsconfig(tsconfigPath)
|
82 | The `tsconfig.json` parser used internally by `getTsconfig`. Returns the parsed tsconfig as `TsConfigJsonResolved`.
|
83 |
|
84 | #### tsconfigPath
|
85 | Type: `string`
|
86 |
|
87 | Required path to the tsconfig file.
|
88 |
|
89 | #### Example
|
90 |
|
91 | ```ts
|
92 | import { parseTsconfig } from 'get-tsconfig'
|
93 |
|
94 | // Must pass in a path to an existing tsconfig.json file
|
95 | console.log(parseTsconfig('./path/to/tsconfig.custom.json'))
|
96 | ```
|
97 |
|
98 | ---
|
99 |
|
100 | ### createFileMatcher(tsconfig: TsconfigResult, caseSensitivePaths?: boolean)
|
101 |
|
102 | Given a `tsconfig.json` file, it returns a file-matcher function that determines whether it should apply to a file path.
|
103 |
|
104 | ```ts
|
105 | type FileMatcher = (filePath: string) => TsconfigResult['config'] | undefined
|
106 | ```
|
107 |
|
108 | #### tsconfig
|
109 | Type: `TsconfigResult`
|
110 |
|
111 | Pass in the return value from `getTsconfig`, or a `TsconfigResult` object.
|
112 |
|
113 | #### caseSensitivePaths
|
114 | Type: `boolean`
|
115 |
|
116 | By default, it uses [`is-fs-case-sensitive`](https://github.com/privatenumber/is-fs-case-sensitive) to detect whether the file-system is case-sensitive.
|
117 |
|
118 | Pass in `true` to make it case-sensitive.
|
119 |
|
120 | #### Example
|
121 |
|
122 | For example, if it's called with a `tsconfig.json` file that has `include`/`exclude`/`files` defined, the file-matcher will return the config for files that match `include`/`files`, and return `undefined` for files that don't match or match `exclude`.
|
123 |
|
124 | ```ts
|
125 | const tsconfig = getTsconfig()
|
126 | const fileMatcher = tsconfig && createFileMatcher(tsconfig)
|
127 |
|
128 | /*
|
129 | * Returns tsconfig.json if it matches the file,
|
130 | * undefined if not
|
131 | */
|
132 | const configForFile = fileMatcher?.('/path/to/file.ts')
|
133 | const distCode = compileTypescript({
|
134 | code: sourceCode,
|
135 | tsconfig: configForFile
|
136 | })
|
137 | ```
|
138 |
|
139 | ---
|
140 |
|
141 | ### createPathsMatcher(tsconfig: TsconfigResult)
|
142 |
|
143 | Given a tsconfig with [`compilerOptions.paths`](https://www.typescriptlang.org/tsconfig#paths) defined, it returns a matcher function.
|
144 |
|
145 | The matcher function accepts an [import specifier (the path to resolve)](https://nodejs.org/api/esm.html#terminology), checks it against `compilerOptions.paths`, and returns an array of possible paths to check:
|
146 | ```ts
|
147 | function pathsMatcher(specifier: string): string[]
|
148 | ```
|
149 |
|
150 | This function only returns possible paths and doesn't actually do any resolution. This helps increase compatibility wtih file/build systems which usually have their own resolvers.
|
151 |
|
152 | #### Example
|
153 |
|
154 | ```ts
|
155 | import { getTsconfig, createPathsMatcher } from 'get-tsconfig'
|
156 |
|
157 | const tsconfig = getTsconfig()
|
158 | const pathsMatcher = createPathsMatcher(tsconfig)
|
159 |
|
160 | function exampleResolver(request: string) {
|
161 | if (pathsMatcher) {
|
162 | const tryPaths = pathsMatcher(request)
|
163 |
|
164 | // Check if paths in `tryPaths` exist
|
165 | }
|
166 | }
|
167 | ```
|
168 |
|
169 | ## FAQ
|
170 |
|
171 | ### How can I use TypeScript to parse `tsconfig.json`?
|
172 | This package is a re-implementation of TypeScript's `tsconfig.json` parser.
|
173 |
|
174 | However, if you already have TypeScript as a dependency, you can simply use it's API:
|
175 |
|
176 | ```ts
|
177 | import {
|
178 | sys as tsSys,
|
179 | findConfigFile,
|
180 | readConfigFile,
|
181 | parseJsonConfigFileContent
|
182 | } from 'typescript'
|
183 |
|
184 | // Find tsconfig.json file
|
185 | const tsconfigPath = findConfigFile(process.cwd(), tsSys.fileExists, 'tsconfig.json')
|
186 |
|
187 | // Read tsconfig.json file
|
188 | const tsconfigFile = readConfigFile(tsconfigPath, tsSys.readFile)
|
189 |
|
190 | // Resolve extends
|
191 | const parsedTsconfig = parseJsonConfigFileContent(
|
192 | tsconfigFile.config,
|
193 | tsSys,
|
194 | path.dirname(tsconfigPath)
|
195 | )
|
196 | ```
|