nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.77k stars 2.37k forks source link

Increase Bundle Size for Module Federation 19.7.2 #27916

Open sorozco653 opened 2 months ago

sorozco653 commented 2 months ago

Current Behavior

I've noticed that after upgrading my project from v19.4.4 to the latest version of Nx v19.7.2, the bundle size has increased from 660 kB to 1.29 MB, almost doubling in size. I haven't updated any module-federation configs, so I'm curious if the v2 of the module federation enhancement is causing a significant increase. Is this increase expected? Are there any best practices I can use to reduce our bundle size?

Before: image

After: image

Here's an another example with simple MFE repo image

Expected Behavior

Ideally, the bundle size should stay the relatively the same

GitHub Repo

N/A

Steps to Reproduce

  1. Create an MFE host and remote app w/nx 19.7.2
  2. create a library that uses loadRemoteModule
  3. Build the libraries and compare

Nx Report

Node           : 20.12.2
OS             : darwin-arm64
Native Target  : aarch64-macos
yarn           : 4.4.1

nx (global)        : 19.5.6
nx                 : 19.7.2
@nx/js             : 19.7.2
@nx/jest           : 19.7.2
@nx/linter         : 19.7.2
@nx/eslint         : 19.7.2
@nx/workspace      : 19.7.2
@nx/angular        : 19.7.2
@nx/devkit         : 19.7.2
@nx/eslint-plugin  : 19.7.2
@nx/playwright     : 19.7.2
@nrwl/tao          : 19.7.2
@nx/vite           : 19.7.2
@nx/web            : 19.7.2
@nx/webpack        : 19.7.2
nx-cloud           : 19.1.0
typescript         : 5.5.4
---------------------------------------
Community plugins:
@ngrx/operators : 18.0.2
@ngrx/signals   : 18.0.2
ng-mocks        : 14.13.1

Failure Logs

N/A

Package Manager Version

4.4.1

Operating System

Additional Information

No response

Coly010 commented 2 months ago

Hey @sorozco653!

This is expected, for now.

Module Federation 2.0 bundles the federtaion runtime into the remoteEntry files. There is work being done currently by Zack Jackson to offer avenues to remove the federation runtime from the remoteEntry files: https://github.com/module-federation/core/pull/2932

sorozco653 commented 1 month ago

It looks like https://github.com/module-federation/core/pull/2932 has been merged and release with v0.6.7

Coly010 commented 1 month ago

It has, but there's a potential bug introduced with that release also.

I'll be investigating it further

sorozco653 commented 1 month ago

Any update with this?

Greech commented 1 week ago

@Coly010 any news on the topic?

We are currently upgrading to Angular 18, and given our cross-platform setup, this is significantly impacting our operations.

Could you please provide any updates on this? Additionally, if there are any actions or patches required on our end, that information would be greatly appreciated.

Thank you!

Coly010 commented 1 week ago

Update:

Module Federation has an option under experiments API called federationRuntime which allows for the option hoisted.

This option removes the runtime code from the various entry chunks, relying instead on the host having the runtime available to be used. We do not currently expose this option, however, we will be shortly. There was some confusion around the impact that setting this option would have caused that I had to get clarified before I could suggest it as an option.

I'll update here again once there is a PR in place exposing that option.

Greech commented 1 week ago

@Coly010 thanks! Waiting on the PR.