7.8.153 • Published 7 months ago

@firanorg/ut-officiis-et v7.8.153

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

@firanorg/ut-officiis-et 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

Install

npm install @firanorg/ut-officiis-et

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 '@firanorg/ut-officiis-et'

// 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 '@firanorg/ut-officiis-et'

// Must pass in a path to an existing tsconfig.json file
console.log(parseTsconfig('./path/to/tsconfig.custom.json'))


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 '@firanorg/ut-officiis-et'

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

const 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

Object.assignserializeprototypecryptoArray.prototype.findLastIndexartnegativemulti-packageenvtouchpreprocessorHyBitrimStartcollection.es6metadatasimpledbprotobufimportexportflattenTypeScriptlogtestingECMAScript 7monoreporequirees-shimssuperagentdiffinstallsyntaxclassnamel10nwatches2018owninferencemobileisConcatSpreadablebannerequalityiamstringifiercss-in-jsminimalUint8ArraycolorsbyteLengthpasswordprettypushclassnameswalkcontainsweaksetmakeimportes-abstracthas-ownschemaaccessorawesomesaucefastcollectionInt16Arrayfastcloneconcatless mixinsdeterministicarrayshotpromisesprunecss lessArray.prototype.includesawaitruntimeObject.entriesmodulesnpmcoercibleboundprocessswfmime-dbclass-validatorvestutilitiesmimebootstrap csstc39esimmutableES2022cjkECMAScript 5namesbufferReactiveXauthconfigurableansiES3hashloadbalancingpackage.jsona11yexpressiondependency managercryptentrieschinese_.extendbundlingpackagesES2021copyeventEmitterparseredactbusystringifyeast-asian-widthterminalpropertieseventDispatcherfileECMAScript 2018workflowmacoswgetArray.prototype.findLastkinesisproxyreact-hook-formtermsigintbreakcompile lessES8ES5limitbundlerrandomtapeinstallernameformupES2017sideoffseteslintpluginponyfillObject.valueses7ECMAScript 2020matchWebSockets3jsenumerableerrordependencieslinuxreuseUint32Arraycloudfrontroute53columnssuperstructttyPromiseTypedArrayes8es5JSONregular expressionregularcomputed-typeses-shim APIrestfulnumbertasktstoArrayenvironmentjsdomsymbolsESjsdiffeslintsyntaxerrorES2015cloudsearchYAMLzodhttpsfindupeslintconfighigher-orderroutingredux-toolkitelmcheckstateObservableclonedataViewslicesinatrafastifybrowserliststyleESnextgradients cssargsargumentexpressnested cssurlmkdirpchaikarmaencryptionmixinsinternal slotsharedsequencetypesrecursiverateprefixstylingURLrouterloggingpluginpreserve-symlinksgroupwordbreakdom-testing-libraryfscachefastcopyreal-timecorehardlinkswatchingsigtermtypesafewritableidutilitygradients css3ajaxtestquerystringparentspostcssbootstrap lessbrowsershimsortappquerynodejsprotocol-buffersSetawstostringtaglockfilebatchgenericsassignslotebsdeepcensordescriptiondeepcopyhasOwnlrulistenersfolderinvariantyupfunctionsinputcore-jsfindLastIndexxtermshamdeletegetterfpsprivatefilterthrottlejavascriptdefineArrayBufferformattingagentidlelookpostcss-pluginmatchAllArray.prototype.flatcirculariteratecomparedebuggerwriteprivate dataiteratorconfigflagslook-upfluxdeep-clonerfc4122Array.prototype.flattenSymbolspecRegExp.prototype.flagsframeworkutil.inspectcallbounddomweakmapestreeWebSocketspatchpipejwtpropertydeep-copyObject.isiterationrm -frlinewrapsymlinkspolyfillexecUint16ArrayargparsefullloggerasyncmatchesextraECMAScript 2022directorygdpropentaketoolsexitendpointjQuerywarningstyled-componentsvariables in cssrdsfindlimitedrmdir-0hasOwnPropertytypedflaghooksoptimistcharactersignalszeroBigUint64ArrayES7queueMicrotaskparentfull-widthamazonelectronpromiseresolveless.jses2015weberror-handlingRxinternalqueuestyleguideprotoeveryoncecommand-linestoragegatewaystreamreducecode pointsmruString.prototype.trimdotenvregular expressionsvisual[[Prototype]]stdlibloadingvaluethroat0ES6symbolUint8ClampedArraylazyguidelbwaittypeofsymlinktypanionfast-copyimmerecmascriptbddsomewatchFiletextformatviewescapecall-bindes2017momentbrowserslistconsumebuffersvalidlesskoreantddpicomatchES2020walkingArrayhandlerssnsextendRFC-6455efficientremoveJSON-SchemaArray.prototype.filtercolorassertionincludestapbluebirdeventsObjecttoStringTagpnpm9extensionnativejestcorsjapanesehascloudtraildataviewfseventsrestomitinterruptshelpersexit-codeMicrosoftsetPrototypeOfaccessibilityindicatoremojiworker
7.8.153

7 months ago

7.8.152

7 months ago

7.7.138

7 months ago

7.7.135

8 months ago

7.7.134

8 months ago

7.7.137

7 months ago

7.7.136

8 months ago

7.8.150

7 months ago

7.8.149

7 months ago

7.8.148

7 months ago

7.8.147

7 months ago

7.8.146

7 months ago

7.8.145

7 months ago

7.8.144

7 months ago

7.8.143

7 months ago

7.8.142

7 months ago

7.8.141

7 months ago

7.8.140

7 months ago

7.8.151

7 months ago

7.8.139

7 months ago

7.8.138

7 months ago

7.7.133

8 months ago

7.7.132

8 months ago

7.7.131

8 months ago

7.7.130

8 months ago

7.7.129

8 months ago

7.7.128

8 months ago

7.7.127

8 months ago

7.7.124

8 months ago

7.7.123

8 months ago

7.7.126

8 months ago

7.7.125

8 months ago

7.7.122

8 months ago

7.7.121

8 months ago

7.7.120

8 months ago

7.7.119

8 months ago

7.7.117

8 months ago

7.7.116

8 months ago

7.7.118

8 months ago

7.7.115

8 months ago

7.6.115

8 months ago

7.6.114

8 months ago

7.6.113

8 months ago

7.6.112

8 months ago

7.6.111

8 months ago

7.6.110

8 months ago

7.6.109

8 months ago

7.6.108

8 months ago

7.6.107

9 months ago

7.6.106

9 months ago

7.6.105

9 months ago

7.5.78

10 months ago

7.5.79

9 months ago

4.1.37

11 months ago

7.5.84

9 months ago

7.5.85

9 months ago

7.5.86

9 months ago

7.5.87

9 months ago

7.5.80

9 months ago

7.5.81

9 months ago

7.5.82

9 months ago

7.5.83

9 months ago

7.5.88

9 months ago

7.5.89

9 months ago

7.6.101

9 months ago

5.2.46

11 months ago

7.6.100

9 months ago

5.2.45

11 months ago

5.2.44

11 months ago

5.2.43

11 months ago

5.2.42

11 months ago

7.6.104

9 months ago

5.2.41

11 months ago

7.6.103

9 months ago

5.2.40

11 months ago

7.6.102

9 months ago

3.1.34

11 months ago

3.1.33

11 months ago

3.1.36

11 months ago

7.5.90

9 months ago

3.1.35

11 months ago

3.1.37

11 months ago

7.5.95

9 months ago

7.5.96

9 months ago

7.5.97

9 months ago

7.5.98

9 months ago

7.5.91

9 months ago

7.5.92

9 months ago

7.5.93

9 months ago

7.5.94

9 months ago

6.4.61

10 months ago

5.2.39

11 months ago

6.4.60

10 months ago

6.4.63

10 months ago

6.4.62

10 months ago

6.4.54

10 months ago

6.4.53

10 months ago

6.4.56

10 months ago

6.4.55

10 months ago

6.4.58

10 months ago

6.4.57

10 months ago

6.4.59

10 months ago

6.4.52

10 months ago

7.4.63

10 months ago

7.4.64

10 months ago

7.4.69

10 months ago

7.4.65

10 months ago

7.4.66

10 months ago

7.4.67

10 months ago

7.4.68

10 months ago

7.4.72

10 months ago

7.4.73

10 months ago

7.4.74

10 months ago

7.4.75

10 months ago

7.6.99

9 months ago

7.6.98

9 months ago

7.4.70

10 months ago

7.4.71

10 months ago

7.4.76

10 months ago

7.4.77

10 months ago

7.4.78

10 months ago

4.2.39

11 months ago

4.2.37

11 months ago

4.2.38

11 months ago

3.0.23

12 months ago

3.0.24

12 months ago

3.0.21

12 months ago

3.0.22

12 months ago

3.0.27

12 months ago

3.0.28

11 months ago

3.0.25

12 months ago

3.0.26

12 months ago

6.3.52

10 months ago

3.0.20

12 months ago

6.3.50

11 months ago

6.3.51

11 months ago

5.3.48

11 months ago

5.3.47

11 months ago

5.3.46

11 months ago

3.0.18

12 months ago

3.0.19

12 months ago

6.3.49

11 months ago

3.0.32

11 months ago

3.0.33

11 months ago

3.0.30

11 months ago

6.3.48

11 months ago

3.0.31

11 months ago

3.0.29

11 months ago

3.0.17

12 months ago

3.0.16

12 months ago

3.0.14

12 months ago

3.0.15

12 months ago

2.0.14

12 months ago

2.0.13

12 months ago

2.0.12

12 months ago

1.0.12

12 months ago

1.0.11

12 months ago

1.0.10

12 months ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago