Repository navigation
API for parsing *.json config files into a complete config objects #44573
Description
Suggestion
Dedicated public beginner-friendly API (ideally, an asynchronous function on the ts namespace) for parsing tsconfig.json / jsconfig.json files into a complete config object.
🔍 Search Terms
parse JSON config tsconfig tsconfig.json jsconfig.json
✅ Viability Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
The suggestion is to have a single function that in its simplest form (overload) would take a string, – path to the config file (either tsconfig.json or jsconfig.json, shouldn't matter), and return (either synchronously or asynchronously) the fully compiled complete configuration object with all the properties initialized (either explicitly from the provided config(s) or implicitly, from known defaults).
See Motivating Example below for why I can't use existing functions on
tsnamespace.
The suggestion is related to issue #44516 and this StackOverflow question.
📃 Motivating Example
I know that there are already a couple of functions on the imported ts namespace whose purpose is to read/parse/compile config, given its filepath/stringified contents/parsed contents. These include:
However, they either provide incomplete functionality (e.g., ts.readConfigFile is just a glorified JSON.parse, it doesn't – for example – crawl parent configs) or are ridiculously complicated to use (e.g., ts.parseConfigFileTextToJson require 3-9 arguments, the third of which is some kind of host object, that has to have a readDirectory method, that in turn requires 4-5 arguments, and has to actually do something, which I honestly don't know why I can't just use fs.readdirSync).
I need a function that takes a path to json and returns a complete config object.
💻 Use Cases
I'm thinking of a couple of variants of the function (for the sake of an example, it is called readConfig):
// given these
type Path = string | Buffer | URL;
interface Config {
include?: string[], exclude?: string[], files?: string[], compilerOptions: { /*...*/ }, /* other properties of *.json config files */
}/**
* Find file by path, parse it, extend parent config objects, fill in missing defaults, return the resulting config object
* If an error occurs, throw it
*/
/* 1 */ function readConfigSync(path: Path): Config;/**
* Find file by path, parse it, extend parent config objects, fill in missing defaults, invoke `callback(null, <resulting config object>)`
* If an error occurs, invoke `callback(<error>)`
*/
/* 2 */ function readConfig(path: Path, callback: (...args: [ err: unknown ] | [ err: null, config: Config ]) => void): void;/**
* Find file by path, parse it, extend parent config objects, fill in missing defaults, return promise that resolves to the default config object
* If an error occurs, return promise that rejects with the error
*/
/* 3 */ function readConfig(path: Path): Promise<Config>;/**
* Return the default config object
* If an error occurs, throw it
*/
/* 4 */ function readConfigSync(): Config;/**
* Invoke `callback(null, <default config object>)`
* If an error occurs, invoke `callback(<error>)`
*/
/* 5 */ function readConfig(callback: (...args: [ err: unknown ] | [ err: null, config: Config ]) => void): void;/**
* Return promise that resolves to the default config object
* If an error occurs, return promise that rejects with the error
*/
/* 6 */ function readConfig(): Promise<Config>;Also, shout out to functions that return { config?: Config; error?: unknown }, which, however, are not idiomatic to Node.JS.
Activity
RyanCavanaugh commentedon Jun 15, 2021
Just a heads up, fundamentally, you can't do this without a level of complexity approximately on par with what is already out there. tsconfig files can have
extendsclauses that can refer to both filenames and module names, and resolving a module name to a file requires being able to probe for files. A similar thing is true fortypes- the default behavior here involves directory enumeration.You need a
hostto provide this functionality because all our APIs are designed in a way that allow them to be completely decoupled from the physical file system (for scenarios like running in a browser)parzhitsky commentedon Jun 15, 2021
I see. Well, crap. The demand is clearly there, but looks like it is incompatible with current design of TypeScript.
And we cannot even use configurable defaults (e.g., use
fs.readdirwhen applicable, otherwise ask for it to be provided, otherwise produce error), can we?Is there any chance then to at least have a 2021 version of this snippet from 2016 by Mohamed Hegazy (@mhegazy)?
RyanCavanaugh commentedon Jun 15, 2021
That sample still works today with surprisingly few tweaks:
andrewbranch commentedon Jun 28, 2021
Dmytro Parzhytskyi (@parzhitsky) I would suggest looking at https://www.npmjs.com/package/@ts-morph/bootstrap. TypeScript needs a high level of flexibility and abstractness in its API so it can run anywhere, but nothing prevents someone from wrapping it with Node.js assumptions like @ts-morph/bootstrap.
dominikg commentedon Sep 15, 2021
some of you may be interested in tsconfck
It also offers a similar
parseNativefunction, which uses the typescript functions mentioned aboveAnd last but not least a cli wrapper for simple checks
# print tsconfig for foo/bar.ts to stdout npx tsconfck parse foo/bar.tsfix(babel-config): read TS config files using TS parser functions (#9121
fix(babel-config): read TS config files using TS parser functions (#9121