Gathering detailed insights and metrics for get-tsconfig
Gathering detailed insights and metrics for get-tsconfig
Gathering detailed insights and metrics for get-tsconfig
Gathering detailed insights and metrics for get-tsconfig
Lightweight tsconfig.json parser & paths resolver
npm install get-tsconfig
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
187 Stars
145 Commits
13 Forks
5 Watching
3 Branches
12 Contributors
Updated on 11 Nov 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-6.6%
2,384,214
Compared to previous day
Last week
2.9%
13,706,752
Compared to previous week
Last month
9.2%
57,319,828
Compared to previous month
Last year
140.9%
500,894,261
Compared to previous year
1
Find and parse tsconfig.json
files.
tsconfig.json
extends
tsconfig.json
7 kB
Minified + GzippedAlready a sponsor? Join the discussion in the Development repo!
1npm install get-tsconfig
For TypeScript related tooling to correctly parse tsconfig.json
file without depending on TypeScript.
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.
Returns:
1type TsconfigResult = { 2 3 /** 4 * The path to the tsconfig.json file 5 */ 6 path: string 7 8 /** 9 * The resolved tsconfig.json file 10 */ 11 config: TsConfigJsonResolved 12}
Type: string
Default: process.cwd()
Accepts a path to a file or directory to search up for a tsconfig.json
file.
Type: string
Default: tsconfig.json
The file name of the TypeScript config file.
Type: Map<string, any>
Default: new Map()
Optional cache for fs operations.
1import { getTsconfig } from 'get-tsconfig'
2
3// Searches for tsconfig.json starting in the current directory
4console.log(getTsconfig())
5
6// Find tsconfig.json from a TypeScript file path
7console.log(getTsconfig('./path/to/index.ts'))
8
9// Find tsconfig.json from a directory file path
10console.log(getTsconfig('./path/to/directory'))
11
12// Explicitly pass in tsconfig.json path
13console.log(getTsconfig('./path/to/tsconfig.json'))
14
15// Search for jsconfig.json - https://code.visualstudio.com/docs/languages/jsconfig
16console.log(getTsconfig('.', 'jsconfig.json'))
The tsconfig.json
parser used internally by getTsconfig
. Returns the parsed tsconfig as TsConfigJsonResolved
.
Type: string
Required path to the tsconfig file.
Type: Map<string, any>
Default: new Map()
Optional cache for fs operations.
1import { parseTsconfig } from 'get-tsconfig'
2
3// Must pass in a path to an existing tsconfig.json file
4console.log(parseTsconfig('./path/to/tsconfig.custom.json'))
Given a tsconfig.json
file, it returns a file-matcher function that determines whether it should apply to a file path.
1type FileMatcher = (filePath: string) => TsconfigResult['config'] | undefined
Type: TsconfigResult
Pass in the return value from getTsconfig
, or a TsconfigResult
object.
Type: boolean
By default, it uses is-fs-case-sensitive
to detect whether the file-system is case-sensitive.
Pass in true
to make it case-sensitive.
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
.
1const tsconfig = getTsconfig()
2const fileMatcher = tsconfig && createFileMatcher(tsconfig)
3
4/*
5 * Returns tsconfig.json if it matches the file,
6 * undefined if not
7 */
8const configForFile = fileMatcher?.('/path/to/file.ts')
9const distCode = compileTypescript({
10 code: sourceCode,
11 tsconfig: configForFile
12})
Given a tsconfig with compilerOptions.paths
defined, it returns a matcher function.
The matcher function accepts an import specifier (the path to resolve), checks it against compilerOptions.paths
, and returns an array of possible paths to check:
1function pathsMatcher(specifier: string): string[]
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.
1import { getTsconfig, createPathsMatcher } from 'get-tsconfig' 2 3const tsconfig = getTsconfig() 4const pathsMatcher = createPathsMatcher(tsconfig) 5 6const exampleResolver = (request: string) => { 7 if (pathsMatcher) { 8 const tryPaths = pathsMatcher(request) 9 10 // Check if paths in `tryPaths` exist 11 } 12}
tsconfig.json
?This package is a re-implementation of TypeScript's tsconfig.json
parser.
However, if you already have TypeScript as a dependency, you can simply use it's API:
1import {
2 sys as tsSys,
3 findConfigFile,
4 readConfigFile,
5 parseJsonConfigFileContent
6} from 'typescript'
7
8// Find tsconfig.json file
9const tsconfigPath = findConfigFile(process.cwd(), tsSys.fileExists, 'tsconfig.json')
10
11// Read tsconfig.json file
12const tsconfigFile = readConfigFile(tsconfigPath, tsSys.readFile)
13
14// Resolve extends
15const parsedTsconfig = parseJsonConfigFileContent(
16 tsconfigFile.config,
17 tsSys,
18 path.dirname(tsconfigPath)
19)
No vulnerabilities found.
No security vulnerabilities found.