Skip to content

Commit

Permalink
refactor: remove some regexps (esm-dev#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije authored Jan 5, 2025
1 parent cc0e0fb commit 014ac3f
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 125 deletions.
8 changes: 4 additions & 4 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
pkgName, _, subPath, _ := splitEsmPath(specifier)
if pkgName == ctx.esm.PkgName {
version = ctx.esm.PkgVersion
} else if v, ok := ctx.pkgJson.Dependencies[pkgName]; ok && regexpVersionStrict.MatchString(v) {
} else if v, ok := ctx.pkgJson.Dependencies[pkgName]; ok && isExactVersion(v) {
version = v
} else if v, ok := ctx.pkgJson.PeerDependencies[pkgName]; ok && regexpVersionStrict.MatchString(v) {
} else if v, ok := ctx.pkgJson.PeerDependencies[pkgName]; ok && isExactVersion(v) {
version = v
}
p := pkgName
Expand Down Expand Up @@ -866,7 +866,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
} else if version, ok := ctx.pkgJson.PeerDependencies["svelte"]; ok {
svelteVersion = version
}
if !regexpVersionStrict.MatchString(svelteVersion) {
if !isExactVersion(svelteVersion) {
info, err := ctx.npmrc.getPackageInfo("svelte", svelteVersion)
if err != nil {
return esbuild.OnLoadResult{}, errors.New("failed to get svelte package info")
Expand Down Expand Up @@ -900,7 +900,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, include
} else if version, ok := ctx.pkgJson.PeerDependencies["vue"]; ok {
vueVersion = version
}
if !regexpVersionStrict.MatchString(vueVersion) {
if !isExactVersion(vueVersion) {
info, err := ctx.npmrc.getPackageInfo("vue", vueVersion)
if err != nil {
return esbuild.OnLoadResult{}, errors.New("failed to get vue package info")
Expand Down
6 changes: 3 additions & 3 deletions server/build_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,11 +923,11 @@ func (ctx *BuildContext) resolveExternalModule(specifier string, kind api.Resolv

var isFixedVersion bool
if dep.GhPrefix {
isFixedVersion = isCommitish(dep.PkgVersion) || regexpVersionStrict.MatchString(strings.TrimPrefix(dep.PkgVersion, "v"))
isFixedVersion = isCommitish(dep.PkgVersion) || isExactVersion(strings.TrimPrefix(dep.PkgVersion, "v"))
} else if dep.PrPrefix {
isFixedVersion = true
} else {
isFixedVersion = regexpVersionStrict.MatchString(dep.PkgVersion)
isFixedVersion = isExactVersion(dep.PkgVersion)
}
if isFixedVersion {
buildArgsPrefix := ""
Expand Down Expand Up @@ -1013,7 +1013,7 @@ func (ctx *BuildContext) resloveDTS(entry BuildEntry) (string, error) {
}

// lookup types in @types scope
if pkgJson := ctx.pkgJson; pkgJson.Types == "" && !strings.HasPrefix(pkgJson.Name, "@types/") && regexpVersionStrict.MatchString(pkgJson.Version) {
if pkgJson := ctx.pkgJson; pkgJson.Types == "" && !strings.HasPrefix(pkgJson.Name, "@types/") && isExactVersion(pkgJson.Version) {
versionParts := strings.Split(pkgJson.Version, ".")
versions := []string{
versionParts[0] + "." + versionParts[1], // major.minor
Expand Down
2 changes: 1 addition & 1 deletion server/cjs_module_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ RETRY:
if strings.HasPrefix(line, "@") {
ret.Reexport = line[1:]
break
} else if regexpJSIdent.MatchString(line) && !isJsReservedWord(line) {
} else if isJsIdentifier(line) && !isJsReservedWord(line) {
ret.Exports = append(ret.Exports, line)
}
}
Expand Down
5 changes: 2 additions & 3 deletions server/legacy_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ func legacyESM(ctx *rex.Context, pathname string) any {
if ctx.R.URL.RawQuery != "" {
query = "?" + ctx.R.URL.RawQuery
}
isFixedVersion := regexpVersionStrict.MatchString(pkgVersion)
if !isFixedVersion {
if !isExactVersion(pkgVersion) {
npmrc := DefaultNpmRC()
pkgInfo, err := npmrc.fetchPackageInfo(pkgName, pkgVersion)
pkgInfo, err := npmrc.getPackageInfo(pkgName, pkgVersion)
if err != nil {
if strings.Contains(err.Error(), " not found") {
return rex.Status(404, err.Error())
Expand Down
4 changes: 2 additions & 2 deletions server/loader_implements.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func resolveSvelteVersion(npmrc *NpmRC, importMap common.ImportMap) (svelteVersi
}
}
}
if !regexpVersionStrict.MatchString(svelteVersion) {
if !isExactVersion(svelteVersion) {
var info *PackageJSON
info, err = npmrc.getPackageInfo("svelte", svelteVersion)
if err != nil {
Expand Down Expand Up @@ -282,7 +282,7 @@ func resolveVueVersion(npmrc *NpmRC, importMap common.ImportMap) (vueVersion str
}
}
}
if !regexpVersionStrict.MatchString(vueVersion) {
if !isExactVersion(vueVersion) {
var info *PackageJSON
info, err = npmrc.getPackageInfo("vue", vueVersion)
if err != nil {
Expand Down
Loading

0 comments on commit 014ac3f

Please sign in to comment.