Skip to content

Experiment: compiling go packages -> JavaScript packages #613

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

Closed
paralin opened this issue Mar 22, 2017 · 3 comments
Closed

Experiment: compiling go packages -> JavaScript packages #613

paralin opened this issue Mar 22, 2017 · 3 comments

Comments

@paralin
Copy link
Contributor

paralin commented Mar 22, 2017

I'm currently planning an experimental fork of GopherJS that adds a compilation output mechanism I first brought up in #524. This issue is to track notes / ideas / results of this experiment, to evaluate if the mechanism might be useful to include in GopherJS, and if it's worth using over allowing GopherJS to produce its own output.

Goal: Build a compiler mode that produces a 1-1 pairing of Go package -> JS package with TypeScript definitions.

Motivations / Logic:

  • TypeScript provides strong typing to the JavaScript language, and is seeing rapid adoption in the ecosystem due to its additive nature.
  • WebPack is facing similar widespread adoption, due to its ability to efficiently process asset pipelines, cache results, and build hot-loaded bundles for development and production.
  • WebPack is also including building "tree-shaking" capabilities, where dead code is eliminated automatically at build time.
  • Large web-apps are usually written in TypeScript. Developers may want to import some of their Go code for use in the browser, without migrating completely to Go.
  • Go follows a strong package based architecture, which is similar enough to the ES6 package architecture to work in a 1-1 mapping (e.x. - init() calls can be executed at the module load time).

User-Story:

A developer has built an existing web-app for their project. They have Go code they use in their CLI that handles some concurrent-heavy behaviors that are optimal for Go code-sharing into the web browser. They want to call Go code from TypeScript in a type-safe way, without having to re-write the Go code in JS or track changes to the Go code that might require updates to the hand-written definitions.

Secondary/tertiary ideas:

  • WebPack plugin to handle importing Go code by paths directly (out of the goworkspace), along with go get etc as build steps.

Implementation Approach:

  • Define a mapping between Go package paths and NPM package names (likely developer specified, with some sort of registry / package.json in the root of the code project).
  • Map Go imports of other packages to require statements, possibly in ES6, that WebPack can statically analyze and optimize.
  • Produce JS packages for each Go package, possibly tracking version compatibility (through a static analysis results file, perhaps?)
@paralin
Copy link
Contributor Author

paralin commented Mar 22, 2017

Also: it should be possible to generate Go bindings for JS from TypeScript definitions. I'll investigate this as well.

@dmitshur
Copy link
Member

Thanks for letting us know.

There are multiple issues open that discuss the similar topic of "having GopherJS produce rather than a single javascript file, multiple files - one for each Go package". I'll close this in favor of #524 so there's one unified place to have that conversation.

If you want to have another place to post details specific to this effort, please feel free to link to it from #524.

@paralin
Copy link
Contributor Author

paralin commented Feb 13, 2018

@shurcooL mainly I was looking for RFC at the time. I ended up concluding that the approach is feasible and likely would produce decent code for the browser, but will require some time / effort to implement correctly.

Exciting to see the effort continuing in those issues, I look forward to seeing where the work goes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants