Skip to content

Commit 234e9ee

Browse files
committed
Merge pull request #429 from jargv/master
Add optional root argument to gopherjs serve. Fixes #384
2 parents 6485815 + fe61552 commit 234e9ee

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ For example, navigating to `http://localhost:8080/example.com/user/project/` sho
3535

3636
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.
3737

38+
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.
39+
3840
### Performance Tips
3941

4042
- Use the `-m` command line flag to generate minified code.

tool.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ func main() {
485485
}
486486

487487
cmdServe := &cobra.Command{
488-
Use: "serve",
488+
Use: "serve [root]",
489489
Short: "compile on-the-fly and serve",
490490
}
491491
cmdServe.Flags().AddFlag(flagVerbose)
@@ -498,7 +498,24 @@ func main() {
498498
cmdServe.Run = func(cmd *cobra.Command, args []string) {
499499
options.BuildTags = strings.Fields(*tags)
500500
dirs := append(filepath.SplitList(build.Default.GOPATH), build.Default.GOROOT)
501-
sourceFiles := http.FileServer(serveCommandFileSystem{options: options, dirs: dirs, sourceMaps: make(map[string][]byte)})
501+
var root string
502+
503+
if len(args) > 1 {
504+
cmdServe.Help()
505+
return
506+
}
507+
508+
if len(args) == 1 {
509+
root = args[0]
510+
}
511+
512+
sourceFiles := http.FileServer(serveCommandFileSystem{
513+
serveRoot: root,
514+
options: options,
515+
dirs: dirs,
516+
sourceMaps: make(map[string][]byte),
517+
})
518+
502519
ln, err := net.Listen("tcp", addr)
503520
if err != nil {
504521
fmt.Fprintln(os.Stderr, err)
@@ -539,12 +556,15 @@ func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
539556
}
540557

541558
type serveCommandFileSystem struct {
559+
serveRoot string
542560
options *gbuild.Options
543561
dirs []string
544562
sourceMaps map[string][]byte
545563
}
546564

547-
func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
565+
func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
566+
name := path.Join(fs.serveRoot, requestName[1:]) // requestName[0] == '/'
567+
548568
dir, file := path.Split(name)
549569
base := path.Base(dir) // base is parent folder name, which becomes the output file name.
550570

@@ -555,7 +575,7 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
555575
if isPkg || isMap || isIndex {
556576
// If we're going to be serving our special files, make sure there's a Go command in this folder.
557577
s := gbuild.NewSession(fs.options)
558-
pkg, err := gbuild.Import(path.Dir(name[1:]), 0, s.InstallSuffix(), fs.options.BuildTags)
578+
pkg, err := gbuild.Import(path.Dir(name), 0, s.InstallSuffix(), fs.options.BuildTags)
559579
if err != nil || pkg.Name != "main" {
560580
isPkg = false
561581
isMap = false
@@ -604,7 +624,15 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
604624
}
605625

606626
for _, d := range fs.dirs {
607-
f, err := http.Dir(filepath.Join(d, "src")).Open(name)
627+
dir := http.Dir(filepath.Join(d, "src"))
628+
629+
f, err := dir.Open(name)
630+
if err == nil {
631+
return f, nil
632+
}
633+
634+
// source maps are served outside of serveRoot
635+
f, err = dir.Open(requestName)
608636
if err == nil {
609637
return f, nil
610638
}

0 commit comments

Comments
 (0)