Skip to content

Commit

Permalink
Improve exports resloving (close #179)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Feb 7, 2022
1 parent f669fce commit 8fb846f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
26 changes: 13 additions & 13 deletions server/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func initModule(wd string, pkg Pkg, target string, isDev bool) (esm *Module, err
}

esm = &Module{
NpmPackage: fixNpmPackage(p, target),
NpmPackage: fixNpmPackage(p, target, isDev),
}

if pkg.Submodule != "" {
Expand All @@ -62,7 +62,7 @@ func initModule(wd string, pkg Pkg, target string, isDev bool) (esm *Module, err
if err != nil {
return
}
np := fixNpmPackage(p, target)
np := fixNpmPackage(p, target, isDev)
if np.Module != "" {
esm.Module = path.Join(pkg.Submodule, np.Module)
} else {
Expand All @@ -89,18 +89,19 @@ func initModule(wd string, pkg Pkg, target string, isDev bool) (esm *Module, err
if p.DefinedExports != nil {
if m, ok := p.DefinedExports.(map[string]interface{}); ok {
for name, defines := range m {
/**
exports: {
"./lib/core": {
"require": "./lib/core.js",
"import": "./es/core.js"
}
}
*/
if name == "./"+pkg.Submodule {
resolvePackageExports(esm.NpmPackage, defines, target)
/**
exports: {
"./lib/core": {
"require": "./lib/core.js",
"import": "./es/core.js"
}
}
*/
resolvePackageExports(esm.NpmPackage, defines, target, isDev)
defined = true
break
} else if strings.HasSuffix(name, "/*") && strings.HasPrefix("./"+pkg.Submodule, strings.TrimSuffix(name, "*")) {
/**
exports: {
"./lib/languages/*": {
Expand All @@ -109,7 +110,6 @@ func initModule(wd string, pkg Pkg, target string, isDev bool) (esm *Module, err
},
}
*/
} else if strings.HasSuffix(name, "/*") && strings.HasPrefix("./"+pkg.Submodule, strings.TrimSuffix(name, "*")) {
suffix := strings.TrimPrefix("./"+pkg.Submodule, strings.TrimSuffix(name, "*"))
replaced := false
if m, ok := defines.(map[string]interface{}); ok {
Expand All @@ -124,7 +124,7 @@ func initModule(wd string, pkg Pkg, target string, isDev bool) (esm *Module, err
replaced = true
}
if replaced {
resolvePackageExports(esm.NpmPackage, defines, target)
resolvePackageExports(esm.NpmPackage, defines, target, isDev)
defined = true
}
}
Expand Down
24 changes: 15 additions & 9 deletions server/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func getNodejsVersion() (version string, major int, err error) {
}

// see https://nodejs.org/api/packages.html
func resolvePackageExports(p *NpmPackage, exports interface{}, target string) {
func resolvePackageExports(p *NpmPackage, exports interface{}, target string, isDev bool) {
s, ok := exports.(string)
if ok {
if p.Type == "module" || p.Module != "" {
Expand All @@ -395,9 +395,16 @@ func resolvePackageExports(p *NpmPackage, exports interface{}, target string) {

m, ok := exports.(map[string]interface{})
if ok {
names := []string{"import", "module", "browser"}
names := []string{"browser", "import", "module"}
if target == "deno" {
names = []string{"deno", "import", "module", "browser"}
names = []string{"deno", "browser", "import", "module"}
}
if p.Type == "module" {
if isDev {
names = append(names, "development", "default")
} else {
names = append(names, "production", "default")
}
}
for _, name := range names {
value, ok := m[name]
Expand All @@ -409,8 +416,7 @@ func resolvePackageExports(p *NpmPackage, exports interface{}, target string) {
}
}
}
names = []string{"require", "node", "default"}
for _, name := range names {
for _, name := range []string{"require", "node", "default"} {
value, ok := m[name]
if ok {
s, ok := value.(string)
Expand All @@ -434,7 +440,7 @@ func resolvePackageExports(p *NpmPackage, exports interface{}, target string) {
}
}

func fixNpmPackage(p NpmPackage, target string) *NpmPackage {
func fixNpmPackage(p NpmPackage, target string, isDev bool) *NpmPackage {
exports := p.DefinedExports
np := &p

Expand All @@ -453,21 +459,21 @@ func fixNpmPackage(p NpmPackage, target string) *NpmPackage {
".": "./esm/index.js"
}
*/
resolvePackageExports(np, v, target)
resolvePackageExports(np, v, target, isDev)
} else {
/*
exports: {
"require": "./cjs/index.js",
"import": "./esm/index.js"
}
*/
resolvePackageExports(np, m, target)
resolvePackageExports(np, m, target, isDev)
}
} else if _, ok := exports.(string); ok {
/*
exports: "./esm/index.js"
*/
resolvePackageExports(np, exports, target)
resolvePackageExports(np, exports, target, isDev)
}
}

Expand Down

0 comments on commit 8fb846f

Please sign in to comment.