Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from esm-dev:main #61

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 59 additions & 54 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type BuildContext struct {
wd string
pkgJson *PackageJSON
path string
rawPath string
status string
splitting *set.ReadOnlySet[string]
esmImports [][2]string
Expand Down Expand Up @@ -98,57 +99,7 @@ func (ctx *BuildContext) Path() string {
return ctx.path
}

asteriskPrefix := ""
if ctx.externalAll {
asteriskPrefix = "*"
}

esm := ctx.esm
if ctx.target == "types" {
if strings.HasSuffix(esm.SubPath, ".d.ts") {
ctx.path = fmt.Sprintf(
"/%s%s/%s%s",
asteriskPrefix,
esm.Name(),
ctx.getBuildArgsPrefix(true),
esm.SubPath,
)
} else {
ctx.path = "/" + esm.Specifier()
}
return ctx.path
}

name := strings.TrimSuffix(path.Base(esm.PkgName), ".js")
if esm.SubModuleName != "" {
if esm.SubModuleName == name {
// if the sub-module name is same as the package name
name = "__" + esm.SubModuleName
} else {
name = esm.SubModuleName
}
// workaround for es5-ext "../#/.." path
if esm.PkgName == "es5-ext" {
name = strings.ReplaceAll(name, "/#/", "/%23/")
}
}

if ctx.dev {
name += ".development"
}
if ctx.bundleMode == BundleAll {
name += ".bundle"
} else if ctx.bundleMode == BundleFalse {
name += ".nobundle"
}
ctx.path = fmt.Sprintf(
"/%s%s/%s%s/%s.mjs",
asteriskPrefix,
esm.Name(),
ctx.getBuildArgsPrefix(ctx.target == "types"),
ctx.target,
name,
)
ctx.buildPath()
return ctx.path
}

Expand Down Expand Up @@ -232,6 +183,60 @@ func (ctx *BuildContext) Build() (meta *BuildMeta, err error) {
return
}

func (ctx *BuildContext) buildPath() {
asteriskPrefix := ""
if ctx.externalAll {
asteriskPrefix = "*"
}

esm := ctx.esm
if ctx.target == "types" {
if strings.HasSuffix(esm.SubPath, ".d.ts") {
ctx.path = fmt.Sprintf(
"/%s%s/%s%s",
asteriskPrefix,
esm.Name(),
ctx.getBuildArgsPrefix(true),
esm.SubPath,
)
} else {
ctx.path = "/" + esm.Specifier()
}
return
}

name := strings.TrimSuffix(path.Base(esm.PkgName), ".js")
if esm.SubModuleName != "" {
if esm.SubModuleName == name {
// if the sub-module name is same as the package name
name = "__" + esm.SubModuleName
} else {
name = esm.SubModuleName
}
// workaround for es5-ext "../#/.." path
if esm.PkgName == "es5-ext" {
name = strings.ReplaceAll(name, "/#/", "/%23/")
}
}

if ctx.dev {
name += ".development"
}
if ctx.bundleMode == BundleAll {
name += ".bundle"
} else if ctx.bundleMode == BundleFalse {
name += ".nobundle"
}
ctx.path = fmt.Sprintf(
"/%s%s/%s%s/%s.mjs",
asteriskPrefix,
esm.Name(),
ctx.getBuildArgsPrefix(ctx.target == "types"),
ctx.target,
name,
)
}

func (ctx *BuildContext) buildModule(analyzeMode bool) (meta *BuildMeta, includes [][2]string, err error) {
entry := ctx.resolveEntry(ctx.esm)
if entry.isEmpty() {
Expand Down Expand Up @@ -1443,10 +1448,9 @@ func (ctx *BuildContext) buildTypes() (ret *BuildMeta, err error) {

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.esm.Package())
if err != nil {
return
return err
}

if ctx.esm.GhPrefix || ctx.esm.PrPrefix {
Expand Down Expand Up @@ -1491,6 +1495,7 @@ func (ctx *BuildContext) install() (err error) {
if isMainModule {
ctx.esm.SubModuleName = ""
ctx.esm.SubPath = ""
ctx.rawPath = ctx.path
ctx.path = ""
}
}
Expand Down
4 changes: 4 additions & 0 deletions server/build_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (q *BuildQueue) run(task *BuildTask) {
q.lock.Lock()
q.queue.Remove(task.el)
delete(q.tasks, task.ctx.Path())
if task.ctx.rawPath != "" {
// the `Build` function may have changed the path
delete(q.tasks, task.ctx.rawPath)
}
q.chann += 1
q.lock.Unlock()

Expand Down
Loading
Loading