Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support additional extends package patterns #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,40 @@ 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 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
}
}
return id
}

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.

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/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-explicit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "tsconfig-pkg-explicit"
}
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.

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"
}

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"
}
42 changes: 42 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,48 @@ 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 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)
})