From a70cdf519607f4567b877f7f52c49e3815955041 Mon Sep 17 00:00:00 2001 From: Je Xia Date: Tue, 21 Jan 2025 22:27:32 +0800 Subject: [PATCH] Fix `exports` resolving (close #1046) --- server/build_resolver.go | 34 +++++++++++++++++++++++++++------- test/issue-1046/test.ts | 16 ++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 test/issue-1046/test.ts diff --git a/server/build_resolver.go b/server/build_resolver.go index 563b7a20f..6f1496424 100644 --- a/server/build_resolver.go +++ b/server/build_resolver.go @@ -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) } } } diff --git a/test/issue-1046/test.ts b/test/issue-1046/test.ts new file mode 100644 index 000000000..aba346bcc --- /dev/null +++ b/test/issue-1046/test.ts @@ -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`); + } +});