UNPKG

5.26 kBMarkdownView Raw
1# get-tsconfig [![Latest version](https://badgen.net/npm/v/get-tsconfig)](https://npm.im/get-tsconfig)
2
3Find 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
17npm install get-tsconfig
18```
19
20## 🙋‍♀️ Why?
21For TypeScript related tooling to correctly parse `tsconfig.json` file without depending on TypeScript.
22
23## ⚙️ API
24
25### getTsconfig(searchPath?, configName?)
26Searches 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
28Returns:
29
30```ts
31type 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
45Type: `string`
46
47Default: `process.cwd()`
48
49Accepts a path to a file or directory to search up for a `tsconfig.json` file.
50
51#### configName
52Type: `string`
53
54Default: `tsconfig.json`
55
56The file name of the TypeScript config file.
57
58#### Example
59
60```ts
61import { getTsconfig } from 'get-tsconfig'
62
63// Searches for tsconfig.json starting in the current directory
64console.log(getTsconfig())
65
66// Find tsconfig.json from a TypeScript file path
67console.log(getTsconfig('./path/to/index.ts'))
68
69// Find tsconfig.json from a directory file path
70console.log(getTsconfig('./path/to/directory'))
71
72// Explicitly pass in tsconfig.json path
73console.log(getTsconfig('./path/to/tsconfig.json'))
74
75// Search for jsconfig.json - https://code.visualstudio.com/docs/languages/jsconfig
76console.log(getTsconfig('.', 'jsconfig.json'))
77```
78
79---
80
81### parseTsconfig(tsconfigPath)
82The `tsconfig.json` parser used internally by `getTsconfig`. Returns the parsed tsconfig as `TsConfigJsonResolved`.
83
84#### tsconfigPath
85Type: `string`
86
87Required path to the tsconfig file.
88
89#### Example
90
91```ts
92import { parseTsconfig } from 'get-tsconfig'
93
94// Must pass in a path to an existing tsconfig.json file
95console.log(parseTsconfig('./path/to/tsconfig.custom.json'))
96```
97
98---
99
100### createFileMatcher(tsconfig: TsconfigResult, caseSensitivePaths?: boolean)
101
102Given a `tsconfig.json` file, it returns a file-matcher function that determines whether it should apply to a file path.
103
104```ts
105type FileMatcher = (filePath: string) => TsconfigResult['config'] | undefined
106```
107
108#### tsconfig
109Type: `TsconfigResult`
110
111Pass in the return value from `getTsconfig`, or a `TsconfigResult` object.
112
113#### caseSensitivePaths
114Type: `boolean`
115
116By 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
118Pass in `true` to make it case-sensitive.
119
120#### Example
121
122For 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
125const tsconfig = getTsconfig()
126const fileMatcher = tsconfig && createFileMatcher(tsconfig)
127
128/*
129 * Returns tsconfig.json if it matches the file,
130 * undefined if not
131 */
132const configForFile = fileMatcher?.('/path/to/file.ts')
133const distCode = compileTypescript({
134 code: sourceCode,
135 tsconfig: configForFile
136})
137```
138
139---
140
141### createPathsMatcher(tsconfig: TsconfigResult)
142
143Given a tsconfig with [`compilerOptions.paths`](https://www.typescriptlang.org/tsconfig#paths) defined, it returns a matcher function.
144
145The 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
147function pathsMatcher(specifier: string): string[]
148```
149
150This 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
155import { getTsconfig, createPathsMatcher } from 'get-tsconfig'
156
157const tsconfig = getTsconfig()
158const pathsMatcher = createPathsMatcher(tsconfig)
159
160function 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`?
172This package is a re-implementation of TypeScript's `tsconfig.json` parser.
173
174However, if you already have TypeScript as a dependency, you can simply use it's API:
175
176```ts
177import {
178 sys as tsSys,
179 findConfigFile,
180 readConfigFile,
181 parseJsonConfigFileContent
182} from 'typescript'
183
184// Find tsconfig.json file
185const tsconfigPath = findConfigFile(process.cwd(), tsSys.fileExists, 'tsconfig.json')
186
187// Read tsconfig.json file
188const tsconfigFile = readConfigFile(tsconfigPath, tsSys.readFile)
189
190// Resolve extends
191const parsedTsconfig = parseJsonConfigFileContent(
192 tsconfigFile.config,
193 tsSys,
194 path.dirname(tsconfigPath)
195)
196```