2.2.2 • Published 5 days ago

@module-federation/node v2.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 days ago

⚡ Features

  • Exposes two Webpack Plugins to enable Module Federation.
  • Can exported as UniversalFederationPlugin or NodeFederationPlugin with StreamingTargetPlugin
  • Allows server to fetch chunks across the network.
  • Allow distributed deployments of federated applications.

📦 Installation

To install the plugin run one of the following commands in your terminal for your application.

# npm
npm install @module-federation/node

# yarn
yarn add @module-federation/node

🚀 Usage

There are two approaches to using the plugins exported from this package, dependent on your use case.

UniversalFederationPlugin

This plugin is an abstraction over both NodeFederationPlugin and ModuleFederationPlugin. It will alternate between which it uses based on where the build is intended to be used.

If the build is intended to be used on the browser, it will use the standard ModuleFederationPlugin and bundle your code accordingly, however, if it is intended for server usage, it will use NodeFederationPlugin to create the bundle.

This simplifies the code required in your webpack.config.js to enable SSR Module Federation. It determines which platform it needs to build for based on two things:

  1. If the options passed to the plugin has specified isServer: true
  2. If the name assigned to the config being used is server

It accepts the other standard options from ModuleFederationPlugin as well. You can see an example usage below:

const { UniversalFederationPlugin } = require('@module-federation/node');

const config = {
  target: isServer ? false : 'web',
  plugins: [
    new UniversalFederationPlugin({
      name: 'website2',
      library: { type: 'commonjs-module' },
      isServer: true, // or false
      remotes: {},
      filename: 'remoteEntry.js',
      exposes: {
        './SharedComponent': './remoteServer/SharedComponent',
      },
    }),
  ],
};

NodeFederationPlugin and StreamingTargetPlugin

You can also use each of the underlying plugins individually if you need more control over when they are used.

At build time, you need to be aware if you're building for the server or for the browser. If it's building for server, we need to set target: false to allow the plugins to function correctly.

The NodeFederationPlugin follows the same API as the Module Federation Plugin and therefore should be a drop-in replacement if you already have it set up in your webpack.config.js.

🔧 Config Example

An example configuration is presented below:

const { NodeFederationPlugin, StreamingTargetPlugin } = require('@module-federation/node');

const config = {
  target: isServer ? false : 'web',
  plugins: [
    new NodeFederationPlugin({
      name: 'website2',
      library: { type: 'commonjs-module' },
      remotes: {},
      filename: 'remoteEntry.js',
      exposes: {
        './SharedComponent': './remoteServer/SharedComponent',
      },
    }),
    new StreamingTargetPlugin({
      name: 'website2',
      library: { type: 'commonjs-module' },
      remotes: {},
    }),
  ],
};

Utilities

This package also exposes a few utilities to help with the setup of your federated application.

revalidate

Used to "hot reload" the federated application.

  • This is useful when you're developing your federated application and want to see changes without having to restart the server.
  • Also useful for production environments where you want to be able to update the federated application without having to restart the server.
import { revalidate } from '@module-federation/node/utils';

// we automatically reset require cache, so the reload callback is only if you need to do something else
revalidate().then((shouldReload) => {
  // do something extra after revalidation
  if (shouldReload) {
    // reload the server
  }
});

Hot reloading Express.js

Express has its own route stack, so reloading require cache will not be enough to reload the routes inside express.

//express.js
const app = express();

global.clearRoutes = () => {
  app._router.stack = app._router.stack.filter((k) => !(k && k.route && k.route.path));
};

// in some other file (within the scope of webpack build)
// wherever you have your revalidation logic
revalidate().then((shouldReload) => {
  if (shouldReload) {
    global.clearRoutes();
  }
});

🔑 License

👨‍💻 Contributors

List of our amazing contributors 💥

2.2.2

5 days ago

2.2.1

8 days ago

2.2.0

11 days ago

2.1.8

14 days ago

2.1.7

16 days ago

2.1.6

19 days ago

2.1.5

19 days ago

2.1.4

21 days ago

2.1.3

24 days ago

2.1.2

1 month ago

2.1.1

1 month ago

2.1.0

1 month ago

2.0.15

2 months ago

2.0.14

2 months ago

2.0.13

2 months ago

2.0.12

2 months ago

2.0.11

3 months ago

2.0.10

3 months ago

2.0.9

3 months ago

2.0.8

3 months ago

2.0.7

4 months ago

2.0.6

4 months ago

2.0.5

4 months ago

2.0.4

4 months ago

2.0.3

4 months ago

2.0.2

5 months ago

2.0.1

8 months ago

2.0.0

8 months ago

0.15.2-rc8.1

10 months ago

2.0.2-beta.0

8 months ago

2.0.2-beta.2

8 months ago

2.0.2-beta.3

7 months ago

2.0.2-beta.4

7 months ago

2.0.2-beta.5

7 months ago

2.0.2-beta.6

7 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago

0.16.3-rc.0

10 months ago

0.16.3-rc.1

10 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

2.1.0-canary.3

6 months ago

2.1.0-canary.2

6 months ago

2.1.0-canary.1

6 months ago

2.1.0-canary.6

6 months ago

0.16.0

10 months ago

2.1.0-canary.5

6 months ago

0.16.1

10 months ago

2.1.0-canary.4

6 months ago

0.16.2

10 months ago

0.15.2-rc8.0

10 months ago

0.15.2-rc7.0

10 months ago

0.15.2-rc1.0

11 months ago

0.14.3-beta.0

1 year ago

0.15.2-rc5.0

11 months ago

0.15.2-rc3.2

11 months ago

0.15.2-rc3.1

11 months ago

0.15.2-rc3.0

11 months ago

0.14.7-beta.3

12 months ago

0.14.7-beta.4

12 months ago

0.14.7-beta.5

12 months ago

0.14.7-beta.0

12 months ago

0.14.5-beta.0

12 months ago

0.14.7-beta.1

12 months ago

0.14.7-beta.2

12 months ago

0.15.0

12 months ago

0.15.1

12 months ago

0.14.7-rc.5x

12 months ago

0.15.2-beta.0

11 months ago

0.15.2-rc6.0

11 months ago

0.14.4-beta.0

12 months ago

0.15.2-rc4.0

11 months ago

0.14.5

12 months ago

0.14.6

12 months ago

0.14.7

12 months ago

0.14.7-rc.3

12 months ago

0.14.7-rc.2

12 months ago

0.14.7-rc.4

12 months ago

0.14.7-rc.1

12 months ago

0.14.7-rc.0

12 months ago

0.14.3

1 year ago

0.14.4

12 months ago

0.15.2-rc.0

12 months ago

0.14.1-beta.0

1 year ago

0.13.0

1 year ago

0.13.1

1 year ago

0.14.2-beta.1

1 year ago

0.12.0

1 year ago

0.12.1

1 year ago

0.14.0

1 year ago

0.12.2

1 year ago

0.14.1

1 year ago

0.12.3

1 year ago

0.14.2

1 year ago

0.13.1-beta.0

1 year ago

0.13.1-beta.1

1 year ago

0.11.0

1 year ago

0.11.1

1 year ago

0.10.3

1 year ago

0.10.4

1 year ago

0.10.5

1 year ago

0.9.8

1 year ago

0.9.7

1 year ago

0.9.9

1 year ago

0.9.4

1 year ago

0.9.10

1 year ago

0.9.6

1 year ago

0.9.11

1 year ago

0.9.5

1 year ago

0.10.1

1 year ago

0.10.2

1 year ago

0.10.0

1 year ago

0.9.0

1 year ago

0.9.1

1 year ago

0.6.7

2 years ago

0.8.4

2 years ago

0.6.6

2 years ago

0.5.7

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.8.1

2 years ago

0.6.3

2 years ago

0.8.0

2 years ago

0.6.2

2 years ago

0.5.3

2 years ago

0.4.4

2 years ago

0.8.3

2 years ago

0.6.5

2 years ago

0.5.6

2 years ago

0.8.2

2 years ago

0.6.4

2 years ago

0.5.5

2 years ago

0.5.0

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.5.2

2 years ago

0.4.3

2 years ago

0.6.0

2 years ago

0.4.2

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago