Skip to content

Add optional root argument to gopherjs serve. Fixes #384 #429

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 3 commits into from
Mar 31, 2016
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ For example, navigating to `http://localhost:8080/example.com/user/project/` sho

Refreshing in the browser will rebuild the served files if needed. Compilation errors will be displayed in terminal, and in browser console. Additionally, it will serve $GOROOT and $GOPATH for sourcemaps.

If you include an argument, it will be the root from which everything is served. For example, if you run gopherjs serve github.com/user/project then the generated JavaScript for the package github.com/user/project/mypkg will be served at http://localhost:8080/mypkg/mypkg.js.

### Performance Tips

- Use the `-m` command line flag to generate minified code.
Expand Down
38 changes: 33 additions & 5 deletions tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func main() {
}

cmdServe := &cobra.Command{
Use: "serve",
Use: "serve [root]",
Short: "compile on-the-fly and serve",
}
cmdServe.Flags().AddFlag(flagVerbose)
Expand All @@ -498,7 +498,24 @@ func main() {
cmdServe.Run = func(cmd *cobra.Command, args []string) {
options.BuildTags = strings.Fields(*tags)
dirs := append(filepath.SplitList(build.Default.GOPATH), build.Default.GOROOT)
sourceFiles := http.FileServer(serveCommandFileSystem{options: options, dirs: dirs, sourceMaps: make(map[string][]byte)})
var root string

if len(args) > 1 {
cmdServe.Help()
return
}

if len(args) == 1 {
root = args[0]
}

sourceFiles := http.FileServer(serveCommandFileSystem{
serveRoot: root,
options: options,
dirs: dirs,
sourceMaps: make(map[string][]byte),
})

ln, err := net.Listen("tcp", addr)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down Expand Up @@ -539,12 +556,15 @@ func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
}

type serveCommandFileSystem struct {
serveRoot string
options *gbuild.Options
dirs []string
sourceMaps map[string][]byte
}

func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
name := path.Join(fs.serveRoot, requestName[1:]) // requestName[0] == '/'

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

Expand All @@ -555,7 +575,7 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
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)
pkg, err := gbuild.Import(path.Dir(name[1:]), 0, s.InstallSuffix(), fs.options.BuildTags)
pkg, err := gbuild.Import(path.Dir(name), 0, s.InstallSuffix(), fs.options.BuildTags)
if err != nil || pkg.Name != "main" {
isPkg = false
isMap = false
Expand Down Expand Up @@ -604,7 +624,15 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
}

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

f, err := dir.Open(name)
if err == nil {
return f, nil
}

// source maps are served outside of serveRoot
f, err = dir.Open(requestName)
if err == nil {
return f, nil
}
Expand Down