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
cmd/go: provide some way to bundle raw JavaScript during wasm compilation #27626
Comments
This seems like an area where it would help to see a specific proposal. |
Drive-by thought. Is there some trick that can be performed here using That way we could avoid the |
If I'm not mistaken, this would require that any such programs be executed with a potentially dangerous content security policy. |
@flimzy seems to work unless I'm missing something really obvious?
gives:
(as it does in the browser) |
@myitcv: I believe the default CSP is wide-open, allowing eval. But if you tighten down security, you'll typically disallow the |
@flimzy got it, thanks. In which case, doesn't that preclude the loading of any JavaScript from within a WASM context? |
@myitcv I don't know the answer to that. |
I've just read the CGO introduction, to familiarize myself with the approach taken there. Based on that, my more specific proposal would be the following: A new special import package (akin to When the Go tool sees this special import is used in a package, then any The contents of each
This This allows the included A simple example: main.js:
main.go:
Build constraints for the This differs from GopherJS's implementation in two key ways:
A few additional thoughts I haven't fully considered:
|
The Go compiler generates wasm binaries. The only way to include JavaScript code in a wasm binary is to use I see no simple way on how something like the |
CGO consumes C code, but still emits a single machine code binary. The same is not possible with JavaScript, since it can not be compiled to WebAssembly. The Go compiler only emits a WebAssembly binary, which may or may not run on a JavaScript host. Also emitting a JavaScript file would be the job of a web application bundler, not the Go compiler itself. Closing this as out of scope for the compiler itself. An implementation with |
What version of Go are you using (
go version
)?Go 1.11
Does this issue reproduce with the latest release?
Yes
What did you expect to see?
GopherJS supports the option to bundle raw JavaScript in the compiled output by including files named
*.inc.js
in the Go source directory. Go's wasm support doesn't offer anything comparable, as far as I am aware.What did you see instead?
I would like to see the functional equivalent included when compiling to a wasm target.
I say functionally equivalent, because identical might be dangerous, in that it could make isomorphic GopherJS/GoWASM packages difficult, if there are ever incompatibilities. It is likely that one might need a particular
*.inc.js
file for GopherJS support, and a separate one for Go/WASM support. And at least at present, build tags in these files are broken for GopherJS (see gopherjs/gopherjs#468), so unless that is resolved, or some other mutual-exclusion scheme is provided, the simplest path forward may be to use an entirely different convention for Go/WASM.No doubt, many lessons from CGO can also be applied here, but this is an area where I have no direct experience.
The text was updated successfully, but these errors were encountered: