Skip to content

Commit b51ad02

Browse files
committed
fix: Support subpaths of extended packages
See microsoft/TypeScript#27348
1 parent 1ea1327 commit b51ad02

9 files changed

Lines changed: 78 additions & 12 deletions

File tree

src/index.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,30 @@ const resolveTsConfigFromExtends = (cwd: string, name: string) => {
3737
} catch (error) {
3838
if ((error as NodeJS.ErrnoException).code === "MODULE_NOT_FOUND") {
3939
// config package did _not_ use pkg.main field
40-
const pkgJsonPath = req.resolve(path.join(name, "package.json"), {
41-
paths: [cwd],
42-
})
43-
const pkgManifest = req(pkgJsonPath) as { tsconfig?: string }
44-
// use explicit pkg.tsconfig or implicit "index" {pkgroot}/tsconfig.json
45-
id = req.resolve(
46-
path.join(
47-
name,
48-
pkgManifest.tsconfig ? pkgManifest.tsconfig : "tsconfig.json",
49-
),
50-
{ paths: [cwd] },
51-
)
40+
const nameParts = name.split("/")
41+
if (
42+
(name.startsWith("@") && nameParts.length === 2) ||
43+
nameParts.length === 1
44+
) {
45+
// a module (with optional scope) lacking subpath
46+
const pkgJsonPath = req.resolve(path.join(name, "package.json"), {
47+
paths: [cwd],
48+
})
49+
const pkgManifest = req(pkgJsonPath) as { tsconfig?: string }
50+
// use explicit pkg.tsconfig or implicit "index" {pkgroot}/tsconfig.json
51+
id = req.resolve(
52+
path.join(
53+
name,
54+
pkgManifest.tsconfig ? pkgManifest.tsconfig : "tsconfig.json",
55+
),
56+
{ paths: [cwd] },
57+
)
58+
} else {
59+
// a subpath directory, all others are handled in try block
60+
id = req.resolve([...nameParts, "tsconfig.json"].join("/"), {
61+
paths: [cwd],
62+
})
63+
}
5264
} else {
5365
throw error
5466
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "tsconfig-pkg-explicit/other"
3+
}

test/fixtures/extends-package-explicit/node_modules/tsconfig-pkg-explicit/other.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "tsconfig-pkg-explicit/other.json"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "tsconfig-pkg-implicit/other"
3+
}

test/fixtures/extends-package-implicit/node_modules/tsconfig-pkg-implicit/other/config.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/extends-package-implicit/node_modules/tsconfig-pkg-implicit/other/tsconfig.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "tsconfig-pkg-implicit/other/config"
3+
}
4+

test/index.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,43 @@ test("extends package with explicit tsconfig", () => {
3131
expect(loaded?.data.files).toEqual(["explicit"])
3232
})
3333

34+
test("extends package subpath file without extension", () => {
35+
const loaded = loadTsConfig(
36+
fixture("extends-package-explicit"),
37+
"no-extn.json",
38+
)
39+
expect(loaded?.data.files).toEqual(["other-file"])
40+
})
41+
42+
test("extends package subpath file with extension", () => {
43+
const loaded = loadTsConfig(
44+
fixture("extends-package-explicit"),
45+
"subpath.json",
46+
)
47+
expect(loaded?.data.files).toEqual(["other-file"])
48+
})
49+
3450
test("extends package with implicit tsconfig", () => {
3551
const loaded = loadTsConfig(fixture("extends-package-implicit"))
3652
expect(loaded?.data.files).toEqual(["implicit"])
3753
})
3854

55+
test("extends package with implicit subpath directory", () => {
56+
const loaded = loadTsConfig(
57+
fixture("extends-package-implicit"),
58+
"no-extn.json",
59+
)
60+
expect(loaded?.data.files).toEqual(["other-directory"])
61+
})
62+
63+
test("extends package with implicit file subpath", () => {
64+
const loaded = loadTsConfig(
65+
fixture("extends-package-implicit"),
66+
"subpath.json",
67+
)
68+
expect(loaded?.data.files).toEqual(["other-directory-config"])
69+
})
70+
3971
test("find nearest file", () => {
4072
expect(loadTsConfig(fixture("find-nearest/nested/dir"))).not.toBe(null)
4173
})

0 commit comments

Comments
 (0)