Skip to content

gopherjs serve: Fix precedence of served files. #257

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

Merged
merged 2 commits into from
Jul 15, 2015
Merged
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
50 changes: 28 additions & 22 deletions tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,36 +458,25 @@ type serveCommandFileSystem struct {
}

func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
for _, d := range fs.dirs {
file, err := http.Dir(filepath.Join(d, "src")).Open(name)
if err == nil {
return file, nil
}
}

dir, _ := path.Split(name)
dir, file := path.Split(name)
base := path.Base(dir) // base is parent folder name, which becomes the output file name.

if strings.HasSuffix(name, "/"+base+".js.map") {
if content, ok := fs.sourceMaps[name]; ok {
return newFakeFile(base+".js.map", content), nil
}
}
isPkg := file == base+".js"
isMap := file == base+".js.map"
isIndex := file == "index.html"

isIndex := strings.HasSuffix(name, "/index.html")
isPkg := strings.HasSuffix(name, "/"+base+".js")
if isIndex || isPkg {
if isPkg || isMap || isIndex {
// If we're going to be serving our special files, make sure there's a Go command in this folder.
s := gbuild.NewSession(fs.options)
buildPkg, err := gbuild.Import(path.Dir(name[1:]), 0, s.InstallSuffix(), fs.options.BuildTags)
if err != nil || buildPkg.Name != "main" {
return nil, os.ErrNotExist
}

if isIndex {
return newFakeFile("index.html", []byte(`<html><head><meta charset="utf-8"><script src="`+base+`.js"></script></head></html>`)), nil
isPkg = false
isMap = false
isIndex = false
}

if isPkg {
switch {
case isPkg:
buf := bytes.NewBuffer(nil)
browserErrors := bytes.NewBuffer(nil)
exitCode := handleError(func() error {
Expand Down Expand Up @@ -519,9 +508,26 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
buf = browserErrors
}
return newFakeFile(base+".js", buf.Bytes()), nil

case isMap:
if content, ok := fs.sourceMaps[name]; ok {
return newFakeFile(base+".js.map", content), nil
}
}
}

for _, d := range fs.dirs {
f, err := http.Dir(filepath.Join(d, "src")).Open(name)
if err == nil {
return f, nil
}
}

if isIndex {
// If there was no index.html file in any dirs, supply our own.
return newFakeFile("index.html", []byte(`<html><head><meta charset="utf-8"><script src="`+base+`.js"></script></head></html>`)), nil
}

return nil, os.ErrNotExist
}

Expand Down