Skip to content
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

using plugins in a go environment #62

Open
kenhowardpdx opened this issue Jun 6, 2021 · 3 comments
Open

using plugins in a go environment #62

kenhowardpdx opened this issue Jun 6, 2021 · 3 comments

Comments

@kenhowardpdx
Copy link

Is it possible to leverage javascript modules as plugins when running in go? I'd like to avoid having the node ecosystem in my go project if possible. Any strategy suggestions to vendor a js plugin and use it with the esbuild go flavor?

@evanw
Copy link
Contributor

evanw commented Jun 6, 2021

This was not a design goal, so this is hard to do. The JS ecosystem revolves around JS and most libraries that you would want to integrate into esbuild are also written in JS, so esbuild's design prioritizes JS interop.

Being written in Go was originally an implementation detail. I have exposed a Go API because people wanted it, and because you can use it to write a very high-performance custom bundler with enough work. But I believe that a JS-based API for esbuild is the best way to work with JS-based libraries so esbuild's plugin API is primarily designed around the JS use case.

Doing what you want is not impossible. You could write an esbuild plugin in Go that launches node as a child process and communicates with it over IPC using whatever communication method you feel like (stdin/stdout, HTTP, WebSockets, etc.) and then in the child node process, you could run any code you want including JS-based esbuild plugins. But esbuild hasn't been designed for this use case so you'd be on your own building all of that.

TL;DR: If you need to run JS code, esbuild's design is such that you want to use the JS API. The Go API is only for cases where you only need to run Go code.

@kenhowardpdx
Copy link
Author

Thank you for responding to my question. I think there's a place for a tool like this in the go ecosystem that would allow folks to keep front-end bundling separate but collocated with the backend.

I'll have to explore that go plugin route you described.

@zaydek
Copy link
Contributor

zaydek commented Aug 28, 2021

@kenhowardpdx I wanted something similar and ended up implementing a Node <-> Go layer so I can have the best of both worlds. I'm still using Node / esbuild but I get to use Go for everything else.

It's a little convoluted but it gets the job done. Essentially I setup IPC in Go using stdio pipes (https://github.com/zaydek/retro/blob/master/pkg/ipc/ipc.go) and then Node responds to events from Go (https://github.com/zaydek/retro/blob/master/scripts/backend.ts#L183).

Evan helped me figure out how to implement something like this between previous issues and looking at his source and I tried to implement the simplest version for my use case. It works and I rely on it everyday!

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

3 participants