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
How to use esbuild with Cloudflare Workers? #1189
Comments
It's possible. I have a It works with most projects ootb. Important things to note are the target version and output format. |
@lukeed Can you share the optimal target and output options for Cloudflare Workers? I been tried a few combinations, but I can't get the project above working. |
@rfgamaral https://github.com/lukeed/cfw/blob/master/src/commands/build.ts#L9-L21 My target is usually esnext, only cuz I don't use much modern syntax. To be safe, you probably want your |
@lukeed Tried a few combinations taking into account your code, but I can't get it working, same issue, |
One cause of this could be that you are using a package which uses |
I've researched a bit more and found this Cloudflare blog post: Somewhere in there:
In other words, I don't think I'm meant to build with
The same doesn't happen when using Webpack, and everything works with Cloudflare Workers when using Webpack. Maybe Webpack is bundling Assuming that's the problem, is it possible to configure esbuild to also bundle those libraries just like Webpack? |
The error is likely thrown by the Although esbuild isn't designed with Cloudflare Workers in mind at all, you may be able to get this to work by setting |
Tried with
I didn't really want to use Webpack, esbuild looks so much better and faster, but I guess I don't have a choice... 😞 |
If you read the docs for
You can try this approach with esbuild as well. In that case you could call esbuild's JS API, use |
@evanw Thank you, I think I understand this now. I wasn't able to get the example project I posted initially working, but I was close (it's missing the auto polyfill bit, but I can't be bothered by that now). However, I was able to apply what I learned here into my real project, and that one built successfully. Thank you so much. |
Has anyone achieved Cloudflare Workers blunding with Many @threepointone @petebacondarwin what do you use for wrangler 2? @evanw How about something like |
Hi @brillout.
Perhaps you could elaborate on the problems with this? |
Webpack provides a |
The main problem is that esbuild resolves the wrong value of |
I've released https://github.com/brillout/build-worker. It's a thin wrapper that configures esbuild for Cloudflare Workers. This esbuild config: https://github.com/brillout/build-worker/blob/master/build-worker.mjs. This will be the official recommendation for vite-plugin-ssr 0.4. (FYI vite-plugin-ssr.com.) CC @dan-lee. |
# Motivation Remove global polyfill and add conditions in esbuild for better compatibility with web worker. # Notes I found this change in esbuild repo discussion [#1189](evanw/esbuild#1189) and implemented in in Juno friday to fix the compatibility with web workers [#37](junobuild/juno-js#37). I have packaged all libs and tested in NNS-dapp.
I first created a Cloudflare Worker project with:
And then ran
npm install
followed bywrangler dev
. A server was available athttp://127.0.0.1:8787
, and the GraphQL server seemed to be working (despite having a bunch of errors related tochokidar
). The next step was to replace the default Webpack bundler withesbuild
. I have a demo repository available here. To testesbuild
:wrangler.toml
towrangler.webpack.toml
wrangler.esbuild.toml
towrangler.toml
wrangler dev
You'll need to install
wrangler
as a global dependency, have a Cloudflare account and change theaccount_id
in the*.toml
file to actually test this through.The above setup is not working with
esbuild
... After runningwrangler dev
, a server is available athttp://127.0.0.1:8787
. Opening that link on a browser results in a web page with a "Worker threw exception" error page, and the following error on the console:I'm not quite sure what I'm missing here, and I'd love to use
esbuild
with Cloudflare Workers, if possible. Hopefully this is a bad configuration on my side, and not an incompatibility onesbuild
or Cloudflare Workers.Any help is appreciated 🙏
The text was updated successfully, but these errors were encountered: