Skip to content

Commit

Permalink
Fix dts transformer (close #599)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Apr 21, 2023
1 parent ba501d7 commit 3c509c7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
4 changes: 2 additions & 2 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ func (task *BuildTask) checkDTS(esm *ESMBuild, npm NpmPackage) {
submodule := task.Pkg.Submodule
var dts string
if npm.Types != "" {
dts = toTypesPath(task.wd, npm, "", encodeBuildArgsPrefix(task.BuildArgs, task.Pkg.Name, true), submodule)
dts = task.toTypesPath(task.wd, npm, "", encodeBuildArgsPrefix(task.BuildArgs, task.Pkg.Name, true), submodule)
} else if !strings.HasPrefix(name, "@types/") {
versions := []string{"latest"}
versionParts := strings.Split(task.Pkg.Version, ".")
Expand All @@ -1061,7 +1061,7 @@ func (task *BuildTask) checkDTS(esm *ESMBuild, npm NpmPackage) {
p, _, err := task.getPackageInfo(typesPkgName, version)
if err == nil {
prefix := encodeBuildArgsPrefix(task.BuildArgs, p.Name, true)
dts = toTypesPath(task.wd, p, version, prefix, submodule)
dts = task.toTypesPath(task.wd, p, version, prefix, submodule)
break
}
}
Expand Down
39 changes: 19 additions & 20 deletions server/dts_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker *
if err != nil {
break
}
subpath = pkg.Submodule
info, fromPackageJSON, err = getPackageInfo(wd, pkg.Name, version)
if err != nil || ((info.Types == "" && info.Typings == "") && !strings.HasPrefix(info.Name, "@types/")) {
p, ok, e := getPackageInfo(wd, toTypesPackageName(pkg.Name), version)
Expand All @@ -209,7 +210,6 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker *
}
}
if err == nil {
subpath = pkg.Submodule
break
}
}
Expand All @@ -227,7 +227,7 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker *

// copy dependent dts files in the node_modules directory in current build context
if fromPackageJSON {
typesPath := toTypesPath(wd, info, "", "", subpath)
typesPath := task.toTypesPath(wd, info, "", "", subpath)
if strings.HasSuffix(typesPath, ".d.ts") && !strings.HasSuffix(typesPath, "~.d.ts") {
imports.Add(typesPath)
}
Expand All @@ -248,7 +248,7 @@ func (task *BuildTask) transformDTS(dts string, aliasDepsPrefix string, marker *
}

pkgBasePath := info.Name + "@" + info.Version + "/" + encodeBuildArgsPrefix(task.BuildArgs, info.Name, true)
importPath = fmt.Sprintf("%s/%s", cdnOriginAndBuildBasePath, pkgBasePath+importPath)
importPath = fmt.Sprintf("%s/%s%s", cdnOriginAndBuildBasePath, pkgBasePath, importPath)
}

return importPath
Expand Down Expand Up @@ -346,26 +346,24 @@ func removeGlobalBlock(input []byte) (output []byte, err error) {
return nil, errors.New("removeGlobalBlock: global block not end")
}

func toTypesPath(wd string, p NpmPackage, version string, buildArgsPrefix string, subpath string) string {
func (task *BuildTask) toTypesPath(wd string, p NpmPackage, version string, buildArgsPrefix string, subpath string) string {
var types string
if subpath != "" {
if p.Types != "" {
var rawPkg NpmPackage
if utils.ParseJSONFile(path.Join(wd, "node_modules", p.Name, "package.json"), &rawPkg) == nil {
if p.Types != rawPkg.Types && p.Types != rawPkg.Typings {
types = p.Types
}
}
t := &BuildTask{
BuildArgs: task.BuildArgs,
Pkg: Pkg{
Name: p.Name,
Version: p.Version,
Submodule: subpath,
Subpath: subpath,
},
Target: task.Target,
Dev: false,
wd: wd,
}
if types == "" {
var subPkg NpmPackage
if utils.ParseJSONFile(path.Join(wd, "node_modules", p.Name, subpath, "package.json"), &subPkg) == nil {
if subPkg.Types != "" {
types = path.Join(subpath, subPkg.Types)
} else if subPkg.Typings != "" {
types = path.Join(subpath, subPkg.Typings)
}
}
_, p, _, e := t.analyze()
if e == nil {
types = p.Types
}
if types == "" {
types = subpath
Expand Down Expand Up @@ -398,5 +396,6 @@ func toTypesPath(wd string, p NpmPackage, version string, buildArgsPrefix string
if version == "" {
version = p.Version
}

return fmt.Sprintf("%s@%s/%s%s", p.Name, version, buildArgsPrefix, utils.CleanPath(types)[1:])
}
9 changes: 9 additions & 0 deletions test/issue-599/issue-599.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import { customElement } from "http://localhost:8080/[email protected]/decorators";
import { map } from "http://localhost:8080/[email protected]/directives/map";

Deno.test("issue #599", async () => {
assertEquals(typeof customElement, "function");
assertEquals(typeof map, "function");
});

0 comments on commit 3c509c7

Please sign in to comment.