Skip to content

Commit 13d7ae2

Browse files
committed
Merge pull request #257 from shurcooL/gopherjs-serve-precedence-fix
gopherjs serve: Fix precedence of served files.
2 parents 65c4fa7 + 14481e2 commit 13d7ae2

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

tool.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -458,36 +458,25 @@ type serveCommandFileSystem struct {
458458
}
459459

460460
func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
461-
for _, d := range fs.dirs {
462-
file, err := http.Dir(filepath.Join(d, "src")).Open(name)
463-
if err == nil {
464-
return file, nil
465-
}
466-
}
467-
468-
dir, _ := path.Split(name)
461+
dir, file := path.Split(name)
469462
base := path.Base(dir) // base is parent folder name, which becomes the output file name.
470463

471-
if strings.HasSuffix(name, "/"+base+".js.map") {
472-
if content, ok := fs.sourceMaps[name]; ok {
473-
return newFakeFile(base+".js.map", content), nil
474-
}
475-
}
464+
isPkg := file == base+".js"
465+
isMap := file == base+".js.map"
466+
isIndex := file == "index.html"
476467

477-
isIndex := strings.HasSuffix(name, "/index.html")
478-
isPkg := strings.HasSuffix(name, "/"+base+".js")
479-
if isIndex || isPkg {
468+
if isPkg || isMap || isIndex {
469+
// If we're going to be serving our special files, make sure there's a Go command in this folder.
480470
s := gbuild.NewSession(fs.options)
481471
buildPkg, err := gbuild.Import(path.Dir(name[1:]), 0, s.InstallSuffix(), fs.options.BuildTags)
482472
if err != nil || buildPkg.Name != "main" {
483-
return nil, os.ErrNotExist
484-
}
485-
486-
if isIndex {
487-
return newFakeFile("index.html", []byte(`<html><head><meta charset="utf-8"><script src="`+base+`.js"></script></head></html>`)), nil
473+
isPkg = false
474+
isMap = false
475+
isIndex = false
488476
}
489477

490-
if isPkg {
478+
switch {
479+
case isPkg:
491480
buf := bytes.NewBuffer(nil)
492481
browserErrors := bytes.NewBuffer(nil)
493482
exitCode := handleError(func() error {
@@ -519,9 +508,26 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
519508
buf = browserErrors
520509
}
521510
return newFakeFile(base+".js", buf.Bytes()), nil
511+
512+
case isMap:
513+
if content, ok := fs.sourceMaps[name]; ok {
514+
return newFakeFile(base+".js.map", content), nil
515+
}
522516
}
523517
}
524518

519+
for _, d := range fs.dirs {
520+
f, err := http.Dir(filepath.Join(d, "src")).Open(name)
521+
if err == nil {
522+
return f, nil
523+
}
524+
}
525+
526+
if isIndex {
527+
// If there was no index.html file in any dirs, supply our own.
528+
return newFakeFile("index.html", []byte(`<html><head><meta charset="utf-8"><script src="`+base+`.js"></script></head></html>`)), nil
529+
}
530+
525531
return nil, os.ErrNotExist
526532
}
527533

0 commit comments

Comments
 (0)