npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tsconfig-resolver

v3.0.1

Published

Walk up the tree to resolve `tsconfig.json` configuration objects.

Downloads

168,799

Readme

tsconfig-resolver

All Contributors

GitHub Actions Build Status Version Weekly Downloads Typed Codebase MIT License semantic-release

A tool for loading the nearest tsconfig file in the same way the TypeScript compiler does. It walks up the directory tree until it finds the first matching tsconfig.json file.

Table of Contents

Usage

tsconfig-resolver is designed to be used inside your node project. It automatically populates the extends field so that the configuration received is the same as would be used by any consuming TypeScript project.

Setup

First, install the plugin and it's peer dependencies:

npm install --save tsconfig-resolver

or

yarn add tsconfig-resolver

Code Example

The following will load the first tsconfig.json file working upwards from the process.cwd() of the running node process.

import { tsconfigResolver } from 'tsconfig-resolver';

const result = await tsconfigResolver();

// without type narrowing
console.log(result?.config);

// with type narrowing
if (result.exists) {
  console.log(result.config);
}

Configuration options can also be passed into the export function.

import { join } from 'path';
import { CacheStrategy, tsconfigResolver } from 'tsconfig-resolver';
const result = tsconfig({
  cwd: join(__dirname, 'src'),
  fileName: 'tsconfig.prod.json',
  cache: CacheStrategy.Directory,
});

API

tsconfigResolverSync

import { tsconfigResolverSync } from 'tsconfig-resolver';

Returns

The function returns an object consisting of the following.

| Property | Type | Description | | ----------------- | ------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | config | TsConfigJson or undefined | The configuration object. undefined when the tsconfig resolver failed and no configuration was found.TsConfigJson (exported by the type-fest library) when the resolved tsconfig has been found and loaded. | | exists | boolean | Whether or not the configuration could be loaded. false when no tsconfig could be found.true when a valid tsconfig file has been found and successfully loaded. | | reason | TsConfigErrorReason or undefined | The reason for failure. TsConfigErrorReason.NotFound when the config failure is because the filename has not been found.TsConfigErrorReason.InvalidConfig when the config failure is because of an invalid config.undefined when no failure has occurred. | | path | string or undefined | The absolute path to the tsconfig.json or given filename.undefined when not found.string when config json is invalid.string when a valid tsconfig has been loaded. | | extendedPaths | string[] or undefined | The extendedPaths array.undefined when the tsconfig resolver failed to load a valid configuration.string[] of absolute paths to resolved tsconfig files when extended paths are encountered.[] an empty array when no extended paths are encountered.[] an empty array when ignoreExtends options is set to true. | | isCircular | boolean or undefined | The isCircular config flag.undefined when the tsconfig resolver failed to load a valid configuration.true when a circular extends property was encountered (an extends path chain that references itself).false when no circular extends property was encountered. |

Options

| Property | Type | Default | Description | | ----------------- | --------------------- | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | cwd | string | process.cwd() | The directory to start searching from. | | searchName | string | 'tsconfig.json' | The tsconfig file name to search for. This is where the TsConfigJson configuration object will be loaded from. | | filePath | string | undefined | A direct path to the tsconfig file you would like to load. The path will be resolved relative to the current process.cwd(). If it leads to a directory then the searchName will be appended. * This also supports the npm: prefix which will find the given npm package directory, if it is installed. When provided the cache is set to 'always' by default. | | cache | string or boolean | 'never' | Set the caching strategy that will be used when searching for a file that's already been found. When a filePath is provided the default value becomes 'always'. | | ignoreExtends | boolean | false | When true will not automatically populate the extends argument. This is useful if all you want is the json object and not the fully resolved configuration. |

tsconfigResolver

import { tsconfigResolver } from 'tsconfig-resolver';

The same API as tsconfigResolverSync except the return value is wrapped in a promise.

clearCache

Clears the cache.

import { clearCache, tsconfigResolver } from 'tsconfig-resolver';

const result = await tsconfigResolver();

// Now clear the cache.
clearCache();

CacheStrategy

import { CacheStrategy } from 'tsconfig-resolver';

Provides the available caching strategies that can be used.

Sometimes you'll want to run this module several times during runtime but it can be slow and expensive walk up the file tree for the tsconfig value every time.

To help prevent unnecessary lookups there are custom caching strategies available.

  • CacheStrategy.Never - Caching never happens and the returned value is always recalculated
  • CacheStrategy.Always - The first time the tsconfigResolver method is run it will save a cached value (by searchName) which will be returned every time after that. This value will always be the same. This is turned on by default when a filePath is provided.
  • CacheStrategy.Directory - The cache will be used when the same directory (and searchName) is being searched.

TsConfigErrorReason

import { TsConfigErrorReason } from 'tsconfig-resolver';

This provides the reason for the error in resolving the tsconfig.

  • TsConfigErrorReason.NotFound - The tsconfig file could not be found.
  • TsConfigErrorReason.InvalidConfig - The file was found but the configuration was invalid.

TsConfigJson

Re-exported from type-fest.

Contributing

Dive into the codebase with Gitpod.

Open in Gitpod

Versioning

This project uses SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE file for details

Contributors