Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Lightweight tsconfig.json parser & paths resolver

License

NotificationsYou must be signed in to change notification settings

privatenumber/get-tsconfig

Repository files navigation

get-tsconfig

Find and parsetsconfig.json files.

Features

  • Zero dependency (not even TypeScript)
  • Tested against TypeScript for correctness
  • Supports comments & dangling commas intsconfig.json
  • Resolvesextends
  • Fully typedtsconfig.json
  • Validates and throws parsing errors
  • Tiny!7 kB Minified + Gzipped

Already a sponsor? Join the discussion in theDevelopment repo!

Install

npm install get-tsconfig

Why?

For TypeScript related tooling to correctly parsetsconfig.json file without depending on TypeScript.

API

getTsconfig(searchPath?, configName?, cache?)

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}

searchPath

Type:string

Default:process.cwd()

Accepts a path to a file or directory to search up for atsconfig.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?)

Parse the tsconfig file provided. Used internally bygetTsconfig. Returns the parsed tsconfig asTsConfigJsonResolved.

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'))

createFileMatcher(tsconfig: TsconfigResult, caseSensitivePaths?: boolean)

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

tsconfig

Type:TsconfigResult

Pass in the return value fromgetTsconfig, or aTsconfigResult object.

caseSensitivePaths

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.

Example

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})

createPathsMatcher(tsconfig: TsconfigResult)

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.

Example

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}}

FAQ

How can I use TypeScript to parsetsconfig.json?

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))

Sponsors

About

Lightweight tsconfig.json parser & paths resolver

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp