Skip to content

Commit 8d2ec54

Browse files
fix(cli): parse tsconfig as jsonc
1 parent 5b3a50f commit 8d2ec54

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import fs from 'node:fs';
2+
import os from 'node:os';
3+
import path from 'node:path';
4+
5+
import { afterEach, describe, expect, it } from 'vitest';
6+
7+
import { hasBaseUrlInTsconfig } from '../utils/tsconfig.js';
8+
9+
const tempDirs: string[] = [];
10+
11+
function createTempDir() {
12+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-tsconfig-'));
13+
tempDirs.push(dir);
14+
return dir;
15+
}
16+
17+
afterEach(() => {
18+
for (const dir of tempDirs.splice(0, tempDirs.length)) {
19+
fs.rmSync(dir, { recursive: true, force: true });
20+
}
21+
});
22+
23+
describe('hasBaseUrlInTsconfig', () => {
24+
it('detects baseUrl in JSONC tsconfig files', () => {
25+
const projectPath = createTempDir();
26+
fs.writeFileSync(
27+
path.join(projectPath, 'tsconfig.json'),
28+
`{
29+
"compilerOptions": {
30+
// Laravel starter tsconfig files commonly keep generated comments.
31+
"moduleResolution": "bundler",
32+
"baseUrl": ".",
33+
}
34+
}
35+
`,
36+
);
37+
38+
expect(hasBaseUrlInTsconfig(projectPath)).toBe(true);
39+
});
40+
41+
it('returns false when baseUrl is only present in a comment', () => {
42+
const projectPath = createTempDir();
43+
fs.writeFileSync(
44+
path.join(projectPath, 'tsconfig.json'),
45+
`{
46+
"compilerOptions": {
47+
// "baseUrl": ".",
48+
"moduleResolution": "bundler"
49+
}
50+
}
51+
`,
52+
);
53+
54+
expect(hasBaseUrlInTsconfig(projectPath)).toBe(false);
55+
});
56+
});

packages/cli/src/utils/tsconfig.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import { applyEdits, modify, parse as parseJsonc } from 'jsonc-parser';
1010
*/
1111
export function hasBaseUrlInTsconfig(projectPath: string): boolean {
1212
try {
13-
const tsconfig = JSON.parse(
13+
const tsconfig = parseJsonc(
1414
fs.readFileSync(path.join(projectPath, 'tsconfig.json'), 'utf-8'),
15-
) as { compilerOptions?: { baseUrl?: string } };
15+
) as {
16+
compilerOptions?: { baseUrl?: string };
17+
};
1618
return tsconfig?.compilerOptions?.baseUrl !== undefined;
1719
} catch {
1820
return false;

0 commit comments

Comments
 (0)