New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding "extends": support to 'ts-node'
loading from tsconfig.json
#1292
Conversation
a1b49fd
to
eac5065
Compare
Codecov Report
|
eac5065
to
8e771ff
Compare
…, so that one `tsconfig` extending another will merge the `'ts-node'` options specified in both. `readTsConfigExtendForTsNodeRecursively` will read as far back as it can while keeping the current stack around so it keeps merging. Higher stack gets priority.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made an initial review. This will also need tests.
const result = | ||
extendFile === undefined | ||
? filterRecognizedTsConfigTsNodeOptions(config[configKey]) | ||
: [extendFile] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: get rid of the filter().map()
chaining. We prefer code like const
, if()
, for(const a of b)
, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cspotcode It's not for everyone 🐼. Once you have a look over the logic itself, I'll swap it out.
src/configuration.ts
Outdated
: [extendFile] | ||
.filter((x) => x && typeof x === 'string') | ||
.map((x) => x as string) | ||
.map((extended) => resolve(cwd, extended)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work when ./bar/tsconfig.json refers to ./bar/tsconfig2.json by relative path? Because the path is not relative to cwd? Does it work for "extends": "@tsconfig/node14/tsconfig.json"
? (https://github.com/tsconfig/bases)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding relative paths...
Not sure if I want to say yes, I do use this myself and I my cwd wasn't relative, but then again I am doing other stuff that might impact it.
Good call, would do best to extract dirname/basename
from current config in order to make relative paths work - but I gotta check first.
There is another approach and that is to let ts
itself try to find the file, there is a function that for that last I checked.
Regarding @tsconfig/node14/tsconfig.json
Yeah no chance that would work.
I am not familiar with that, is that something you distribute or TS?
If it's the latter, then I'd reckon there wouldn't be a ts-node
key there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need tests for this, and we'll need to support typescript's "extends"
resolution behavior. @tsconfig/node14 was just an example; our users can choose to "extends"
from anywhere that typescript supports, and we'll need to match that behavior. Ideally, we figure out how TypeScript is doing this logic and do the exact same, using as many TypeScript APIs as possible to avoid duplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, if TS exposes that, then I guess best bet is to use it, but to reimplement it is overkill.
In general, I think the simplest approach is just following the links.
In regards to your previous points about relative paths, this is an easy fix:
.map(path => isAbsolute(path) ? [path] : [currentConfigPath, path])
.map(paths => resolve(paths)
However I don't care much for extends: "@tsconfig/node14/tsconfig.json"
, on the premise that I just don't approve of this path they're going down on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using relative paths and not CWD now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However I don't care much for extends: "@tsconfig/node14/tsconfig.json", on the premise that I just don't approve of this path they're going down on.
Unfortunately tsc already does this so we need to match. Otherwise it gets confusing and we need to document how our behavior deviates, and I'll be the one answering the issues that get files.
I certainly understand why you might choose not to use this feature on your projects, but for ts-node
, we need to match TSC's behavior or else we're making things confusing.
Correctomundo. You can see that by running What I am trying to do here is somewhat of a hack really, it's just recursively following the extends path and collects |
I looked at TypeScript's source code.
It internally makes this chain of function calls:
We need to mimic the resolution logic of
|
@cspotcode Sounds a bit like what I mentioned earlier It seems like a simplified version will just do the job fine - unless you want to go ahead and use all that. .map(path => isAbsolute(path) ? [path] : [currentConfigPath, path])
.map(paths => resolve(paths) |
Is this a cache that they offer while traversing the extend or something you had in mind to store? |
I'm looking at the |
@seivan do you plan to come back to this, or should I close it? |
@cspotcode I actually think your original idea of supporting a single env variable with all the options was better. I did this for my fork and thought it was enough but as I understand, you want the exact same inheritance support as Edit: To clarify, I also feel uneasy about non Feel free to close it. Sorry for the delay! |
@seivan Understood, no worries. |
So that one
tsconfig
extending another will merge the'ts-node'
options specified in both.readTsConfigExtendForTsNodeRecursively(cwd: string, ts, rawConfig)
will read as far as it can and merge with previous config.Each
extend
has lower priority than previous, so your original"ts-node"
values will not be overwritten by the nextextend
and so forth.Based on comment #1007 (comment)