Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix exports resolving (close #1046)
Browse files Browse the repository at this point in the history
ije committed Jan 21, 2025
1 parent 195d7c2 commit a70cdf5
Showing 2 changed files with 43 additions and 7 deletions.
34 changes: 27 additions & 7 deletions server/build_resolver.go
Original file line number Diff line number Diff line change
@@ -129,8 +129,28 @@ func (ctx *BuildContext) resolveEntry(esm EsmPath) (entry BuildEntry) {
}
*/
path := strings.ReplaceAll(s, "*", diff)
if ctx.existsPkgFile(path) {
exportEntry.update(path, pkgJson.Type == "module")
if endsWith(path, ".mjs", ".js", ".cjs") {
if ctx.existsPkgFile(path) {
exportEntry.update(path, pkgJson.Type == "module")
break
}
} else if p := path + ".mjs"; ctx.existsPkgFile(p) {
exportEntry.update(p, true)
break
} else if p := path + ".js"; ctx.existsPkgFile(p) {
exportEntry.update(p, pkgJson.Type == "module")
break
} else if p := path + ".cjs"; ctx.existsPkgFile(p) {
exportEntry.update(p, false)
break
} else if p := path + "/index.mjs"; ctx.existsPkgFile(p) {
exportEntry.update(p, true)
break
} else if p := path + "/index.js"; ctx.existsPkgFile(p) {
exportEntry.update(p, pkgJson.Type == "module")
break
} else if p := path + "/index.cjs"; ctx.existsPkgFile(p) {
exportEntry.update(p, false)
break
}
} else if obj, ok := conditions.(JSONObject); ok {
@@ -1131,19 +1151,19 @@ func matchAsteriskExport(exportName string, subModuleName string) (diff string,
return "", false
}

func resloveAsteriskPathMapping(obj JSONObject, diff string) JSONObject {
func resloveAsteriskPathMapping(conditions JSONObject, diff string) JSONObject {
reslovedConditions := JSONObject{
values: make(map[string]any),
}
for _, key := range obj.keys {
value, ok := obj.Get(key)
for _, key := range conditions.keys {
value, ok := conditions.Get(key)
if ok {
if s, ok := value.(string); ok {
reslovedConditions.keys = append(reslovedConditions.keys, key)
reslovedConditions.values[key] = strings.ReplaceAll(s, "*", diff)
} else if obj, ok := value.(JSONObject); ok {
} else if c, ok := value.(JSONObject); ok {
reslovedConditions.keys = append(reslovedConditions.keys, key)
reslovedConditions.values[key] = resloveAsteriskPathMapping(obj, diff)
reslovedConditions.values[key] = resloveAsteriskPathMapping(c, diff)
}
}
}
16 changes: 16 additions & 0 deletions test/issue-1046/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { assertEquals, assertStringIncludes } from "jsr:@std/assert";

Deno.test("issue #1046", async () => {
{
const res = await fetch("http://localhost:8080/@statistikzh/leu@0.13.1/leu-dropdown.js", {
headers: { "user-agent": "i'm a browser" },
});
assertEquals(res.status, 200);
assertStringIncludes(await res.text(), `export * from "/@statistikzh/leu@0.13.1/es2022/leu-dropdown.mjs`);
}
{
const res = await fetch("http://localhost:8080/@statistikzh/leu@0.13.1/leu-dropdown", { headers: { "user-agent": "i'm a browser" } });
assertEquals(res.status, 200);
assertStringIncludes(await res.text(), `export * from "/@statistikzh/leu@0.13.1/es2022/leu-dropdown.mjs`);
}
});

0 comments on commit a70cdf5

Please sign in to comment.