Skip to content
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

Add support for JSONC project configuration files #13

Closed
wants to merge 1 commit into from

Conversation

Xunnamius
Copy link

Uses strip-json-comments to load TSConfig files instead of require. There are no unit tests, but it works across my repos (including my monorepos).

Closes #10

@jonkwheeler
Copy link
Owner

Sick Let me test this on my repos. If it works, I'll merge asap.

@jonkwheeler
Copy link
Owner

jonkwheeler commented Oct 20, 2021

I've spent about an hour+ trying to get this change to work in my local, and it just won't do it. Give me more time and I'll def try and figure out why. Unsure how you're testing, but I'm doing yarn build and yarn link. I've ran into about 10 different errors. It might be yarn link's fault to be honest.

Edit: this is bugging the shit out of me.

@Xunnamius
Copy link
Author

Xunnamius commented Oct 20, 2021

@jonkwheeler Hmm, this might be my fault: I used NPM (instead of Yarn) to install stuff, and I did notice it touched the yarn.lock file. Maybe have to regenerate the lockfile?

Btw, I'm testing by installing directly from GitHub: npm install --save-dev github:xunnamius/tsconfig-replace-paths. In the fork repo, I committed dist/ (the result of npm run build:cjs and npm run build:esm), which is why direct installation is possible.

@Xunnamius
Copy link
Author

Xunnamius commented Oct 20, 2021

Currently using that fork in this monorepo which has a tsconfig.json file with comments.

@vic1707
Copy link

vic1707 commented Oct 20, 2021

hello,
It seems that this should work (avoid adding a dependency)

JSON.parse(
    readFileSync(file).toString('utf8')
      .replace(/\/\*.*?\*\//g, '')
      .replace(/\/\/.*/g, '')
      .replace(/\/\*(.|\n)*?\*\//g, '')
) as IRawTSConfig;

It worked with my TSconfig even when adding comments like so it should be the same result as the one from the dependency :

{
    "t": /* hello */ true,
     /*
     * really big
     * comment
     */
}

Here are a tsconfig and the output of those regex replace.
tsconfig.bak.txt
transformed.txt

@Xunnamius
Copy link
Author

Xunnamius commented Oct 20, 2021

@vic1707 Personally, I prefer the battle-tested dependency (33,000,000 weekly downloads) over an adhoc solution. Plus, looking at strip-json-comments's source, it looks like replacement is a bit hairier than what a regex replace can handle. strip-json-comments also replaces the comments with whitespace, which conveniently preserves error positions in stack traces when JSON.parse fails.

@vic1707
Copy link

vic1707 commented Oct 20, 2021

@vic1707 Personally, I prefer the battle-tested dependency (33,000,000 weekly downloads) over an adhoc solution. Plus, looking at strip-json-comments's source, it looks like replacement is a bit hairier than what a regex replace can handle. strip-json-comments also replaces the comments with whitespace, which conveniently preserves error positions in stack traces when JSON.parse fails.

Understandable, I was just proposing the regex solution in case @jonkwheeler wasn't able to solve his issue 😉. Also because I prefer to rely the least on external dependencies (maybe not the Best practice ? Feel free to tell me I'm quite new to JS/TS outside of ANGULAR)

As for error positions (I'm assuming that an error could be thrown while parsing the JSON) I replaced all the whitespaces from the transformed file myself. I wanted to show a more concise file (the three replaces from the code leave all the space taken by the comment blank).

@jonkwheeler
Copy link
Owner

Just to chime in, this morning I even copy pasta'd the file from strip-json-comments straight into the repo and imported that and still had a bunch of errors. Again, apologies for the delay on this... shit's broke and I need to figure out why.

@Xunnamius
Copy link
Author

Xunnamius commented Oct 21, 2021

Also because I prefer to rely the least on external dependencies (maybe not the Best practice ? Feel free to tell me I'm quite new to JS/TS outside of ANGULAR)

I'm usually of the same opinion, but when I'm dealing exclusively with tooling (rather than something that's bundled and pushed to the browser), I don't mind extra deps 😄

Just to chime in, this morning I even copy pasta'd the file from strip-json-comments straight into the repo and imported that and still had a bunch of errors. Again, apologies for the delay on this... shit's broke and I need to figure out why.

No rush! Thanks for your work on this.

@jonkwheeler
Copy link
Owner

See: #15

@jonkwheeler
Copy link
Owner

Just had a PR Party. You guys can go nuts now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow using tsconfig.json with comments and tailing commas
3 participants