Skip to content

Commit

Permalink
Add support of resolving typesVersions field (close #593)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Apr 17, 2023
1 parent ea1904e commit 9df0c74
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
42 changes: 37 additions & 5 deletions server/build_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,42 @@ func (task *BuildTask) fixNpmPackage(p NpmPackage) NpmPackage {
p.Name = task.Pkg.Name
p.Version = task.Pkg.Version
}

if p.Types == "" && p.Typings != "" {
p.Types = p.Typings
}

if len(p.TypesVersions) > 0 {
var usedCondition string
for c, e := range p.TypesVersions {
if c == "*" && strings.HasPrefix(c, ">") || strings.HasPrefix(c, ">=") {
if usedCondition == "" || c == "*" || c > usedCondition {
if m, ok := e.(map[string]interface{}); ok {
d, ok := m["*"]
if !ok {
d, ok = m["."]
}
if ok {
if a, ok := d.([]interface{}); ok && len(a) > 0 {
if t, ok := a[0].(string); ok {
usedCondition = c
if strings.HasSuffix(t, "*") {
f := p.Types
if f == "" {
f = "index.d.ts"
}
t = path.Join(t[:len(t)-1], f)
}
p.Types = t
}
}
}
}
}
}
}
}

if exports := p.DefinedExports; exports != nil {
if m, ok := exports.(map[string]interface{}); ok {
v, ok := m["."]
Expand Down Expand Up @@ -422,10 +458,6 @@ func (task *BuildTask) fixNpmPackage(p NpmPackage) NpmPackage {
}
}

if p.Types == "" && p.Typings != "" {
p.Types = p.Typings
}

if p.Types == "" && p.Main != "" {
if strings.HasSuffix(p.Main, ".d.ts") {
p.Types = p.Main
Expand Down Expand Up @@ -482,7 +514,7 @@ func (task *BuildTask) applyConditions(p *NpmPackage, exports interface{}, pType
m, ok := exports.(map[string]interface{})
if ok {
targetConditions := []string{"browser"}
conditions := []string{"import", "module", "es2015"}
conditions := []string{"module", "import", "es2015"}
switch task.Target {
case "deno", "denonext":
targetConditions = []string{"deno", "worker", "browser"}
Expand Down
13 changes: 10 additions & 3 deletions server/dts_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,12 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker *
}
info, fromPackageJSON, err = getPackageInfo(wd, pkg.Name, version)
if err != nil || ((info.Types == "" && info.Typings == "") && !strings.HasPrefix(info.Name, "@types/")) {
info, fromPackageJSON, err = getPackageInfo(wd, toTypesPackageName(pkg.Name), version)
p, ok, e := getPackageInfo(wd, toTypesPackageName(pkg.Name), version)
if e == nil {
info = p
fromPackageJSON = ok
err = nil
}
}
if err == nil {
subpath = pkg.Submodule
Expand All @@ -212,7 +217,7 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker *
return importPath
}

// use types with `exports` contidions
// use types with `exports` and `typesVersions` contidions
info = task.fixNpmPackage(info)

// use version defined in `?deps`
Expand All @@ -234,6 +239,8 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker *
importPath = utils.CleanPath(info.Types)[1:]
} else if info.Typings != "" {
importPath = utils.CleanPath(info.Typings)[1:]
} else {
importPath = "index.d.ts"
}
if !strings.HasSuffix(importPath, ".d.ts") && !strings.HasSuffix(importPath, "/*") {
importPath += "~.d.ts"
Expand Down Expand Up @@ -374,7 +381,7 @@ func toTypesPath(wd string, p NpmPackage, version string, buildArgsPrefix string
types = "index.d.ts"
}
} else {
return ""
types = "index.d.ts"
}

if !endsWith(types, ".d.ts", ".d.mts") && !strings.HasSuffix(types, "/*") {
Expand Down
3 changes: 3 additions & 0 deletions server/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type NpmPackageTemp struct {
Dependencies map[string]string `json:"dependencies,omitempty"`
PeerDependencies map[string]string `json:"peerDependencies,omitempty"`
Imports map[string]interface{} `json:"imports,omitempty"`
TypesVersions map[string]interface{} `json:"typesVersions,omitempty"`
DefinedExports interface{} `json:"exports,omitempty"`
Deprecated interface{} `json:"deprecated,omitempty"`
}
Expand Down Expand Up @@ -96,6 +97,7 @@ func (a *NpmPackageTemp) ToNpmPackage() *NpmPackage {
Dependencies: a.Dependencies,
PeerDependencies: a.PeerDependencies,
Imports: a.Imports,
TypesVersions: a.TypesVersions,
DefinedExports: a.DefinedExports,
Deprecated: deprecated,
}
Expand All @@ -117,6 +119,7 @@ type NpmPackage struct {
Dependencies map[string]string
PeerDependencies map[string]string
Imports map[string]interface{}
TypesVersions map[string]interface{}
DefinedExports interface{}
Deprecated string
}
Expand Down
7 changes: 7 additions & 0 deletions test/issue-593/issue-593.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import { eventChannel } from "http://localhost:8080/[email protected]";

Deno.test("issue #593", async () => {
assertEquals(typeof eventChannel, "function");
});

0 comments on commit 9df0c74

Please sign in to comment.