Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork17
Lightweight tsconfig.json parser & paths resolver
License
privatenumber/get-tsconfig
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Find and parsetsconfig.json files.
- Zero dependency (not even TypeScript)
- Tested against TypeScript for correctness
- Supports comments & dangling commas in
tsconfig.json - Resolves
extends - Fully typed
tsconfig.json - Validates and throws parsing errors
- Tiny!
7 kBMinified + Gzipped
Already a sponsor? Join the discussion in theDevelopment repo!
npm install get-tsconfig
For TypeScript related tooling to correctly parsetsconfig.json file without depending on TypeScript.
Searches for a tsconfig file (defaults totsconfig.json) in thesearchPath and parses it. (If you already know the tsconfig path, useparseTsconfig instead). Returnsnull 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}
Type:string
Default:process.cwd()
Accepts a path to a file or directory to search up for atsconfig.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.
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'))
Parse the tsconfig file provided. Used internally bygetTsconfig. Returns the parsed tsconfig asTsConfigJsonResolved.
Type:string
Required path to the tsconfig file.
Type:Map<string, any>
Default:new Map()
Optional cache for fs operations.
import{parseTsconfig}from'get-tsconfig'// Must pass in a path to an existing tsconfig.json fileconsole.log(parseTsconfig('./path/to/tsconfig.custom.json'))
Given atsconfig.json file, it returns a file-matcher function that determines whether it should apply to a file path.
typeFileMatcher=(filePath:string)=>TsconfigResult['config']|undefined
Type:TsconfigResult
Pass in the return value fromgetTsconfig, or aTsconfigResult object.
Type:boolean
By default, it usesis-fs-case-sensitive to detect whether the file-system is case-sensitive.
Pass intrue to make it case-sensitive.
For example, if it's called with atsconfig.json file that hasinclude/exclude/files defined, the file-matcher will return the config for files that matchinclude/files, and returnundefined for files that don't match or matchexclude.
consttsconfig=getTsconfig()constfileMatcher=tsconfig&&createFileMatcher(tsconfig)/* * Returns tsconfig.json if it matches the file, * undefined if not */constconfigForFile=fileMatcher?.('/path/to/file.ts')constdistCode=compileTypescript({code:sourceCode,tsconfig:configForFile})
Given a tsconfig withcompilerOptions.paths defined, it returns a matcher function.
The matcher function accepts animport specifier (the path to resolve), checks it againstcompilerOptions.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.
import{getTsconfig,createPathsMatcher}from'get-tsconfig'consttsconfig=getTsconfig()constpathsMatcher=createPathsMatcher(tsconfig)constexampleResolver=(request:string)=>{if(pathsMatcher){consttryPaths=pathsMatcher(request)// Check if paths in `tryPaths` exist}}
This package is a re-implementation of TypeScript'stsconfig.json parser.
However, if you already have TypeScript as a dependency, you can simply use it's API:
import{sysastsSys,findConfigFile,readConfigFile,parseJsonConfigFileContent}from'typescript'// Find tsconfig.json fileconsttsconfigPath=findConfigFile(process.cwd(),tsSys.fileExists,'tsconfig.json')// Read tsconfig.json fileconsttsconfigFile=readConfigFile(tsconfigPath,tsSys.readFile)// Resolve extendsconstparsedTsconfig=parseJsonConfigFileContent(tsconfigFile.config,tsSys,path.dirname(tsconfigPath))
About
Lightweight tsconfig.json parser & paths resolver
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.


