Skip to content

Commit

Permalink
fix: Support subpaths of extended packages
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed May 16, 2022
1 parent 1ea1327 commit b51ad02
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,30 @@ const resolveTsConfigFromExtends = (cwd: string, name: string) => {
} 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] },
)
const nameParts = name.split("/")
if (
(name.startsWith("@") && nameParts.length === 2) ||
nameParts.length === 1
) {
// a module (with optional scope) lacking subpath
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 {
// a subpath directory, all others are handled in try block
id = req.resolve([...nameParts, "tsconfig.json"].join("/"), {
paths: [cwd],
})
}
} else {
throw error
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/extends-package-explicit/no-extn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "tsconfig-pkg-explicit/other"
}

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/subpath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "tsconfig-pkg-explicit/other.json"
}
3 changes: 3 additions & 0 deletions test/fixtures/extends-package-implicit/no-extn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "tsconfig-pkg-implicit/other"
}

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.

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

32 changes: 32 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,43 @@ test("extends package with explicit tsconfig", () => {
expect(loaded?.data.files).toEqual(["explicit"])
})

test("extends package subpath file without extension", () => {
const loaded = loadTsConfig(
fixture("extends-package-explicit"),
"no-extn.json",
)
expect(loaded?.data.files).toEqual(["other-file"])
})

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

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

test("extends package with implicit subpath directory", () => {
const loaded = loadTsConfig(
fixture("extends-package-implicit"),
"no-extn.json",
)
expect(loaded?.data.files).toEqual(["other-directory"])
})

test("extends package with implicit file subpath", () => {
const loaded = loadTsConfig(
fixture("extends-package-implicit"),
"subpath.json",
)
expect(loaded?.data.files).toEqual(["other-directory-config"])
})

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

0 comments on commit b51ad02

Please sign in to comment.