Skip to content

Commit

Permalink
Fix package nested conditions export (#546)
Browse files Browse the repository at this point in the history
* Fix package nested conditions export

* Add tests for nested conditions
  • Loading branch information
Justinidlerz authored Mar 14, 2023
1 parent 1d3ac2a commit 2d47fa8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
27 changes: 26 additions & 1 deletion server/esm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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 {
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/nested-conditions/nested-conditions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { assertExists } from "https://deno.land/[email protected]/testing/asserts.ts";

import * as utils from 'http://localhost:8080/v111/[email protected]/es2022/vanilla/utils.js'

Deno.test("Nested conditions", async () => {
assertExists(utils.splitAtom);
});

0 comments on commit 2d47fa8

Please sign in to comment.