diff --git a/tool.go b/tool.go index 0141da04b..f9c54eb17 100644 --- a/tool.go +++ b/tool.go @@ -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(`
`)), nil + isPkg = false + isMap = false + isIndex = false } - if isPkg { + switch { + case isPkg: buf := bytes.NewBuffer(nil) browserErrors := bytes.NewBuffer(nil) exitCode := handleError(func() error { @@ -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(``)), nil + } + return nil, os.ErrNotExist }