Skip to content

Commit af6637d

Browse files
committed
Add optional root argument to gopherjs serve. Fixes #384
1 parent 14ba2f5 commit af6637d

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
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: 23 additions & 4 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,14 @@ 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] == '/'
548567
dir, file := path.Split(name)
549568
base := path.Base(dir) // base is parent folder name, which becomes the output file name.
550569

@@ -555,7 +574,7 @@ func (fs serveCommandFileSystem) Open(name string) (http.File, error) {
555574
if isPkg || isMap || isIndex {
556575
// If we're going to be serving our special files, make sure there's a Go command in this folder.
557576
s := gbuild.NewSession(fs.options)
558-
pkg, err := gbuild.Import(path.Dir(name[1:]), 0, s.InstallSuffix(), fs.options.BuildTags)
577+
pkg, err := gbuild.Import(path.Dir(name), 0, s.InstallSuffix(), fs.options.BuildTags)
559578
if err != nil || pkg.Name != "main" {
560579
isPkg = false
561580
isMap = false

0 commit comments

Comments
 (0)