Skip to content

Commit

Permalink
feat: support extends when parsing tsconfig.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
herberttn committed Mar 4, 2024
1 parent 6976499 commit 4ce7744
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname } from 'node:path';
import { basename, dirname } from 'node:path';
import { EOL } from 'node:os';

import { createFilter } from 'vite';
Expand Down Expand Up @@ -64,26 +64,31 @@ function prepareCompilerOptions(cache: Map<string, CompilerOptions>, file: strin
return cache.get(key) as CompilerOptions;
}

const compilerOptions = parseCompilerOptions(file, options);
cache.set(key, compilerOptions);

return compilerOptions;
}

function parseCompilerOptions(file: string, options?: Options): CompilerOptions {
const location = options?.tsconfig?.location
?? ts.findConfigFile(file, ts.sys.fileExists);

if (location) {
const parsed = ts.readConfigFile(location, ts.sys.readFile);

if (parsed.error) {
throw parsed.error;
}
if (!location) {
throw new Error(`Could not find TypeScript configuration for ${file}`);
}

const compilerOptions = {
...parsed.config.compilerOptions,
...options?.tsconfig?.override,
} satisfies CompilerOptions;
const { config: tsconfig, error } = ts.readConfigFile(location, ts.sys.readFile);

cache.set(key, compilerOptions);
return compilerOptions;
if (error) {
throw error;
}

throw new Error(`Could not find TypeScript configuration for ${file}`);
const directory = dirname(location);
const name = basename(location);
const parsed = ts.parseJsonConfigFileContent(tsconfig, ts.sys, directory, undefined, name);

return parsed.options;
}

export {
Expand Down

0 comments on commit 4ce7744

Please sign in to comment.