-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
Support comments in tsconfig.json #7248
Conversation
I'm not sure this is the right solution, as we also need to handle jsconfig.json. We probably don't want to have a hard dependency on typescript, so we already need to solve jsconfig parsing without it, and then whatever we use for that can also be used for ts. I wonder exactly how nuanced the parsing is? Related to #6865 |
I think the solution is probably to read the files, instead of using require, and then parsing them. Perhaps we need a library to help here. @crosscompile, TypeScript would only be installed when the project is TypeScript, so this solution wouldn't work for |
I adapted this approach from I think a question is whether we want to use a single custom parser for Is there a downside to using |
@crosscompile, I think what we're saying is that we would like a single approach for both. We could:
The problem with "c" is that TypeScript would need to be installed at all times, and it's now only added for TS projects. |
Ah got it, yes those seem like the three options. After doing a little research a few thoughts came up for each option:
In addition to parsing the JSON we also have to resolve the
I tracked down the
As an alternative to always installing I also had not used
|
Correct, but you can use We also look at With option A, we could use this: https://www.npmjs.com/package/comment-json. So, we'd read the file (sync) and then process with this to get the JSON we need. That should solve the issue you've reported, right? |
Cool, yep that approach would solve the issue with comments in my config. One issue I ran into when trying out comment-json is that it doesn't handle trailing commas, which some CRA users have run into (#6865). Maybe there is an alternative library that handles trailing commas.
|
Honestly, I feel that JSON with comments should have had a mandatory file extension (like JSONC) as many tools aren't going to support it... Your concerns about extends are valid. I think that's a good case to consider installing TypeScript as a part of |
@mrmckeb My gut feeling is that adding a dependency on typescript is a bad choice. I'm just not sure how well multiple versions of typescript in the dependency tree play together, and I think pinning CRA to a particular version would be a mistake. But maybe it's fine? Ideally we could rip out just the code we need, but I'm not sure anyone wants to go through the trouble or take on the maintenance burden. We could use typescript if it's installed, but fall back to using This:
It's not a perfect solution, which maybe isn't the CRA way, but I can write the PR if you think it's a good path forward. |
Having multiple Typescript versions in the node_modules tree isn't a problem, I have used that setup in multiple projects. However, pinning CRA to a particular Typescript version would be problematic though as Typescript does not follow semver and Typescript version updates inside CRA can become a hassle. With a big project like CRA having this problem, maybe the TS team can be persuaded to split "readConfigFile" into its own package that both TS and CRA can depend upon. However while neither bundling Typescript (but still allowing override) and @heyimalex "switching parser" solutions is perfect, I think both is workable and the caveats are only inconvenient. |
Thanks @heyimalex, and good thoughts. I've spoken to @ianschmitz about pinning TS before, as we know that a few of the dependencies (such as the ESLint parser) also pin/restrict versions... so that's why it might be an acceptable option. And as @Pajn said it should be fine. We'd need to test a little. I actually also think that asking the TS team to split out the reader isn't a bad idea - it seems like something that should exist (JSON reader with comment support) given that so many people are using JSONC now. That being said, it seems that Microsoft publishes this already - https://www.npmjs.com/package/jsonc-parser Any thoughts @ianschmitz? |
I like the idea of adding Since CRA would be using the I also think it is very unlikely that If the TS team is open to splitting out |
OK, let's go with that approach @crosscompile. I'll approve, but I need a second approval. @iansu might be able to take a look. |
Sorry @crosscompile, I just realised that this doesn't cover the If not, let me know and I can do it. Still chasing a second approval on this ;) |
@mrmckeb yep I can definitely make that change. Do you also want me to add the |
@@ -71,7 +72,10 @@ function getModules() { | |||
// TypeScript project and set up the config | |||
// based on tsconfig.json | |||
if (hasTsConfig) { | |||
config = require(paths.appTsConfig); | |||
const ts = require(resolve.sync('typescript', { |
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.
Why are you using resolve.sync
here?
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.
I saw this approach being used in verifyTypeScriptSetup
and thought that there must be a reason require
isn't used directly.
I'm not 100% certain but it seems like it might have to do with how appDirectory
is setup to resolve symlinks?
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.
I be leave this is necessary to support yarn pnp. As typescript, isn't a dependency of CRA, yarn would not let CRA require it directly. However, by resolving it from the app directory (which do have a dependency on typescript), CRA can still access it.
Hey @crosscompile, I was talking to @iansu and we think we'll merge this in with TypeScript-only support, and consider how to support |
Since tsconfig files allow comments, using
require
for atsonfig.json
with comments results in the following syntax error:This PR uses
typescript
'sreadConfigFile
which handles comments.