For TypeScript related tooling to correctly parse tsconfig.json file without depending on TypeScript.
API
getTsconfig(searchPath?, configName?, cache?)
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:
typeTsconfigResult = {
/**
* The path to the tsconfig.json file
*/path: string/**
* The resolved tsconfig.json file
*/config: TsConfigJsonResolved
}
searchPath
Type: string
Default: process.cwd()
Accepts a path to a file or directory to search up for a tsconfig.json file.
configName
Type: string
Default: tsconfig.json
The file name of the TypeScript config file.
cache
Type: Map<string, any>
Default: new Map()
Optional cache for fs operations.
Example
import { getTsconfig } from'get-tsconfig'// Searches for tsconfig.json starting in the current directoryconsole.log(getTsconfig())
// Find tsconfig.json from a TypeScript file pathconsole.log(getTsconfig('./path/to/index.ts'))
// Find tsconfig.json from a directory file pathconsole.log(getTsconfig('./path/to/directory'))
// Explicitly pass in tsconfig.json pathconsole.log(getTsconfig('./path/to/tsconfig.json'))
// Search for jsconfig.json - https://code.visualstudio.com/docs/languages/jsconfigconsole.log(getTsconfig('.', 'jsconfig.json'))
parseTsconfig(tsconfigPath, cache?)
The tsconfig.json parser used internally by getTsconfig. Returns the parsed tsconfig as TsConfigJsonResolved.
tsconfigPath
Type: string
Required path to the tsconfig file.
cache
Type: Map<string, any>
Default: new Map()
Optional cache for fs operations.
Example
import { parseTsconfig } from'get-tsconfig'// Must pass in a path to an existing tsconfig.json fileconsole.log(parseTsconfig('./path/to/tsconfig.custom.json'))
Pass in the return value from getTsconfig, or a TsconfigResult object.
caseSensitivePaths
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.
Example
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.
const tsconfig = getTsconfig()
const fileMatcher = tsconfig && createFileMatcher(tsconfig)
/*
* Returns tsconfig.json if it matches the file,
* undefined if not
*/const configForFile = fileMatcher?.('/path/to/file.ts')
const distCode = compileTypescript({
code: sourceCode,
tsconfig: configForFile
})
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:
functionpathsMatcher(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.
Find and parse the tsconfig.json file from a directory path
The npm package get-tsconfig receives a total of 8,071,999 weekly downloads. As such, get-tsconfig popularity was classified as popular.
We found that get-tsconfig demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 1 open source maintainer collaborating on the project.
Last updated on 10 Aug 2023
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Researchers have demonstrated that teams of LLM agents can exploit zero-day vulnerabilities with a 53% success rate, and the costs of using AI to do so are rapidly becoming more affordable than hiring a human penetration tester.
In an unprecedented surge, May 2024 saw the publication of over 5,000 CVEs, marking a historic milestone in cybersecurity with an average of 164 CVEs per day, nearly double the 2023 daily average.
The White House is addressing fragmented cybersecurity regulations as CISOs report spending up to 50% of their time on compliance, aiming to harmonize requirements and improve cybersecurity outcomes.