Skip to content

Commit

Permalink
fix: Support explicit and implicit tsconfig from packages
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed May 16, 2022
1 parent 3e5a01a commit 1ea1327
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,28 @@ const resolveTsConfigFromFile = (cwd: string, filename: string) => {
const resolveTsConfigFromExtends = (cwd: string, name: string) => {
if (path.isAbsolute(name)) return fs.existsSync(name) ? name : null
if (name.startsWith(".")) return findUp(name, cwd)
const id = req.resolve(name, { paths: [cwd] })
let id: string | null = null
try {
id = req.resolve(name, { paths: [cwd] })
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "MODULE_NOT_FOUND") {
// config package did _not_ use pkg.main field
const pkgJsonPath = req.resolve(path.join(name, "package.json"), {
paths: [cwd],
})
const pkgManifest = req(pkgJsonPath) as { tsconfig?: string }
// use explicit pkg.tsconfig or implicit "index" {pkgroot}/tsconfig.json
id = req.resolve(
path.join(
name,
pkgManifest.tsconfig ? pkgManifest.tsconfig : "tsconfig.json",
),
{ paths: [cwd] },
)
} else {
throw error
}
}
return id
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/extends-package-explicit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "tsconfig-pkg-explicit"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/extends-package-implicit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "tsconfig-pkg-implicit"
}
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ test("extends package", () => {
])
})

test("extends package with explicit tsconfig", () => {
const loaded = loadTsConfig(fixture("extends-package-explicit"))
expect(loaded?.data.files).toEqual(["explicit"])
})

test("extends package with implicit tsconfig", () => {
const loaded = loadTsConfig(fixture("extends-package-implicit"))
expect(loaded?.data.files).toEqual(["implicit"])
})

test("find nearest file", () => {
expect(loadTsConfig(fixture("find-nearest/nested/dir"))).not.toBe(null)
})

0 comments on commit 1ea1327

Please sign in to comment.