Skip to content

Commit

Permalink
feat: use jsconfig.json if present
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
aleclarson committed Jan 16, 2024
1 parent dd3fc7e commit 353926a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 63 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
107 changes: 45 additions & 62 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 = {}
Expand Down Expand Up @@ -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'
},
})
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 353926a

Please sign in to comment.