Skip to content

Commit

Permalink
Fix resolveEntry funcation
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Dec 26, 2024
1 parent b539242 commit b969623
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 225 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/evanw/esbuild v0.24.2
github.com/gorilla/websocket v1.5.3
github.com/ije/esbuild-internal v0.24.2
github.com/ije/gox v0.9.3
github.com/ije/rex v1.14.4
github.com/ije/gox v0.9.4
github.com/ije/rex v1.14.5
github.com/mssola/useragent v1.0.0
github.com/yuin/goldmark v1.7.8
github.com/yuin/goldmark-meta v1.1.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/ije/esbuild-internal v0.24.2 h1:i1sjIu6suFZ1Arc4w0gWNE8OLLiAhEQ3nhU987uB+5w=
github.com/ije/esbuild-internal v0.24.2/go.mod h1:s7HvKZ4ZGifyzvgWpSwnJOQTr6b+bsgfNBZ8HAEwwSM=
github.com/ije/gox v0.9.3 h1:2JdnELIXaHF1JCojgS0ftq4ftF7koBcSO+PtVSWyZP8=
github.com/ije/gox v0.9.3/go.mod h1:3GTaK8WXf6oxRbrViLqKNLTNcMR871Dz0zoujFNmG48=
github.com/ije/rex v1.14.4 h1:wnARHMHmP7rwYuuzkg8nsxTJ4xa2aFuMqzGE7YDeS4A=
github.com/ije/rex v1.14.4/go.mod h1:OSFw49N5XRBzjskSqsAKzNPxksK9gUWT4CKjO7KkyWU=
github.com/ije/gox v0.9.4 h1:UJH637YSS1xYfUzSrIwi9DDukONUHdpKgzbvVwZHZf0=
github.com/ije/gox v0.9.4/go.mod h1:3GTaK8WXf6oxRbrViLqKNLTNcMR871Dz0zoujFNmG48=
github.com/ije/rex v1.14.5 h1:Db6lqRJOMxqeiSs4cixa8AkIqkldhMg0mO+qdHmrOUE=
github.com/ije/rex v1.14.5/go.mod h1:+ZeiG36LreDX5XrIl4i4feyvjr5kzMcHCO/a6LIoS48=
github.com/mssola/useragent v1.0.0 h1:WRlDpXyxHDNfvZaPEut5Biveq86Ze4o4EMffyMxmH5o=
github.com/mssola/useragent v1.0.0/go.mod h1:hz9Cqz4RXusgg1EdI4Al0INR62kP7aPSRNHnpU+b85Y=
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
Expand Down
100 changes: 50 additions & 50 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

type BuildContext struct {
esm Esm
esmPath EsmPath
npmrc *NpmRC
args BuildArgs
bundleMode BundleMode
Expand Down Expand Up @@ -168,11 +168,11 @@ func (ctx *BuildContext) Build() (meta *BuildMeta, err error) {
}
if !exportAll {
for _, exportName := range exportNames {
esm := ctx.esm
esm := ctx.esmPath
esm.SubPath = exportName
esm.SubModuleName = stripEntryModuleExt(exportName)
b := &BuildContext{
esm: esm,
esmPath: esm,
npmrc: ctx.npmrc,
args: ctx.args,
externalAll: ctx.externalAll,
Expand Down Expand Up @@ -220,11 +220,11 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
}

// json module
if strings.HasSuffix(ctx.esm.SubModuleName, ".json") {
if strings.HasSuffix(ctx.esmPath.SubModuleName, ".json") {
if analyzeMode {
return
}
jsonPath := path.Join(ctx.wd, "node_modules", ctx.esm.PkgName, ctx.esm.SubModuleName)
jsonPath := path.Join(ctx.wd, "node_modules", ctx.esmPath.PkgName, ctx.esmPath.SubModuleName)
if existsFile(jsonPath) {
var jsonData []byte
jsonData, err = os.ReadFile(jsonPath)
Expand All @@ -246,13 +246,13 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
}
}

entry := ctx.resolveEntry(ctx.esm)
entry := ctx.resolveEntry(ctx.esmPath)
if entry.isEmpty() {
err = fmt.Errorf("could not resolve build entry")
return
}
if DEBUG && !analyzeMode {
log.Debugf(`build(%s): Entry{main: "%s", module: %v, types: "%s"}`, ctx.esm.Specifier(), entry.main, entry.module, entry.types)
log.Debugf(`build(%s): Entry{main: "%s", module: %v, types: "%s"}`, ctx.esmPath.Specifier(), entry.main, entry.module, entry.types)
}

isTypesOnly := strings.HasPrefix(ctx.pkgJson.Name, "@types/") || entry.isTypesOnly()
Expand All @@ -266,7 +266,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
}
meta = &BuildMeta{
TypesOnly: true,
Dts: "/" + ctx.esm.Name() + entry.types[1:],
Dts: "/" + ctx.esmPath.Name() + entry.types[1:],
}
return
}
Expand All @@ -291,7 +291,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
return
}
b := &BuildContext{
esm: dep,
esmPath: dep,
npmrc: ctx.npmrc,
args: ctx.args,
externalAll: ctx.externalAll,
Expand Down Expand Up @@ -323,10 +323,10 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
return
}

entryModuleFilename := path.Join(ctx.wd, "node_modules", ctx.esm.PkgName, entry.main)
entrySpecifier := ctx.esm.PkgName
if ctx.esm.SubModuleName != "" {
entrySpecifier += "/" + ctx.esm.SubModuleName
entryModuleFilename := path.Join(ctx.wd, "node_modules", ctx.esmPath.PkgName, entry.main)
entrySpecifier := ctx.esmPath.PkgName
if ctx.esmPath.SubModuleName != "" {
entrySpecifier += "/" + ctx.esmPath.SubModuleName
}

var (
Expand Down Expand Up @@ -389,7 +389,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
// ban file urls
if strings.HasPrefix(args.Path, "file:") {
return esbuild.OnResolveResult{
Path: fmt.Sprintf("/error.js?type=unsupported-file-dependency&name=%s&importer=%s", strings.TrimPrefix(args.Path, "file:"), ctx.esm.Specifier()),
Path: fmt.Sprintf("/error.js?type=unsupported-file-dependency&name=%s&importer=%s", strings.TrimPrefix(args.Path, "file:"), ctx.esmPath.Specifier()),
External: true,
}, nil
}
Expand Down Expand Up @@ -485,8 +485,8 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
if forceNpmSpecifiers[specifier] && ctx.target == "denonext" {
version := ""
pkgName, _, subPath, _ := splitEsmPath(specifier)
if pkgName == ctx.esm.PkgName {
version = ctx.esm.PkgVersion
if pkgName == ctx.esmPath.PkgName {
version = ctx.esmPath.PkgVersion
} else if v, ok := ctx.pkgJson.Dependencies[pkgName]; ok && regexpVersionStrict.MatchString(v) {
version = v
} else if v, ok := ctx.pkgJson.PeerDependencies[pkgName]; ok && regexpVersionStrict.MatchString(v) {
Expand Down Expand Up @@ -529,15 +529,15 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
// node native modules do not work via http import
if strings.HasSuffix(fullFilepath, ".node") && existsFile(fullFilepath) {
return esbuild.OnResolveResult{
Path: fmt.Sprintf("/error.js?type=unsupported-node-native-module&name=%s&importer=%s", path.Base(args.Path), ctx.esm.Specifier()),
Path: fmt.Sprintf("/error.js?type=unsupported-node-native-module&name=%s&importer=%s", path.Base(args.Path), ctx.esmPath.Specifier()),
External: true,
}, nil
}

// externalize parent module
// e.g. "react/jsx-runtime" imports "react"
if ctx.esm.SubModuleName != "" && specifier == ctx.esm.PkgName && ctx.bundleMode != BundleAll {
externalPath, err := ctx.resolveExternalModule(ctx.esm.PkgName, args.Kind, analyzeMode)
if ctx.esmPath.SubModuleName != "" && specifier == ctx.esmPath.PkgName && ctx.bundleMode != BundleAll {
externalPath, err := ctx.resolveExternalModule(ctx.esmPath.PkgName, args.Kind, analyzeMode)
if err != nil {
return esbuild.OnResolveResult{}, err
}
Expand Down Expand Up @@ -582,9 +582,9 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree

if path.Ext(fullFilepath) == "" || !existsFile(fullFilepath) {
subPath := utils.NormalizePathname(moduleSpecifier)[1:]
entry := ctx.resolveEntry(Esm{
PkgName: ctx.esm.PkgName,
PkgVersion: ctx.esm.PkgVersion,
entry := ctx.resolveEntry(EsmPath{
PkgName: ctx.esmPath.PkgName,
PkgVersion: ctx.esmPath.PkgVersion,
SubModuleName: stripEntryModuleExt(subPath),
SubPath: subPath,
})
Expand Down Expand Up @@ -699,7 +699,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
}

// module file path
moduleFilename := path.Join(ctx.wd, "node_modules", ctx.esm.PkgName, moduleSpecifier)
moduleFilename := path.Join(ctx.wd, "node_modules", ctx.esmPath.PkgName, moduleSpecifier)

// split the module that is an alias of a dependency
// means this file just include a single line(js): `export * from "dep"`
Expand Down Expand Up @@ -733,7 +733,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
// - it's not a dynamic import and `?bundle=false` query is not present
if moduleSpecifier == entry.main || (exportSpecifier != "" && exportSpecifier == entrySpecifier) || (args.Kind != esbuild.ResolveJSDynamicImport && !noBundle) {
if existsFile(moduleFilename) {
pkgDir := path.Join(ctx.wd, "node_modules", ctx.esm.PkgName)
pkgDir := path.Join(ctx.wd, "node_modules", ctx.esmPath.PkgName)
moduleName := strings.TrimPrefix(moduleSpecifier, pkgDir)[1:]
if analyzeMode && moduleFilename != entryModuleFilename && strings.HasPrefix(args.Importer, pkgDir) {
depTree.lock.Lock()
Expand Down Expand Up @@ -878,7 +878,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
if semverLessThan(svelteVersion, "4.0.0") {
return esbuild.OnLoadResult{}, errors.New("svelte version must be greater than 4.0.0")
}
out, err := transformSvelte(ctx.npmrc, svelteVersion, ctx.esm.Specifier(), string(code))
out, err := transformSvelte(ctx.npmrc, svelteVersion, ctx.esmPath.Specifier(), string(code))
if err != nil {
return esbuild.OnLoadResult{}, err
}
Expand Down Expand Up @@ -912,7 +912,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
if semverLessThan(vueVersion, "3.0.0") {
return esbuild.OnLoadResult{}, errors.New("vue version must be greater than 3.0.0")
}
out, err := transformVue(ctx.npmrc, vueVersion, ctx.esm.Specifier(), string(code))
out, err := transformVue(ctx.npmrc, vueVersion, ctx.esmPath.Specifier(), string(code))
if err != nil {
return esbuild.OnLoadResult{}, err
}
Expand Down Expand Up @@ -945,7 +945,7 @@ func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, depTree
}
} else {
if ctx.isBrowserTarget() {
switch ctx.esm.PkgName {
switch ctx.esmPath.PkgName {
case "react", "react-dom", "typescript":
// safe to reserve `process` for these packages
delete(define, "process")
Expand Down Expand Up @@ -1070,21 +1070,21 @@ rebuild:
header, recycle := NewBuffer()
defer recycle()
header.WriteString("/* esm.sh - ")
if ctx.esm.GhPrefix {
if ctx.esmPath.GhPrefix {
header.WriteString("github:")
} else if ctx.esm.PrPrefix {
} else if ctx.esmPath.PrPrefix {
header.WriteString("pkg.pr.new/")
}
header.WriteString(ctx.esm.PkgName)
if ctx.esm.GhPrefix {
header.WriteString(ctx.esmPath.PkgName)
if ctx.esmPath.GhPrefix {
header.WriteByte('#')
} else {
header.WriteByte('@')
}
header.WriteString(ctx.esm.PkgVersion)
if ctx.esm.SubModuleName != "" {
header.WriteString(ctx.esmPath.PkgVersion)
if ctx.esmPath.SubModuleName != "" {
header.WriteByte('/')
header.WriteString(ctx.esm.SubModuleName)
header.WriteString(ctx.esmPath.SubModuleName)
}
header.WriteString(" */\n")

Expand Down Expand Up @@ -1222,7 +1222,7 @@ rebuild:
isEsModule[i] = true
} else {
b := &BuildContext{
esm: dep,
esmPath: dep,
npmrc: ctx.npmrc,
args: ctx.args,
externalAll: ctx.externalAll,
Expand Down Expand Up @@ -1279,10 +1279,10 @@ rebuild:
finalJS.Write(jsContent)

// check if the package is deprecated
if !ctx.esm.GhPrefix && !ctx.esm.PrPrefix {
if !ctx.esmPath.GhPrefix && !ctx.esmPath.PrPrefix {
deprecated, _ := ctx.npmrc.isDeprecated(ctx.pkgJson.Name, ctx.pkgJson.Version)
if deprecated != "" {
fmt.Fprintf(finalJS, `console.warn("%%c[esm.sh]%%c %%cdeprecated%%c %s@%s: " + %s, "color:grey", "", "color:red", "");%s`, ctx.esm.PkgName, ctx.esm.PkgVersion, utils.MustEncodeJSON(deprecated), "\n")
fmt.Fprintf(finalJS, `console.warn("%%c[esm.sh]%%c %%cdeprecated%%c %s@%s: " + %s, "color:grey", "", "color:red", "");%s`, ctx.esmPath.PkgName, ctx.esmPath.PkgVersion, utils.MustEncodeJSON(deprecated), "\n")
}
}

Expand Down Expand Up @@ -1353,10 +1353,10 @@ func (ctx *BuildContext) buildTypes() (ret *BuildMeta, err error) {
}

var dts string
if endsWith(ctx.esm.SubPath, ".d.ts", "d.mts") {
dts = "./" + ctx.esm.SubPath
if endsWith(ctx.esmPath.SubPath, ".d.ts", "d.mts") {
dts = "./" + ctx.esmPath.SubPath
} else {
entry := ctx.resolveEntry(ctx.esm)
entry := ctx.resolveEntry(ctx.esmPath)
if entry.types == "" {
err = errors.New("types not found")
return
Expand All @@ -1370,31 +1370,31 @@ func (ctx *BuildContext) buildTypes() (ret *BuildMeta, err error) {
return
}

ret = &BuildMeta{Dts: "/" + ctx.esm.Name() + dts[1:]}
ret = &BuildMeta{Dts: "/" + ctx.esmPath.Name() + dts[1:]}
return
}

func (ctx *BuildContext) install() (err error) {
if ctx.wd == "" || ctx.pkgJson == nil {
var p *PackageJSON
p, err = ctx.npmrc.installPackage(ctx.esm.Package())
p, err = ctx.npmrc.installPackage(ctx.esmPath.Package())
if err != nil {
return
}

if ctx.esm.GhPrefix || ctx.esm.PrPrefix {
if ctx.esmPath.GhPrefix || ctx.esmPath.PrPrefix {
// if the name in package.json is not the same as the repository name
if p.Name != ctx.esm.PkgName {
if p.Name != ctx.esmPath.PkgName {
p.PkgName = p.Name
p.Name = ctx.esm.PkgName
p.Name = ctx.esmPath.PkgName
}
p.Version = ctx.esm.PkgVersion
p.Version = ctx.esmPath.PkgVersion
} else {
p.Version = strings.TrimPrefix(p.Version, "v")
}

// Check if the `SubPath` is the same as the `main` or `module` field of the package.json
if subModule := ctx.esm.SubModuleName; subModule != "" && ctx.target != "types" {
if subModule := ctx.esmPath.SubModuleName; subModule != "" && ctx.target != "types" {
isMainModule := false
check := func(s string) bool {
return isMainModule || (s != "" && subModule == utils.NormalizePathname(stripModuleExt(s))[1:])
Expand Down Expand Up @@ -1422,13 +1422,13 @@ func (ctx *BuildContext) install() (err error) {
isMainModule = (p.Module != "" && check(p.Module)) || (p.Main != "" && check(p.Main))
}
if isMainModule {
ctx.esm.SubModuleName = ""
ctx.esm.SubPath = ""
ctx.esmPath.SubModuleName = ""
ctx.esmPath.SubPath = ""
ctx.path = ""
}
}

ctx.wd = path.Join(ctx.npmrc.StoreDir(), ctx.esm.Name())
ctx.wd = path.Join(ctx.npmrc.StoreDir(), ctx.esmPath.Name())
ctx.pkgJson = p
}

Expand Down
2 changes: 1 addition & 1 deletion server/build_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func encodeBuildArgs(args BuildArgs, isDts bool) string {
}

// resolveBuildArgs resolves `alias`, `deps`, `external` of the build args
func resolveBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, esm Esm) error {
func resolveBuildArgs(npmrc *NpmRC, installDir string, args *BuildArgs, esm EsmPath) error {
if len(args.alias) > 0 || len(args.deps) > 0 || args.external.Len() > 0 {
// quick check if the alias, deps, external are all in dependencies of the package
depsSet, err := func() (set *Set, err error) {
Expand Down
Loading

0 comments on commit b969623

Please sign in to comment.