get-tsconfig
Find and parse the tsconfig.json file from a directory path
Last updated 2 years ago by hirokiosame .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install get-tsconfig 
SYNC missed versions from official npm registry.

get-tsconfig Latest version npm downloads

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! 7 kB Minified + Gzipped

<picture> <source width="830" media="(prefers-color-scheme: dark)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=platinum&image=dark"> <source width="830" media="(prefers-color-scheme: light)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=platinum&image"> Premium sponsor banner </picture>

Install

npm install get-tsconfig

Why?

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:

type TsconfigResult = {
    /**
     * 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 directory
console.log(getTsconfig())

// Find tsconfig.json from a TypeScript file path
console.log(getTsconfig('./path/to/index.ts'))

// Find tsconfig.json from a directory file path
console.log(getTsconfig('./path/to/directory'))

// Explicitly pass in tsconfig.json path
console.log(getTsconfig('./path/to/tsconfig.json'))

// Search for jsconfig.json - https://code.visualstudio.com/docs/languages/jsconfig
console.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 file
console.log(parseTsconfig('./path/to/tsconfig.custom.json'))

<picture> <source width="830" media="(prefers-color-scheme: dark)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=gold&image=dark"> <source width="830" media="(prefers-color-scheme: light)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=gold&image"> Premium sponsor banner </picture>


createFileMatcher(tsconfig: TsconfigResult, caseSensitivePaths?: boolean)

Given a tsconfig.json file, it returns a file-matcher function that determines whether it should apply to a file path.

type FileMatcher = (filePath: string) => TsconfigResult['config'] | undefined

tsconfig

Type: TsconfigResult

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

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:

function 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

import { getTsconfig, createPathsMatcher } from 'get-tsconfig'

const tsconfig = getTsconfig()
const pathsMatcher = createPathsMatcher(tsconfig)

function exampleResolver(request: string) {
    if (pathsMatcher) {
        const tryPaths = pathsMatcher(request)

        // Check if paths in `tryPaths` exist
    }
}

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:

import {
    sys as tsSys,
    findConfigFile,
    readConfigFile,
    parseJsonConfigFileContent
} from 'typescript'

// Find tsconfig.json file
const tsconfigPath = findConfigFile(process.cwd(), tsSys.fileExists, 'tsconfig.json')

// Read tsconfig.json file
const tsconfigFile = readConfigFile(tsconfigPath, tsSys.readFile)

// Resolve extends
const parsedTsconfig = parseJsonConfigFileContent(
    tsconfigFile.config,
    tsSys,
    path.dirname(tsconfigPath)
)

Sponsors

<picture> <source width="410" media="(prefers-color-scheme: dark)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver1&image=dark"> <source width="410" media="(prefers-color-scheme: light)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver1&image"> Premium sponsor banner </picture> <picture> <source width="410" media="(prefers-color-scheme: dark)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver2&image=dark"> <source width="410" media="(prefers-color-scheme: light)" srcset="https://privatenumber-sponsors.vercel.app/api/sponsor?tier=silver2&image"> Premium sponsor banner </picture>

Current Tags

  • 4.7.2                                ...           latest (a year ago)

25 Versions

  • 4.7.2                                ...           a year ago
  • 4.7.1                                ...           a year ago
  • 4.7.0                                ...           a year ago
  • 4.6.2                                ...           a year ago
  • 4.6.1                                ...           a year ago
  • 4.6.0                                ...           2 years ago
  • 4.5.0                                ...           2 years ago
  • 4.4.0                                ...           2 years ago
  • 4.3.0                                ...           2 years ago
  • 4.2.0                                ...           2 years ago
  • 4.1.0                                ...           2 years ago
  • 4.0.7                                ...           2 years ago
  • 4.0.6                                ...           2 years ago
  • 4.0.5                                ...           2 years ago
  • 4.0.4                                ...           2 years ago
  • 4.0.3                                ...           2 years ago
  • 4.0.2                                ...           2 years ago
  • 4.0.1                                ...           2 years ago
  • 4.0.0                                ...           3 years ago
  • 3.2.0                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.0.1                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 8
Last Day 0
Last Week 8
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None

Copyright 2013 - present © cnpmjs.org | Home |