Skip to content

Commit

Permalink
Merge pull request esm-dev#238 from marcushultman/infer-types
Browse files Browse the repository at this point in the history
esm: infer types if an index.d.ts exists next to main
  • Loading branch information
ije authored Dec 26, 2021
2 parents 5e16204 + 9940bf6 commit 49a67ca
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion server/esm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type ESM struct {
}

func initESM(wd string, pkg Pkg, checkExports bool, isDev bool) (esm *ESM, err error) {
packageFile := path.Join(wd, "node_modules", pkg.Name, "package.json")
packageDir := path.Join(wd, "node_modules", pkg.Name)
packageFile := path.Join(packageDir, "package.json")

var p NpmPackage
err = utils.ParseJSONFile(packageFile, &p)
Expand Down Expand Up @@ -185,6 +186,15 @@ func initESM(wd string, pkg Pkg, checkExports bool, isDev bool) (esm *ESM, err e
// esm.ExportDefault = exportDefault
// }
}

if (path.Dir(esm.Main) != "" && esm.Types == "" && esm.Typings == "") {
typesPath := path.Join(path.Dir(esm.Main), "index.d.ts")

if (fileExists(path.Join(packageDir, typesPath))) {
esm.Types = typesPath
}
}

return
}

Expand Down

0 comments on commit 49a67ca

Please sign in to comment.