Skip to content

flex-development/tsconfig-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

456 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

tsconfig-utils

github release npm npm downloads install size codecov module type: esm license conventional commits typescript vitest yarn

Utilities for working with tsconfig files

Contents

What is this?

This package exports utilities for working with TypeScript configuration files.

When should I use this?

This package can be used to merge and load tsconfig files, as well resolve path aliases.

Install

This package is ESM only.

In Node.js (version 20+) with yarn:

yarn add @flex-development/tsconfig-utils
See Git - Protocols | Yarn  for details regarding installing from Git.

In Deno with esm.sh:

import {
  createModuleResolutionHost,
  loadTsconfig,
  resolvePath
} from 'https://esm.sh/@flex-development/tsconfig-utils'

In browsers with esm.sh:

<script type="module">
  import {
    createModuleResolutionHost,
    loadTsconfig,
    resolvePath
  } from 'https://esm.sh/@flex-development/tsconfig-utils'
</script>

Use

import {
  loadTsconfig,
  type ResolvedTsconfig
} from '@flex-development/tsconfig-utils'
import fs from 'node:fs'

/**
 * The resolved tsconfig.
 *
 * @const {ResolvedTsconfig | null} resolved
 */
const resolved: ResolvedTsconfig | null = await loadTsconfig(null, {
  encoding: 'utf8',
  fs: fs.promises
})

if (resolved) {
  console.dir(resolved.tsconfig) // the loaded tsconfig, with bases applied
  console.dir(resolved.url) // the url of the tsconfig file
}

API

This package exports the following identifiers listed below.

There is no default export.

createGetCanonicalFileName([useCaseSensitiveFileNames])

Create a canonical file name function.

Parameters

Returns

(GetCanonicalFileName) A function that returns a canonical file name given a module id

createModuleResolutionHost([options])

Create a module resolution host.

The module resolution host acts a bridge between the TypeScript compiler and the file system.

👉 Note: The host can have both asynchronous and synchronous methods, but when used with the native TypeScript compiler, all methods must return synchronous values.

Parameters

Returns

(ModuleResolutionHost) The module resolution host

createParseConfigHost([options])

Create a configuration parser host.

The parser host provides methods for accessing the file system and resolving module paths.

👉 Note: The host can have both asynchronous and synchronous methods, but when used with the native TypeScript compiler, all methods must return synchronous values.

Parameters

Returns

(ParseConfigHost) The parse config host

isResolvedTsconfig(value)

Check if value is a resolved configuration object.

Parameters

  • value (unknown) — the value to check

Returns

(value is ResolvedTsconfig) true if value is resolved tsconfig object, false otherwise

isTsconfigHost(value)

Check if value is an object with a Tsconfig.

Parameters

  • value (unknown) — the value to check

Returns

(value is TsconfigHost) true if value is tsconfig host object, false otherwise

loadTsconfig<T>([id][, options])

Load a tsconfig file.

Type Parameters

Parameters

  • id (ModuleId | null | undefined) — the module id or specifier of the tsconfig file
    • default: 'tsconfig.json'
  • options (LoadTsconfigOptions | null | undefined) — load options

Returns

(T) The resolved tsconfig, or null if tsconfig file is not found

mergeTsconfig<T>(target[, ...tsconfigs])

Merge one or more tsconfig objects into a single Tsconfig.

Tsconfig source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

👉 Note: If target is a TsconfigHost, target.tsconfig will be modified. Otherwise, target will be modified.

Type Parameters

Parameters

  • target (Tsconfig | TsconfigHost | null | undefined) — the target tsconfig or tsconfig host
  • ...tsconfigs (readonly (Tsconfig | TsconfigHost | null | undefined)[]) — the source tsconfig object(s)

Returns

(T) The merged tsconfig

readTsconfig<T>([id][, options])

Read a tsconfig file.

👉 Note: Returns a promise if getSource or resolveModule returns a promise.

Type Parameters

Parameters

  • id (ModuleId | null | undefined) — the module id or specifier of the tsconfig file
    • default: 'tsconfig.json'
  • options (ReadTsconfigOptions | null | undefined) — read options

Returns

(T) The resolved tsconfig, or null if tsconfig file is not found

resolvePath(specifier[, options])

Resolve an aliased specifier.

Parameters

  • specifier (string) — the specifier using an alias
  • options (ResolvePathOptions | null | undefined) — alias resolution options

Returns

(string | null) The path alias match or null if match is not found

Types

This package is fully typed with TypeScript.

tsconfig-types

This package re-exports TypeScript definitions from tsconfig-types. This is primarily for the convenience of TypeScript users who do not hoist packages, but may need to import definitions used in this package.

Awaitable<T>

Create a union of T and T as a promise-like object (type).

type Awaitable<T> = PromiseLike<T> | T

Type Parameters

  • T (any) — the value

DirectoryExists

Check if a directory exists (interface).

Signatures

<T extends Awaitable<boolean>>(id: ModuleId): T

Type Parameters

Parameters

  • id (ModuleId) — the module id to check

Returns

(T) true if directory exists at id, false otherwise

Dirent

Information about a directory entry (interface).

Properties

  • isDirectory (IsDirectory) — check if the entry is a directory
  • isFile (IsFile) — check if the entry is a file
  • isSymbolicLink (IsSymbolicLink) — check if the entry is a symbolic link
  • name (string) — the path to the entry, relative to the parentPath
  • parentPath (string) — the path to the parent directory

FileExists

Check if a file exists (interface).

Signatures

<T extends Awaitable<boolean>>(id: ModuleId): T

Type Parameters

Parameters

  • id (ModuleId) — the module id to check

Returns

(T) true if file exists at id, false otherwise

FileSystem

The file system API (interface).

Properties

  • readFile (ReadFile) — read the entire contents of a file
  • readdir (Readdir) — read the entire contents of a directory
  • realpath (Realpath) — compute a canonical pathname by resolving ., .., and symbolic links
  • stat (Stat) — get information about a file system entry

GetCanonicalFileName

Get the canonical file name for a module id (type).

type GetCanonicalFileName = (this: void, id: ModuleId) => string

Parameters

Returns

(string) The canonical file name

GetCurrentDirectory

Get the path to the current working directory (interface).

Signatures

<T extends Awaitable<string>>(): T

Type Parameters

Parameters

  • id (ModuleId) — the module id to check

Returns

(T) The current working directory path

GetDirectories

Get a list of subdirectories (interface).

Signatures

<T extends Awaitable<string[]>>(parent: ModuleId): T

Type Parameters

Parameters

  • id (ModuleId) — the module id of the parent directory

Returns

(T) The list of subdirectory names

HostReadFile

Read the entire contents of a file (interface).

Signatures

<T extends Awaitable<string | undefined>>(id: ModuleId): T

Type Parameters

Parameters

  • id (ModuleId) — the module id of the file

Returns

(T) The file contents, or undefined if file does not exist at id

IsDirectory

Check if a file system entry is a directory (interface).

Extends

IsFile

Check if a file system entry is a file (interface).

Extends

IsSymbolicLink

Check if a file system entry is a symbolic link (interface).

Signatures

(): boolean

Returns

(boolean) true if entry is symbolic link, false otherwise

List<[T]>

A list (type).

type List<T = unknown> = ReadonlySet<T> | readonly T[]

Type Parameters

  • T (any, optional) — the list item type

LoadTsconfigOptions

Options for loading tsconfig files (interface).

Extends

Properties

  • relativePaths (List<string> | null | undefined) — the list of property paths where the value may be a relative path

ModuleResolutionHostOptions

Options for creating module resolution hosts (interface).

Properties

  • encoding? (BufferEncoding) — the encoding to use when reading files
    • default: 'utf8'
  • fs? (FileSystem | null | undefined) — the file system api
  • root? (ModuleId | null | undefined) — the module id of the current working directory
  • useCaseSensitiveFileNames? (UseCaseSensitiveFileNames) — boolean indicating whether filenames should be treated as case sensitive, or a function that returns such a value

ModuleResolutionHost

The module resolution host API (interface).

The module resolution host acts a bridge between the TypeScript compiler and the file system.

👉 Note: The host can have both asynchronous and synchronous methods, but when used with the native TypeScript compiler, all methods must return synchronous values.

Properties

  • directoryExists (DirectoryExists) — check if a directory exists
  • fileExists (FileExists) — check if a file exists
  • getCurrentDirectory (GetCurrentDirectory) — get the path to the current working directory
  • getDirectories (GetDirectories) — get a list of subdirectories
  • readFile (HostReadFile) — read the entire contents of a file
  • realpath (Realpath) — compute a canonical pathname by resolving ., .., and symbolic links
  • useCaseSensitiveFileNames? (UseCaseSensitiveFileNames) — whether to treat filenames as case sensitive

ParseConfigHostOptions

Options for creating parse config hosts (interface).

Extends

ParseConfigHost

The configuration parser host API (interface).

The parser host provides methods for accessing the file system and resolving module paths.

👉 Note: The host can have both asynchronous and synchronous methods, but when used with the native TypeScript compiler, all methods must return synchronous values.

Extends

Properties

  • readDirectory (ReadDirectory) — read the contents of a directory
  • useCaseSensitiveFileNames (boolean) — whether to treat filenames as case sensitive

ReadDirectory

Read the contents of a directory (interface).

Signatures

<T extends Awaitable<readonly string[]>>(
  parent: ModuleId,
  extensions?: List<string> | null | undefined,
  exclude?: List<string> | null | undefined,
  include?: List<string> | null | undefined,
  depth?: number | null | undefined
): T

Type Parameters

Parameters

  • parent (ModuleId) — the module id of the parent directory
  • extensions (List<string> | null | undefined) — the list of file extensions to filter for
  • exclude (List<string> | null | undefined) — the list of glob patterns matching files to exclude
  • include (List<string> | null | undefined) — the list of glob patterns matching files to include
  • depth (number | null | undefined) — the maximum search depth (inclusive)

Returns

(T) The list of matched files

ReadFile

Read the entire contents of a file (interface).

Extends

ReadTsconfigOptions

Options for reading tsconfig files (interface).

Extends

Properties

  • conditions (List<Condition> | null | undefined) — the list of export/import conditions

    👉 note: should be sorted by priority

  • cwd (ModuleId | null | undefined) — the url of the current working directory
  • mainFields (List<MainField> | null | undefined) — the list of legacy main fields

    👉 note: should be sorted by priority

  • parent (ModuleId | null | undefined) — the parent module id
  • preserveSymlinks (boolean | null | undefined) — whether to keep symlinks instead of resolving them

ReaddirDirentOptions

Options for reading the contents of a directory (interface).

Extends

Properties

  • withFileTypes (true) — whether the result should be a content object list instead of just strings.
    if true, the result will be a list of Direct objects, which provide methods like isDirectory() and isFile() to get more information about a file system entry without additional fs.stat() calls

ReaddirOptions

Options for reading the contents of a directory (interface).

Extends

Properties

  • withFileTypes? (boolean | null | undefined) — whether the result should be a content object list instead of just strings.
    if true, the result will be a list of Direct objects, which provide methods like isDirectory() and isFile() to get more information about a file system entry without additional fs.stat() calls

Readdir

Read the entire contents of a directory (interface).

Signatures

<T extends Awaitable<readonly Dirent[]>>(id: ModuleId, options: ReaddirDirentOptions): T

Type Parameters

Parameters

Returns

(T) The directory contents

Realpath

Compute a canonical pathname by resolving ., .., and symbolic links (interface).

Extends

ResolvePathOptions

Options for path alias resolution (interface).

Extends

Properties

  • aliases? (null | undefined) — the url of the tsconfig file

    👉 note: path aliases are read from the tsconfig

  • tsconfig? (ResolvedTsconfig | Tsconfig null | undefined) — the tsconfig object, or the resolved tsconfig

ResolvedTsconfig

A resolved TypeScript configuration (interface).

Extends

Properties

  • url (URL) — the url of the tsconfig file

Stat

Get information about a file system entry (interface).

Extends

Stats

Information about a file system entry (interface).

Properties

  • isDirectory (IsDirectory) — check if the entry is a directory
  • isFile (IsFile) — check if the entry is a file

TsconfigHost

An object with a TypeScript configuration (interface).

Properties

  • tsconfig (Tsconfig) — the tsconfig object

UseCaseSensitiveFileNamesFn

Determine if file names should be treated as case sensitive (type).

type UseCaseSensitiveFileNamesFn = (this: void) => boolean | null | undefined

Returns

(boolean | null | undefined) true if file names should be treated as case sensitive

UseCaseSensitiveFileNames

Union of values used to determine if file names should be treated as case sensitive (type).

type UseCaseSensitiveFileNames =
  | UseCaseSensitiveFileNamesFn
  | boolean
  | null
  | undefined

Returns

(boolean | null | undefined) true if file names should be treated as case sensitive

Related

Contribute

See CONTRIBUTING.md.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

Sponsor this project

 

Packages

 
 
 

Contributors 4

  •  
  •  
  •  
  •  

Languages