Skip to content

Commit

Permalink
feat(tsconfig): support tsconfig.json files with comments
Browse files Browse the repository at this point in the history
Built-in JSON.parse() doesn't allow json with comments, but `tsconfig.json` files support comments.
By using `json5` instead of native JSON we prevent errors when trying to parse such json files with comments

Even though Typescript uses 'jsonc-parser' [here](https://github.com/microsoft/TypeScript/blob/5439c8111de50fa6ea239f95fc23c76dc868fc07/scripts/build/utils.mjs#L78-L81),
we can use json5, because it is a superset of jsonc syntax.
  • Loading branch information
miluoshi committed Aug 22, 2023
1 parent fe84f6a commit 7fa650a
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/parser/tsconfig.js
@@ -1,10 +1,12 @@
const requirePackageName = require('require-package-name');
const { readFileSync } = require('fs');
const JSON5 = require('json5');

export default function tsconfigParser(filePath, deps) {
const content = readFileSync(filePath, { encoding: 'utf8' });
const foundDeps = [];
const tsconfigJson = JSON.parse(content);
// Typescript uses 'jsonc-parser' to parse tsconfig.json, but json5 is a superset of jsonc syntax (JSON + js comments)
const tsconfigJson = JSON5.parse(content);
const types = tsconfigJson.compilerOptions?.types;
if (types) {
types.forEach((pkg) => {
Expand Down

0 comments on commit 7fa650a

Please sign in to comment.