cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.46k stars 629 forks source link

🚀 Feature Request: Esbuild External #6231

Open emersonlaurentino opened 2 weeks ago

emersonlaurentino commented 2 weeks ago

Describe the solution

Add support to configure external libs to esbuild config.

Explanation:

Currently, I'm using the @node-rs/argon2 dependency, and I'm receiving this error when run the command wrangler dev:

Could not resolve "@node-rs/argon2-wasm32-wasi"

    node_modules/@node-rs/argon2/browser.js:1:14:
      1 │ export * from '@node-rs/argon2-wasm32-wasi'
        ╵               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "@node-rs/argon2-wasm32-wasi" as external to exclude it from the bundle, which will remove this error.

The solution is adding this lib to external on esbuild config, but we can't because we don't have access to esbuild config.

threepointone commented 2 weeks ago

I'm not sure marking it as external would solve your problem, since during runtime it would still try to import it and fail. I'd be curious why the module doesn't exist for you, do you need to manually install it?

As a workaround we support module aliasing now (via https://github.com/cloudflare/workers-sdk/pull/6167, pending documentation), so you can add this to wrangler.toml

[alias]
"@node-rs/argon2-wasm32-wasi" = "./some-file.js"

so that it resolves to something you export in some-file.js. I'm not sure it fixes your problem, but may give you a path forward.

petebacondarwin commented 1 week ago

I guess the problem is that since it is a WASI library it will be pulling in a WASM binary file, which cannot be bundled directly. I wonder if adding a "rule" in wrangler.toml to tell that this file is a WASM would help:

wrangler.toml

[[rules]]
globs = ["@node-rs/argon2-wasm32-wasi"]
type = "CompiledWasm"