tsconfck
A utility to find and parse tsconfig files without depending on typescript
Why
Because no simple official api exists and tsconfig.json isn't actual json.
Features
Install
npm install --save-dev tsconfck # or pnpm, yarn
Usage
without typescript installed
import { parse } from 'tsconfck';
const {
tsconfigFile,
tsconfig,
extended,
solution,
referenced
} = await parse('foo/bar.ts');
with typescript
import { parseNative } from 'tsconfck';
const {
tsconfigFile,
tsconfig,
result,
solution,
referenced
} = await parseNative('foo/bar.ts');
API
see API-DOCS
Advanced
caching
a TSConfckCache instance can be created and passed to find and parse functions to reduce overhead when they are called often within the same project
import { find, parse, TSCOnfckCache } from 'tsconfck';
const cache = new TSCOnfckCache();
const fooTSConfig = await find(('src/foo.ts', { cache }));
const barTSConfig = await find(('src/bar.ts', { cache }));
const fooResult = await parse('src/foo.ts', { cache });
const barResult = await parse('src/bar.ts', { cache });
cache invalidation
You are responsible for clearing the cache if tsconfig files are added/removed/changed after reading them during the cache lifetime.
Call cache.clear()
and also discard all previous compilation results based previously cached configs.
cache mutation
Returned results are direct cache objects. If you want to modify them, deep-clone first.
cache reuse
Never use the same cache instance for mixed calls of find/findNative or parse/parseNative as result structures are different
root
This option can be used to limit finding tsconfig files outside of a root directory
import { parse, TSConfckCache } from 'tsconfck';
const root = '.';
const parseOptions = { root };
const fooResult = await find('src/foo.ts', parseOptions);
const barResult = await parse('src/bar.ts', parseOptions);
Using the root option can lead to errors if there is no tsconfig found inside root.
error handling
find and parse reject for all errors they encounter.
For parse, you can choose to resolve with an empty result instead if no tsconfig file was found
import { parse } from 'tsconfck';
const result = await parse('some/path/without/tsconfig/foo.ts', {
resolveWithEmptyIfConfigNotFound: true
});
TSConfig type (optional, requires typescript as devDependency)
import type { TSConfig } from 'pkg-types';
Check out https://github.com/unjs/pkg-types
cli
A simple cli wrapper is included, you can use it like this
find
# prints /path/to/tsconfig.json on stdout
tsconfck find src/index.ts
find-all
# prints all tsconfig.json in dir on stdout
tsconfck find-all src/
parse
# print content of ParseResult.tsconfig on stdout
tsconfck parse src/index.ts
# print to file
tsconfck parse src/index.ts > output.json
parse-result
# print content of ParseResult on stdout
tsconfck parse-result src/index.ts
# print to file
tsconfck parse-result src/index.ts > output.json
help
# print usage
tsconfck -h # or --help, -?, help
Links
Develop
This repo uses
In addition to default commit-msg prefixes you can use 'wip: ' for commit messages in branches.
PRs are going to be squash-merged
# install dependencies
pnpm install
# run tests
pnpm test
#run tests in watch mode (doesn't require dev in parallel)
pnpm test:watch
License
MIT