From 2d47fa89b41b5e58af99d0dd35a33f40780ac175 Mon Sep 17 00:00:00 2001 From: idler Date: Tue, 14 Mar 2023 16:42:46 +0800 Subject: [PATCH] Fix package nested conditions export (#546) * Fix package nested conditions export * Add tests for nested conditions --- server/esm.go | 27 ++++++++++++++++++- .../nested-conditions.test.ts | 7 +++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/nested-conditions/nested-conditions.test.ts diff --git a/server/esm.go b/server/esm.go index 0b1ce9bf2..83ffecb71 100644 --- a/server/esm.go +++ b/server/esm.go @@ -95,7 +95,7 @@ func initModule(wd string, pkg Pkg, target string, isDev bool) (esm *ESM, npm Np "require": "./lib/core.js", "import": "./esm/core.js" }, - "./lib/core.js": { + "./lib/core.js": { "require": "./lib/core.js", "import": "./esm/core.js" } @@ -117,11 +117,35 @@ func initModule(wd string, pkg Pkg, target string, isDev bool) (esm *ESM, npm Np hasDefines := false if m, ok := defines.(map[string]interface{}); ok { newDefines := map[string]interface{}{} + for key, value := range m { if s, ok := value.(string); ok && s != name { newDefines[key] = strings.Replace(s, "*", suffix, -1) hasDefines = true } + + /** + exports: { + "./*": { + "types": "./*.d.ts", + "import": { + "types": "./esm/*.d.mts", + "default": "./esm/*.mjs" + }, + "default": "./*.js" + } + } + */ + if s, ok := value.(map[string]interface{}); ok { + subNewDefinies := map[string]interface{}{} + for subKey, subValue := range s { + if s1, ok := subValue.(string); ok && s1 != name { + subNewDefinies[subKey] = strings.Replace(s1, "*", suffix, -1) + hasDefines = true + } + } + newDefines[key] = subNewDefinies + } } defines = newDefines } else if s, ok := defines.(string); ok && name != s { @@ -136,6 +160,7 @@ func initModule(wd string, pkg Pkg, target string, isDev bool) (esm *ESM, npm Np } } } + if !resolved { if npm.Type == "module" || npm.Module != "" { // follow main module type diff --git a/test/nested-conditions/nested-conditions.test.ts b/test/nested-conditions/nested-conditions.test.ts new file mode 100644 index 000000000..3ab06efda --- /dev/null +++ b/test/nested-conditions/nested-conditions.test.ts @@ -0,0 +1,7 @@ +import { assertExists } from "https://deno.land/std@0.178.0/testing/asserts.ts"; + +import * as utils from 'http://localhost:8080/v111/jotai@2.0.3/es2022/vanilla/utils.js' + +Deno.test("Nested conditions", async () => { + assertExists(utils.splitAtom); +});