From 353926a230500fe2bc1b36a53c786640593b0ad0 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:22:49 -0500 Subject: [PATCH] feat: use jsconfig.json if present Closes #22 --- package.json | 2 +- src/index.ts | 107 ++++++++++++++++++++++----------------------------- src/types.ts | 5 +++ 3 files changed, 51 insertions(+), 63 deletions(-) diff --git a/package.json b/package.json index e8972ca..30fa249 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "dependencies": { "debug": "^4.1.1", "globrex": "^0.1.2", - "tsconfck": "^2.1.0" + "tsconfck": "^3.0.1" }, "devDependencies": { "@alloc/fast-rimraf": "^1.0.8", diff --git a/src/index.ts b/src/index.ts index c7e457d..6e91a4a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,11 +51,19 @@ export default (opts: PluginOptions = {}): Plugin => { workspaceRoot = root } - const projects = await resolveProjectPaths( - opts.projects, - projectRoot, - workspaceRoot - ) + const projects = opts.projects + ? opts.projects.map((file) => { + if (!file.endsWith('.json')) { + file = join(file, 'tsconfig.json') + } + return resolve(projectRoot, file) + }) + : await tsconfck.findAll(workspaceRoot, { + configNames: opts.configNames || ['tsconfig.json', 'jsconfig.json'], + skip(dir) { + return dir == 'node_modules' || dir == '.git' + }, + }) debug('projects:', projects) @@ -78,48 +86,43 @@ export default (opts: PluginOptions = {}): Plugin => { let firstError: any - const parseOptions = { - cache: new Map(), - resolveWithEmptyIfConfigNotFound: true, - } satisfies import('tsconfck').TSConfckParseOptions + const parseOptions: tsconfck.TSConfckParseOptions = { + cache: new tsconfck.TSConfckCache(), + } const parsedProjects = new Set( - ( - await Promise.all( - projects.map((tsconfigFile) => - (hasTypeScriptDep + await Promise.all( + projects.map((tsconfigFile) => { + if (tsconfigFile === null) { + debug('tsconfig file not found:', tsconfigFile) + return null + } + return ( + 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 + ).catch((error) => { + if (opts.ignoreConfigErrors) { + debug('tsconfig file caused a parsing error:', tsconfigFile) + } else { + 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) } - return null - }) - ) - ) - ).filter((project, i) => { - if (!project) { - return false - } - if (project.tsconfigFile !== 'no_tsconfig_file_found') { - return true - } - debug('tsconfig file not found:', projects[i]) - return false - }) + firstError = error + } + return null + }) + }) + ) ) resolversByDir = {} @@ -399,23 +402,3 @@ function compileGlob(glob: string) { globstar: true, }).regex } - -function resolveProjectPaths( - projects: string[] | undefined, - projectRoot: string, - workspaceRoot: string -) { - if (projects) { - return projects.map((file) => { - if (!file.endsWith('.json')) { - file = join(file, 'tsconfig.json') - } - return resolve(projectRoot, file) - }) - } - return tsconfck.findAll(workspaceRoot, { - skip(dir) { - return dir == 'node_modules' || dir == '.git' - }, - }) -} diff --git a/src/types.ts b/src/types.ts index 9a6d0d9..4810d2d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -42,6 +42,11 @@ export interface PluginOptions { * Silence the warning about malformed `tsconfig.json` files. */ ignoreConfigErrors?: boolean + /** + * An array of `tsconfig.json` file names to search for. + * @default ["tsconfig.json", "jsconfig.json"] + */ + configNames?: string[] } export interface TSConfig {