Lightweight tsconfig.json parser & paths resolver
Installations
npm install @unts/get-tsconfig
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Node Version
16.14.2
NPM Version
8.5.0
Score
79
Supply Chain
99.1
Quality
75
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
privatenumber
Download Statistics
Total Downloads
2,098,677
Last Day
488
Last Week
2,456
Last Month
10,809
Last Year
329,618
GitHub Statistics
196 Stars
148 Commits
14 Forks
5 Watching
3 Branches
12 Contributors
Bundle Size
10.19 kB
Minified
3.68 kB
Minified + Gzipped
Sponsor this package
Package Meta Information
Latest Version
4.1.1
Package Id
@unts/get-tsconfig@4.1.1
Unpacked Size
52.88 kB
Size
14.75 kB
File Count
6
NPM Version
8.5.0
Node Version
16.14.2
Total Downloads
Cumulative downloads
Total Downloads
2,098,677
Last day
-35.1%
488
Compared to previous day
Last week
-28.1%
2,456
Compared to previous week
Last month
-9.8%
10,809
Compared to previous month
Last year
-44.7%
329,618
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
get-tsconfig
Find and parse tsconfig.json
files.
Features
- 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!
3.6 kB
Minified + Gzipped
🚀 Install
1npm install get-tsconfig
🙋♀️ Why?
For TypeScript related tooling to correctly parse tsconfig.json
file without depending on TypeScript.
⚙️ API
getTsconfig(searchPath?, configName?)
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 * The path to the tsconfig.json file 4 */ 5 path: string 6 7 /** 8 * The resolved tsconfig.json file 9 */ 10 config: TsConfigJsonResolved 11}
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.
Example
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'))
parseTsconfig(tsconfigPath)
The tsconfig.json
parser used internally by getTsconfig
. Returns the parsed tsconfig as TsConfigJsonResolved
.
tsconfigPath
Type: string
Required path to the tsconfig file.
Example
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'))
createPathsMatcher(tsconfig: TsconfigResult)
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.
Example
1import { getTsconfig, createPathsMatcher } from 'get-tsconfig'
2
3const tsconfig = getTsconfig()
4const pathsMatcher = createPathsMatcher(tsconfig)
5
6function exampleResolver(request: string) {
7 if (pathsMatcher) {
8 const tryPaths = pathsMatcher(request)
9
10 // Check if paths in `tryPaths` exist
11 }
12}
FAQ
How can I use TypeScript to parse 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.