module-federation / core

Module Federation is a concept that allows developers to share code and resources across multiple JavaScript applications
https://module-federation.io/
MIT License
1.54k stars 238 forks source link

Bun Support #1373

Closed sauldhernandez closed 1 year ago

sauldhernandez commented 1 year ago

Hi! I was wondering if you were contemplating in adding module federation support for Bun (https://bun.sh). They seem to have a plugin mechanism but I'm unsure if the hooks they have would be enough to support it: https://bun.sh/docs/runtime/plugins

Happy to contribute if pointed in the right direction and this is something that can be accomplished.

ScriptedAlchemy commented 1 year ago

couldnt bun run a webpack compile? webpack is just JS so is the runtime able to execute a webpack build?

ScriptedAlchemy commented 1 year ago

Bun's plugin API is based on esbuild. Only a subset of the esbuild API is implemented, but some esbuild plugins "just work" in Bun, like the official MDX loader:

Esbuild API design is far too limiting, if buns api is based off esbuild then its highly unlikely we would have near the api surface needed to add federation as a plugin.

Since bun is native, like https://rspack.dev it matters the amount of data we send between native and JS, federation needs a huge amount of graph data about the module tree, depending on how the config is interprated, if its going back to a native layer - theres limits in speed. For rspack we implemented it in rust internally as via JS would be too slow and exposing that many webpack hooks via rust is alot more work than just writing MFP in rust.

We may in the future be able to offer an "sdk" which would be framework agnostic and provide a runtime api/container factory #1170

However since its a JS runtime, you probbaly can run webpack --config=xx just fine

FFdhorkin commented 9 months ago

Bun is not currently compatible as of version 1.0.28. I have tracked down the issue to @module-federation/typescript's dependency on the node-fetch package. packages/typescript/src/lib/download.ts uses node:zlib's createBrotliDecompress, which Bun's current implementation is missing createBrotliDecompress, thus the failure I'm seeing.

Patching this locally to just use native fetch (no import) seems to solve the issue. Alternatively, replacing node-fetch with minipass-fetch seems to solve it (so presumably make-fetch-happen or similar would also work, though directly dropping in make-fetch-happen didn't work for me.)

The error itself was no help at all in tracking down the problem:

2024-02-20 15:13:55 $ rsbuild dev
2024-02-20 15:13:55   Rsbuild v0.3.11
2024-02-20 15:13:55 
2024-02-20 15:13:56 error   Failed to load file: /app/rsbuild.config.ts
2024-02-20 15:13:56 error   Failed to start dev server.
2024-02-20 15:13:56 error   First argument must be an Error object
2024-02-20 15:13:56     at ErrorPrepareStackTrace (native)
2024-02-20 15:13:56     at <anonymous> (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:0:160014)
2024-02-20 15:13:56     at captureStackTrace (native)
2024-02-20 15:13:56     at new CustomError (:620:6)
2024-02-20 15:13:56     at createErrorType (:627:28)
2024-02-20 15:13:56     at <anonymous> (:53:30)
2024-02-20 15:13:56     at evalModule (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:32578:18)
2024-02-20 15:13:56     at jiti (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:32542:23)
2024-02-20 15:13:56     at <anonymous> (:11:26)
2024-02-20 15:13:56     at evalModule (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:32578:18)
2024-02-20 15:13:56     at jiti (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:32542:23)
2024-02-20 15:13:56     at <anonymous> (:15:57)
2024-02-20 15:13:56     at evalModule (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:32578:18)
2024-02-20 15:13:56     at jiti (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:1848:642270)
2024-02-20 15:13:56     at <anonymous> (:20:32)
2024-02-20 15:13:56     at evalModule (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:32578:18)
2024-02-20 15:13:56     at jiti (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:1848:642270)
2024-02-20 15:13:56     at <anonymous> (:2:192)
2024-02-20 15:13:56     at evalModule (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:1848:643633)
2024-02-20 15:13:56     at jiti (/app/node_modules/@rsbuild/shared/compiled/jiti/index.js:32542:23)
2024-02-20 15:13:56     at <anonymous> (/app/node_modules/@rsbuild/core/dist/cli/config.js:104:21)
2024-02-20 15:13:56     at processTicksAndRejections (:13:78)
2024-02-20 15:13:56 error: script "start" exited with code 1

I am also filing a bug on node-fetch about this, as I imagine they could probably do a workaround.

FFdhorkin commented 9 months ago

It does appear that bun expects to add this functionality to its zlib within the next 2 months: https://github.com/oven-sh/bun/issues/267