1 | # get-tsconfig [![Latest version](https://badgen.net/npm/v/get-tsconfig)](https://npm.im/get-tsconfig) [![npm downloads](https://badgen.net/npm/dm/esbuild-loader)](https://npm.im/esbuild-loader)
|
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! `7 kB` Minified + Gzipped
|
13 |
|
14 | <br>
|
15 |
|
16 | <p align="center">
|
17 | <a href="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=platinum">
|
18 | <picture>
|
19 | <source width="830" media="(prefers-color-scheme: dark)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=platinum&image=dark">
|
20 | <source width="830" media="(prefers-color-scheme: light)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=platinum&image">
|
21 | <img width="830" src="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=platinum&image" alt="Premium sponsor banner">
|
22 | </picture>
|
23 | </a>
|
24 | </p>
|
25 |
|
26 | ## Install
|
27 |
|
28 | ```bash
|
29 | npm install get-tsconfig
|
30 | ```
|
31 |
|
32 | ## Why?
|
33 | For TypeScript related tooling to correctly parse `tsconfig.json` file without depending on TypeScript.
|
34 |
|
35 | ## API
|
36 |
|
37 | ### getTsconfig(searchPath?, configName?, cache?)
|
38 | 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.
|
39 |
|
40 | Returns:
|
41 |
|
42 | ```ts
|
43 | type TsconfigResult = {
|
44 | /**
|
45 | * The path to the tsconfig.json file
|
46 | */
|
47 | path: string
|
48 |
|
49 | /**
|
50 | * The resolved tsconfig.json file
|
51 | */
|
52 | config: TsConfigJsonResolved
|
53 | }
|
54 | ```
|
55 |
|
56 | #### searchPath
|
57 | Type: `string`
|
58 |
|
59 | Default: `process.cwd()`
|
60 |
|
61 | Accepts a path to a file or directory to search up for a `tsconfig.json` file.
|
62 |
|
63 | #### configName
|
64 | Type: `string`
|
65 |
|
66 | Default: `tsconfig.json`
|
67 |
|
68 | The file name of the TypeScript config file.
|
69 |
|
70 | #### cache
|
71 | Type: `Map<string, any>`
|
72 |
|
73 | Default: `new Map()`
|
74 |
|
75 | Optional cache for fs operations.
|
76 |
|
77 | #### Example
|
78 |
|
79 | ```ts
|
80 | import { getTsconfig } from 'get-tsconfig'
|
81 |
|
82 | // Searches for tsconfig.json starting in the current directory
|
83 | console.log(getTsconfig())
|
84 |
|
85 | // Find tsconfig.json from a TypeScript file path
|
86 | console.log(getTsconfig('./path/to/index.ts'))
|
87 |
|
88 | // Find tsconfig.json from a directory file path
|
89 | console.log(getTsconfig('./path/to/directory'))
|
90 |
|
91 | // Explicitly pass in tsconfig.json path
|
92 | console.log(getTsconfig('./path/to/tsconfig.json'))
|
93 |
|
94 | // Search for jsconfig.json - https://code.visualstudio.com/docs/languages/jsconfig
|
95 | console.log(getTsconfig('.', 'jsconfig.json'))
|
96 | ```
|
97 |
|
98 | ---
|
99 |
|
100 | ### parseTsconfig(tsconfigPath, cache?)
|
101 | The `tsconfig.json` parser used internally by `getTsconfig`. Returns the parsed tsconfig as `TsConfigJsonResolved`.
|
102 |
|
103 | #### tsconfigPath
|
104 | Type: `string`
|
105 |
|
106 | Required path to the tsconfig file.
|
107 |
|
108 | #### cache
|
109 | Type: `Map<string, any>`
|
110 |
|
111 | Default: `new Map()`
|
112 |
|
113 | Optional cache for fs operations.
|
114 |
|
115 | #### Example
|
116 |
|
117 | ```ts
|
118 | import { parseTsconfig } from 'get-tsconfig'
|
119 |
|
120 | // Must pass in a path to an existing tsconfig.json file
|
121 | console.log(parseTsconfig('./path/to/tsconfig.custom.json'))
|
122 | ```
|
123 |
|
124 | ---
|
125 |
|
126 | <p align="center">
|
127 | <a href="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=gold">
|
128 | <picture>
|
129 | <source width="830" media="(prefers-color-scheme: dark)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=gold&image=dark">
|
130 | <source width="830" media="(prefers-color-scheme: light)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=gold&image">
|
131 | <img width="830" src="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=gold&image" alt="Premium sponsor banner">
|
132 | </picture>
|
133 | </a>
|
134 | </p>
|
135 |
|
136 | ---
|
137 |
|
138 | ### createFileMatcher(tsconfig: TsconfigResult, caseSensitivePaths?: boolean)
|
139 |
|
140 | Given a `tsconfig.json` file, it returns a file-matcher function that determines whether it should apply to a file path.
|
141 |
|
142 | ```ts
|
143 | type FileMatcher = (filePath: string) => TsconfigResult['config'] | undefined
|
144 | ```
|
145 |
|
146 | #### tsconfig
|
147 | Type: `TsconfigResult`
|
148 |
|
149 | Pass in the return value from `getTsconfig`, or a `TsconfigResult` object.
|
150 |
|
151 | #### caseSensitivePaths
|
152 | Type: `boolean`
|
153 |
|
154 | 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.
|
155 |
|
156 | Pass in `true` to make it case-sensitive.
|
157 |
|
158 | #### Example
|
159 |
|
160 | 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`.
|
161 |
|
162 | ```ts
|
163 | const tsconfig = getTsconfig()
|
164 | const fileMatcher = tsconfig && createFileMatcher(tsconfig)
|
165 |
|
166 | /*
|
167 | * Returns tsconfig.json if it matches the file,
|
168 | * undefined if not
|
169 | */
|
170 | const configForFile = fileMatcher?.('/path/to/file.ts')
|
171 | const distCode = compileTypescript({
|
172 | code: sourceCode,
|
173 | tsconfig: configForFile
|
174 | })
|
175 | ```
|
176 |
|
177 | ---
|
178 |
|
179 | ### createPathsMatcher(tsconfig: TsconfigResult)
|
180 |
|
181 | Given a tsconfig with [`compilerOptions.paths`](https://www.typescriptlang.org/tsconfig#paths) defined, it returns a matcher function.
|
182 |
|
183 | 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:
|
184 | ```ts
|
185 | function pathsMatcher(specifier: string): string[]
|
186 | ```
|
187 |
|
188 | 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.
|
189 |
|
190 | #### Example
|
191 |
|
192 | ```ts
|
193 | import { getTsconfig, createPathsMatcher } from 'get-tsconfig'
|
194 |
|
195 | const tsconfig = getTsconfig()
|
196 | const pathsMatcher = createPathsMatcher(tsconfig)
|
197 |
|
198 | function exampleResolver(request: string) {
|
199 | if (pathsMatcher) {
|
200 | const tryPaths = pathsMatcher(request)
|
201 |
|
202 | // Check if paths in `tryPaths` exist
|
203 | }
|
204 | }
|
205 | ```
|
206 |
|
207 | ## FAQ
|
208 |
|
209 | ### How can I use TypeScript to parse `tsconfig.json`?
|
210 | This package is a re-implementation of TypeScript's `tsconfig.json` parser.
|
211 |
|
212 | However, if you already have TypeScript as a dependency, you can simply use it's API:
|
213 |
|
214 | ```ts
|
215 | import {
|
216 | sys as tsSys,
|
217 | findConfigFile,
|
218 | readConfigFile,
|
219 | parseJsonConfigFileContent
|
220 | } from 'typescript'
|
221 |
|
222 | // Find tsconfig.json file
|
223 | const tsconfigPath = findConfigFile(process.cwd(), tsSys.fileExists, 'tsconfig.json')
|
224 |
|
225 | // Read tsconfig.json file
|
226 | const tsconfigFile = readConfigFile(tsconfigPath, tsSys.readFile)
|
227 |
|
228 | // Resolve extends
|
229 | const parsedTsconfig = parseJsonConfigFileContent(
|
230 | tsconfigFile.config,
|
231 | tsSys,
|
232 | path.dirname(tsconfigPath)
|
233 | )
|
234 | ```
|
235 |
|
236 | ## Sponsors
|
237 |
|
238 | <p align="center">
|
239 | <a href="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver1">
|
240 | <picture>
|
241 | <source width="410" media="(prefers-color-scheme: dark)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver1&image=dark">
|
242 | <source width="410" media="(prefers-color-scheme: light)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver1&image">
|
243 | <img width="410" src="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver1&image" alt="Premium sponsor banner">
|
244 | </picture>
|
245 | </a>
|
246 | <a href="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver2">
|
247 | <picture>
|
248 | <source width="410" media="(prefers-color-scheme: dark)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver2&image=dark">
|
249 | <source width="410" media="(prefers-color-scheme: light)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver2&image">
|
250 | <img width="410" src="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver2&image" alt="Premium sponsor banner">
|
251 | </picture>
|
252 | </a>
|
253 | </p>
|
254 |
|
255 | <p align="center">
|
256 | <a href="https://github.com/sponsors/privatenumber">
|
257 | <img src="https://cdn.jsdelivr.net/gh/privatenumber/sponsors/sponsorkit/sponsors.svg">
|
258 | </a>
|
259 | </p>
|