From 17c562f502969ace338891f93f5df40b7f6cbf57 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Wed, 15 Jul 2015 00:07:40 -0700 Subject: [PATCH 1/2] gopherjs serve: Fix precedence of served files. Previously, if there was already a {base}.js file on disk (possibly as a result of running `gopherjs build` in the past), it would take higher precedence than the freshly compiled {base}.js file. Fix that by rearranging the serving precedence logic as follows: 1. {base}.js && valid Go command in folder -> compiled {base}.js 2. {base}.js.map && valid Go command in folder -> compiled {base}.js.map 3. Look in all Go workspaces (GOPATH and GOROOT) 4. index.html && valid Go command in folder -> our own stub index.html This way, if there's an index.html file on disk, it takes higher precedence, but not {base}.js and {base}.js.map files. --- tool.go | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/tool.go b/tool.go index 0141da04b..6f7bc463a 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) 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 - } - } - - isIndex := strings.HasSuffix(name, "/index.html") isPkg := strings.HasSuffix(name, "/"+base+".js") - if isIndex || isPkg { + isMap := strings.HasSuffix(name, "/"+base+".js.map") + isIndex := strings.HasSuffix(name, "/index.html") + + 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 + isPkg = false + isMap = false + isIndex = false } - if isIndex { - return newFakeFile("index.html", []byte(``)), nil - } - - 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 { + file, err := http.Dir(filepath.Join(d, "src")).Open(name) + if err == nil { + return file, 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 } From 14481e20e5facf9364bb44384332ce3af15dace5 Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Wed, 15 Jul 2015 00:14:13 -0700 Subject: [PATCH 2/2] gopherjs serve: Simplify filename matching logic. --- tool.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tool.go b/tool.go index 6f7bc463a..f9c54eb17 100644 --- a/tool.go +++ b/tool.go @@ -458,12 +458,12 @@ type serveCommandFileSystem struct { } func (fs serveCommandFileSystem) Open(name string) (http.File, error) { - dir, _ := path.Split(name) + dir, file := path.Split(name) base := path.Base(dir) // base is parent folder name, which becomes the output file name. - isPkg := strings.HasSuffix(name, "/"+base+".js") - isMap := strings.HasSuffix(name, "/"+base+".js.map") - isIndex := strings.HasSuffix(name, "/index.html") + isPkg := file == base+".js" + isMap := file == base+".js.map" + isIndex := file == "index.html" if isPkg || isMap || isIndex { // If we're going to be serving our special files, make sure there's a Go command in this folder. @@ -517,9 +517,9 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) { } for _, d := range fs.dirs { - file, err := http.Dir(filepath.Join(d, "src")).Open(name) + f, err := http.Dir(filepath.Join(d, "src")).Open(name) if err == nil { - return file, nil + return f, nil } }