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

Support .d.cts types #987

Merged
merged 2 commits into from
Jan 6, 2025
Merged
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
43 changes: 30 additions & 13 deletions server/build_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,20 @@ func (ctx *BuildContext) resolveEntry(esm EsmPath) (entry BuildEntry) {
entry.types = stripModuleExt(entry.main) + ".d.mts"
} else if entry.main != "" && ctx.existsPkgFile(stripModuleExt(entry.main)+".d.ts") {
entry.types = stripModuleExt(entry.main) + ".d.ts"
} else if entry.main != "" && ctx.existsPkgFile(stripModuleExt(entry.main)+".d.cts") {
entry.types = stripModuleExt(entry.main) + ".d.cts"
} else if ctx.existsPkgFile(subModuleName + ".d.mts") {
entry.types = "./" + subModuleName + ".d.mts"
} else if ctx.existsPkgFile(subModuleName + ".d.ts") {
entry.types = "./" + subModuleName + ".d.ts"
} else if ctx.existsPkgFile(subModuleName + ".d.cts") {
entry.types = "./" + subModuleName + ".d.cts"
} else if ctx.existsPkgFile(subModuleName, "index.d.mts") {
entry.types = "./" + subModuleName + "/index.d.mts"
} else if ctx.existsPkgFile(subModuleName, "index.d.ts") {
entry.types = "./" + subModuleName + "/index.d.ts"
} else if ctx.existsPkgFile(subModuleName, "index.d.cts") {
entry.types = "./" + subModuleName + "/index.d.cts"
}
}
} else {
Expand Down Expand Up @@ -289,6 +295,8 @@ func (ctx *BuildContext) resolveEntry(esm EsmPath) (entry BuildEntry) {
entry.types = "./index.d.mts"
} else if ctx.existsPkgFile("index.d.ts") {
entry.types = "./index.d.ts"
} else if ctx.existsPkgFile("index.d.cts") {
entry.types = "./index.d.cts"
}
}

Expand All @@ -297,12 +305,16 @@ func (ctx *BuildContext) resolveEntry(esm EsmPath) (entry BuildEntry) {
entry.types = stripModuleExt(entry.main) + ".d.mts"
} else if ctx.existsPkgFile(stripModuleExt(entry.main) + ".d.ts") {
entry.types = stripModuleExt(entry.main) + ".d.ts"
} else if ctx.existsPkgFile(stripModuleExt(entry.main) + ".d.cts") {
entry.types = stripModuleExt(entry.main) + ".d.cts"
} else if stripModuleExt(path.Base(entry.main)) == "index" {
dir, _ := utils.SplitByLastByte(entry.main, '/')
if ctx.existsPkgFile(dir, "index.d.mts") {
entry.types = dir + "/index.d.mts"
} else if ctx.existsPkgFile(dir, "index.d.ts") {
entry.types = dir + "/index.d.ts"
} else if ctx.existsPkgFile(dir, "index.d.cts") {
entry.types = dir + "/index.d.cts"
}
}
}
Expand Down Expand Up @@ -424,34 +436,39 @@ func (ctx *BuildContext) finalizeBuildEntry(entry *BuildEntry) {
if entry.types != "" {
entry.types = normalizeEntryPath(entry.types)
if endsWith(entry.types, ".js", ".mjs", ".cjs") {
maybeDts := stripModuleExt(entry.types) + ".d.mts"
if ctx.existsPkgFile(maybeDts) {
entry.types = maybeDts
bearName := stripModuleExt(entry.types)
if ctx.existsPkgFile(bearName + ".d.mts") {
entry.types += ".d.mts"
} else if ctx.existsPkgFile(bearName + ".d.ts") {
entry.types += ".d.ts"
} else if ctx.existsPkgFile(bearName + ".d.cts") {
entry.types += ".d.cts"
} else {
maybeDts = stripModuleExt(entry.types) + ".d.ts"
if ctx.existsPkgFile(maybeDts) {
entry.types = maybeDts
} else {
entry.types = ""
}
entry.types = ""
}
} else if strings.HasSuffix(entry.types, ".d") {
if ctx.existsPkgFile(entry.types + ".mts") {
entry.types += ".mts"
} else if ctx.existsPkgFile(entry.types + ".ts") {
entry.types += ".ts"
} else if ctx.existsPkgFile(entry.types + ".cts") {
entry.types += ".cts"
} else {
entry.types = ""
}
} else if !endsWith(entry.types, ".d.ts", ".d.mts") {
} else if !endsWith(entry.types, ".d.ts", ".d.mts", ".d.cts") {
if ctx.existsPkgFile(entry.types + ".d.mts") {
entry.types = entry.types + ".d.mts"
} else if ctx.existsPkgFile(entry.types + ".d.ts") {
entry.types = entry.types + ".d.ts"
} else if ctx.existsPkgFile(entry.types + ".d.cts") {
entry.types = entry.types + ".d.cts"
} else if ctx.existsPkgFile(entry.types, "index.d.mts") {
entry.types = entry.types + "/index.d.mts"
} else if ctx.existsPkgFile(entry.types, "index.d.ts") {
entry.types = entry.types + "/index.d.ts"
} else if ctx.existsPkgFile(entry.types, "index.d.cts") {
entry.types = entry.types + "/index.d.cts"
} else {
entry.types = ""
}
Expand All @@ -468,7 +485,7 @@ func (ctx *BuildContext) resolveConditionExportEntry(conditions JSONObject, pref
condition, ok := conditions.Get(conditionName)
if ok {
if s, ok := condition.(string); ok {
if entry.types == "" || endsWith(s, ".d.ts", ".d.mts", ".d") {
if entry.types == "" || endsWith(s, ".d.ts", ".d.mts", ".d.cts", ".d") {
entry.types = s
}
} else if obj, ok := condition.(JSONObject); ok {
Expand Down Expand Up @@ -1090,15 +1107,15 @@ func (ctx *BuildContext) analyzeSplitting() (err error) {
exportAll := false
for _, exportName := range ctx.pkgJson.Exports.keys {
exportName := stripEntryModuleExt(exportName)
if (exportName == "." || strings.HasPrefix(exportName, "./")) && !endsWith(exportName, ".json", ".css", ".wasm", ".d.ts", ".d.mts") {
if (exportName == "." || strings.HasPrefix(exportName, "./")) && !endsWith(exportName, ".json", ".css", ".wasm", ".d.ts", ".d.mts", ".d.cts") {
if exportName == "./*" {
exportAll = true
break
}
if !strings.ContainsRune(exportName, '*') {
v := ctx.pkgJson.Exports.values[exportName]
if s, ok := v.(string); ok {
if endsWith(s, ".json", ".css", ".wasm", ".d.ts", ".d.mts") {
if endsWith(s, ".json", ".css", ".wasm", ".d.ts", ".d.mts", ".d.cts") {
continue
}
} else if obj, ok := v.(JSONObject); ok {
Expand Down
10 changes: 8 additions & 2 deletions server/dts_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func transformDTS(ctx *BuildContext, dts string, buildArgsPrefix string, marker

if isRelPathSpecifier(specifier) {
specifier = strings.TrimSuffix(specifier, ".d")
if !endsWith(specifier, ".d.ts", ".d.mts") {
if !endsWith(specifier, ".d.ts", ".d.mts", ".d.cts") {
var p PackageJSONRaw
var hasTypes bool
if utils.ParseJSONFile(path.Join(dtsWd, specifier, "package.json"), &p) == nil {
Expand All @@ -97,22 +97,28 @@ func transformDTS(ctx *BuildContext, dts string, buildArgsPrefix string, marker
specifier = specifier + ".d.mts"
} else if existsFile(path.Join(dtsWd, specifier+".d.ts")) {
specifier = specifier + ".d.ts"
} else if existsFile(path.Join(dtsWd, specifier+".d.cts")) {
specifier = specifier + ".d.cts"
} else if existsFile(path.Join(dtsWd, specifier, "index.d.mts")) {
specifier = strings.TrimSuffix(specifier, "/") + "/index.d.mts"
} else if existsFile(path.Join(dtsWd, specifier, "index.d.ts")) {
specifier = strings.TrimSuffix(specifier, "/") + "/index.d.ts"
} else if existsFile(path.Join(dtsWd, specifier, "index.d.cts")) {
specifier = strings.TrimSuffix(specifier, "/") + "/index.d.cts"
} else if endsWith(specifier, ".js", ".mjs", ".cjs") {
specifier = stripModuleExt(specifier)
if existsFile(path.Join(dtsWd, specifier+".d.mts")) {
specifier = specifier + ".d.mts"
} else if existsFile(path.Join(dtsWd, specifier+".d.ts")) {
specifier = specifier + ".d.ts"
} else if existsFile(path.Join(dtsWd, specifier+".d.cts")) {
specifier = specifier + ".d.cts"
}
}
}
}

if endsWith(specifier, ".d.ts", ".d.mts") {
if endsWith(specifier, ".d.ts", ".d.mts", ".d.cts") {
internalDts.Add(specifier)
} else {
specifier += ".d.ts"
Expand Down
6 changes: 3 additions & 3 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,12 +882,12 @@ func esmRouter() rex.Handle {
types = info.Types
} else if info.Typings != "" {
types = info.Typings
} else if info.Main != "" && endsWith(info.Main, ".d.ts", ".d.mts") {
} else if info.Main != "" && endsWith(info.Main, ".d.ts", ".d.mts", ".d.cts") {
types = info.Main
}
if strings.HasSuffix(types, ".d") {
types += ".ts"
} else if !endsWith(types, ".d.ts", ".d.mts") {
} else if !endsWith(types, ".d.ts", ".d.mts", ".d.cts") {
types += ".d.ts"
}
return redirect(ctx, fmt.Sprintf("%s/%s@%s%s", origin, info.Name, info.Version, utils.NormalizePathname(types)), isExactVersion)
Expand Down Expand Up @@ -945,7 +945,7 @@ func esmRouter() rex.Handle {
pathKind = EsmBuild
}
case ".ts", ".mts":
if endsWith(pathname, ".d.ts", ".d.mts") {
if endsWith(pathname, ".d.ts", ".d.mts", ".d.cts") {
pathKind = EsmDts
}
case ".css":
Expand Down
7 changes: 7 additions & 0 deletions test/issue-791/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { assertEquals } from "jsr:@std/assert";

Deno.test("issue #791", async () => {
const res = await fetch("http://localhost:8080/@vueuse/[email protected]");
res.body?.cancel();
assertEquals(res.headers.get("x-typescript-types"), "http://localhost:8080/@vueuse/[email protected]/index.d.cts");
});
Loading