From 7a97869db40bf42924856bf586b615cb3003dbc7 Mon Sep 17 00:00:00 2001 From: Julien Goux Date: Wed, 12 Apr 2023 16:56:15 +0200 Subject: [PATCH] feat: add `ignoreConfigErrors` option (#109) Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com> --- README.md | 3 +++ src/index.ts | 25 ++++++++++++++++++++++++- src/types.ts | 4 ++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f4d6b4b..b0f763e 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,9 @@ Give [`vite`] the ability to resolve imports using TypeScript's path mapping. 600ms, due to the size of the TypeScript compiler. Only use it when necessary. +- `ignoreConfigErrors: boolean` + When true, parsing errors encountered while loading tsconfig files will be ignored. This is useful if you have a monorepo with multiple tsconfig files, and you don't want to see errors for the ones that aren't relevant to the current project. +   ### allowJs diff --git a/src/index.ts b/src/index.ts index af689a7..7b632a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,6 +76,8 @@ export default (opts: PluginOptions = {}): Plugin => { } } + let firstError: any + const parseOptions = { resolveWithEmptyIfConfigNotFound: true, } satisfies import('tsconfck').TSConfckParseOptions @@ -84,12 +86,33 @@ export default (opts: PluginOptions = {}): Plugin => { ( await Promise.all( projects.map((tsconfigFile) => - hasTypeScriptDep + (hasTypeScriptDep ? tsconfck.parseNative(tsconfigFile, parseOptions) : tsconfck.parse(tsconfigFile, parseOptions) + ).catch((error) => { + if (!opts.ignoreConfigErrors) { + config.logger.error( + '[tsconfig-paths] An error occurred while parsing "' + + tsconfigFile + + '". See below for details.' + + (firstError + ? '' + : ' To disable this message, set the `ignoreConfigErrors` option to true.'), + { error } + ) + if (config.logger.hasErrorLogged(error)) { + console.error(error) + } + firstError = error + } + return null + }) ) ) ).filter((project, i) => { + if (!project) { + return false + } if (project.tsconfigFile !== 'no_tsconfig_file_found') { return true } diff --git a/src/types.ts b/src/types.ts index bdf4b9e..9a6d0d9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -38,6 +38,10 @@ export interface PluginOptions { * necessary. */ parseNative?: boolean + /** + * Silence the warning about malformed `tsconfig.json` files. + */ + ignoreConfigErrors?: boolean } export interface TSConfig {