-
Notifications
You must be signed in to change notification settings - Fork 570
Add "gopherjs serve" command #121
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
Comments
What would it do? Would it be something like https://github.com/ajhager/srvi#srvi or https://github.com/shurcooL/cmd/blob/master/gopherjs_serve_html/main.go? |
Yes, something similar. I think that this functionality should be built-in. My plan is to have a command like
and it will spawn an HTTP server which will serve the compiled version and watch for changes. Additionally it will serve GOROOT and GOPATH for the source maps. |
👍 This is so important! |
I ended up writing one of these myself for testing purposes ... so yes, like Joseph, I think it is really important too. |
@neelance, do you want some help with this? I'm very comfortable using the http package in go and built something similar to what you're talking about in a static blog generator I'm working on. I understand this might be low priority, but it also seems relatively independent and a great candidate for something I could help with. Let me know and I'll work up a PR! |
Truth is, I'm undecided. I am very happy that you want to contribute. On the other hand I already have quite specific plans for this and it was one of the next things on my todo list, because I need it before starting to promote https://github.com/neelance/dom/tree/master/examples/todomvc . Speaking of promotion, publishing helper libraries, blog posts, etc. would be tremendously useful, because we still need to raise awareness. |
Totally understand if you think it's easier to just do it yourself. I would love to see gopherjs succeed, so I thought I'd offer to help here and see if it made sense. For now, I'll try and do what I can to help you with promotion. For me that means writing blog posts and building libraries on top of gopherjs. |
Initial implementation is done. You can try it on the example of https://github.com/neelance/dom . Feedback very welcome. :-) |
Just tried it out. My goal is to deprecate my personal tool and rely on So far it works well. It took me a minute or two to figure out. Instead of going to I like that it serves all of your GOPATH so can use same command to test multiple GopherJS projects (my personal tool only served current directory). But I wonder how that will handle projects that have static assets in working directory of the Go package. I also like that it serves sourcemaps, I did not have that luxury before. :) One minor thought/suggestion, I wish it would use port 8080 by default rather than 6060. 6060 is typically used by godoc, and I've always used 8080 for testing personal projects. I know it can be overridden, which is great, but I just think 8080 is a better default than 6060. I'll post more feedback as I have it, but so far I think this will work really well! |
It works for Go packages with multiple |
It works for static assets too, but only if I use relative paths. |
Changed default port to 8080. Yes, assets need relative paths. |
Some immediate notes:
|
Agree. I recommend the approach my tool has used. Just take the error output of GopherJS and call func handleJsError(jsCode string, err error) string {
if err != nil {
fmt.Fprintln(os.Stderr, err)
return `console.error("` + template.JSEscapeString(err.Error()) + `");`
}
return jsCode
} |
Also I just noticed that the default HTML file has just:
There isn't a |
Yeah, an empty body should be added to the default However, your script will not be able to access it directly, since the script is in head, and body is not yet available. If you want to do something with the body, you can use the func main() {
document.AddEventListener("DOMContentLoaded", false, func(_ dom.Event) {
runAndUseDocumentBody()
})
} |
Is there reason to not move the
Then code like this would work without an event handler:
|
Yes, the body is left out on purpose, exactly because it is not yet available in the head. The intended way is to create a body element and set it, see |
@neelance the SetBody approach works great for me! I wasn't aware that it was that easy (Apologies for the verbosity). I'd still like to see compilation errors go to the browser -- would you like me to file a separate issue for it? |
I agree with and echo what @slimsag said above. |
@slimsag No need for a separate issue. I'll add this soon. |
My |
@dustywilson, I also have I haven't noticed any issues with my own setup and |
I've looked at the code, and it's definitely trying to treat GOPATH as a list of workspaces and serve from all of them. @dustywilson, can you try explicitly going to the URL that you expect to be served? E.g., I have a feeling that Readdir implementation may not be joining contents of multiple folders, so it may be listing only folders in one GOPATH workspace but not others. But they are still served if you navigate to your target import path explicitly. @neelance, are you familiar with https://godoc.org/golang.org/x/tools/godoc/vfs#NameSpace? It may be useful here. It implements non-trivial logic to make |
Quite right, it does seem to point to the right files, even though the index fails to list them. Thanks! |
Hey guys, |
That is understandable. I agree that there should be just one official, recommended solution for this in GopherJS, and described in its README.
My main criteria for what I think would be good is not myself as a user, but other people. I want If I have to explain to others "well, to get this cool functionality that I'm showing you right now, you need to go get gopherjs, but then also install this 3rd party dependency and figure out how to use it; oh and you also need to install node", it's not a great solution. However, if GopherJS itself can itegrate webpack, perhaps that would work? By integrate, I mean perhaps in the README it can describe how to use it, or even embed it so that running Having looked at webpack documentation and introduction just now, I think it looks useful and thorough, but at the same time it feels too complex, advanced, and uses too many JavaScript/Node conventions for my liking (but that's my personal opinion). Plus it is not written in Go. So my initial reaction is that it doesn't seem like a great Given the scope and polish of the webpack project, I was not surprised to see it has 1200~ commits. I am actually working on what is likely effectively a "webpack competitor", but I haven't been able to get it finished enough to release it yet. It would be my solution for #98. But it's written in Go and uses code generation and is based on top of a virtual filesystem paradigm. So I can see the attractiveness of webpack and I want something similar, I just want to do it using Go. |
Now that gopherjs has a capable serve command built in (see gopherjs/gopherjs#121), it's better to suggest using it directly. This way there's no need to download and learn to use a separate command when this functionality is now available upstream in gopherjs itself. /cc @ajhager Also update description of what the update.sh script does. It does not pull the latest version, it simply does `go install ...gopherjs` which installs whatever version you have locally.
I believe the original issue, described as "Add "gopherjs serve" command", has been completed with the following changes:
IMO it works great and I'm happy to recommend it to other people rather than my own similar tool (which I can now deprecate). This issue is becoming longwinded and it's hard to track what actionable things are remaining. I'll close this issue since the original task is complete. @neelance, in the last few comments you started discussing webpack. Could you make make that a separate issue, if you still want to make that proposal and have a discussion about it? |
No description provided.
The text was updated successfully, but these errors were encountered: