Skip to content

Commit

Permalink
Improve "normalizeBuildArgs" function
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Nov 23, 2024
1 parent 94c593b commit 6b875eb
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions server/build_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ func encodeBuildArgs(args BuildArgs, isDts bool) string {
}

// normalizeBuildArgs removes invalid alias, deps, external from the build args
func normalizeBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, url ESMPath) error {
func normalizeBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, esmPath ESMPath) error {
if len(args.alias) > 0 || len(args.deps) > 0 || args.external.Len() > 0 {
depsSet := NewStringSet()
err := walkDeps(npmrc, installDir, url, depsSet)
err := walkDeps(npmrc, installDir, esmPath, depsSet)
if err != nil {
return err
}
Expand All @@ -165,7 +165,7 @@ func normalizeBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, url ES
}
for from, to := range alias {
pkgName, _, _, _ := splitESMPath(to)
if pkgName == url.PkgName {
if pkgName == esmPath.PkgName {
delete(alias, from)
} else {
depsSet.Add(pkgName)
Expand All @@ -176,25 +176,39 @@ func normalizeBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, url ES
if len(args.deps) > 0 {
deps := map[string]string{}
for name, version := range args.deps {
if url.PkgName == "react-dom" && name == "react" {
// react version should be the same as react-dom
// The version of package "react" should be the same as "react-dom"
if esmPath.PkgName == "react-dom" && name == "react" {
continue
}
if depsSet.Has(name) {
if name != url.PkgName {
deps[name] = version
}
if name != esmPath.PkgName && depsSet.Has(name) {
deps[name] = version
continue
}
// fix some edge cases
// for example, the package "htm" doesn't declare 'preact' as a dependency explicitly
// as a workaround, we check if the package name is in the subPath of the package
if esmPath.SubBareName != "" && contains(strings.Split(esmPath.SubBareName, "/"), name) {
deps[name] = version
}
}
args.deps = deps
}
if args.external.Len() > 0 {
external := NewStringSet()
for _, name := range args.external.Values() {
if strings.HasPrefix(name, "node:") || depsSet.Has(name) {
if name != url.PkgName || url.SubPath != "" {
if strings.HasPrefix(name, "node:") {
if nodeBuiltinModules[name[5:]] {
external.Add(name)
}
continue
}
// if the subModule externalizes the package entry
if name == esmPath.PkgName && esmPath.SubPath != "" {
external.Add(name)
continue
}
if name != esmPath.PkgName && depsSet.Has(name) {
external.Add(name)
}
}
args.external = external
Expand All @@ -203,24 +217,24 @@ func normalizeBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, url ES
return nil
}

func walkDeps(npmrc *NpmRC, installDir string, esm ESMPath, mark *StringSet) (err error) {
if mark.Has(esm.PkgName) {
func walkDeps(npmrc *NpmRC, installDir string, esmPath ESMPath, mark *StringSet) (err error) {
if mark.Has(esmPath.PkgName) {
return
}
mark.Add(esm.PkgName)
mark.Add(esmPath.PkgName)
var p *PackageJSON
pkgJsonPath := path.Join(installDir, "node_modules", ".pnpm", "node_modules", esm.PkgName, "package.json")
pkgJsonPath := path.Join(installDir, "node_modules", ".pnpm", "node_modules", esmPath.PkgName, "package.json")
if !existsFile(pkgJsonPath) {
pkgJsonPath = path.Join(installDir, "node_modules", esm.PkgName, "package.json")
pkgJsonPath = path.Join(installDir, "node_modules", esmPath.PkgName, "package.json")
}
if existsFile(pkgJsonPath) {
var raw PackageJSONRaw
err = utils.ParseJSONFile(pkgJsonPath, &raw)
if err == nil {
p = raw.ToNpmPackage()
}
} else if regexpVersionStrict.MatchString(esm.PkgVersion) || esm.GhPrefix {
p, err = npmrc.installPackage(esm)
} else if regexpVersionStrict.MatchString(esmPath.PkgVersion) || esmPath.GhPrefix {
p, err = npmrc.installPackage(esmPath)
} else {
return nil
}
Expand All @@ -235,7 +249,7 @@ func walkDeps(npmrc *NpmRC, installDir string, esm ESMPath, mark *StringSet) (er
pkgDeps[name] = version
}
for name, version := range pkgDeps {
if strings.HasPrefix(name, "@types/") || strings.HasPrefix(name, "@babel/") || strings.HasPrefix(name, "is-") {
if strings.HasPrefix(name, "@types/") || strings.HasPrefix(name, "@babel/") {
continue
}
err := walkDeps(npmrc, installDir, ESMPath{PkgName: name, PkgVersion: version}, mark)
Expand Down

0 comments on commit 6b875eb

Please sign in to comment.