npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@telenko/node-mf

v0.0.7

Published

Module Federation plugin support for NodeJS

Downloads

5,294

Readme

About

Library is a set of webpack plugins which gives support of Module Federation into NodeJS.

Usage example with NextJS https://github.com/telenko/node-mf-example

Here is also my article about this library.

Purpose of creating

Release of Webpack Module Federation really have made a shift in modern architecture of web applications. But what about NodeJS and such frameworks like Next? What about SSR? I see for now a gap in webpack, so pure ModuleFederation plugin can not be used inside NodeJS environment. Muliple examples of NextJS+MF here https://github.com/module-federation/module-federation-examples with NextJS using only client-side rendering or not using remote deployed build at all.

What can I propose

I have implemented 2 plugins for webpack:

  1. NodeAsyncHttpRuntime - Plugin which should be used on a remote side to build remote scripts for NodeJS. But the main key of plugin is resolution of modules - it is done via http requests, so NodeJS can dynamically resolve child modules, load them and perform.
  2. NodeModuleFederation - Plugin is wrapper around origin WebpackModuleFederation plugin and adds NodeJS specific resolution of remote modules (same thing: resolution is done via http requests)

Getting started

1) On remote library:

1.1) Install package (remote can be either pure JS application or NodeJS application - doesn't matter) npm i --save-dev @telenko/node-mf

1.2. Customize webpack configuration, so it should now build 2 targets: for web (if you want legacy browser build) and for node (webpack for now doesn't support universal targets)

1.3) For node build add NodeAsyncHttpRuntime plugin and set webpack's 'target' flag to false

    //pseudocode
    module.exports = [
        {
            target: "web",
            ...webOptionsWebpack
        },
        {
            target: false,
            plugins: [
                new NodeAsyncHttpRuntime() //this is instead of target to make it work
            ],
            ...nodeOptionsWebpack
        }
    ];

1.4) Serve both builds

Full example is here https://github.com/telenko/node-mf-example/blob/master/remoteLib/webpack.config.js

2) On NodeJS application:

2.1) Install library npm i --save-dev @telenko/node-mf

2.2) Add NodeModuleFederation plugin to the webpack config (api parameters schema is same) !Note, remote script url should point to 'node' build (which should be built with NodeAsyncHttpRuntime)

  module.exports = {
      plugins: [
          new NodeModuleFederation({
              remotes: {
                  someLib: "someLib@http://some-url/remoteEntry.node.js"
              },
              shared: {
                  lodash: {
                      eager: true,
                      singleton: true,
                      requiredVersion: "1.1.2"
                  }
              }
          })
      ],
      ...otherWebpackOptions
  };

Usage with Next.js

As Next.js use an internal version of webpack, it's mandatory to use their version. To do so, use the second argument of the 2 plugins to send the "context" of the webpack options.

// next.config.js
module.exports = {
    webpack: (config, options) => {
        config.plugins.push(
            new NodeAsyncHttpRuntime({}, options)
        );
        return config
    },
}
// next.config.js
module.exports = {
    webpack: (config, options) => {
        config.plugins.push(
            new NodeModuleFederation({
                remotes: {
                    someLib: "someLib@http://some-url/remoteEntry.node.js"
                },
                shared: {
                    lodash: {
                        eager: true,
                        singleton: true,
                        requiredVersion: "1.1.2"
                    }
                }
            }, options)
        );
        return config
    },
}

Full example of setuping NextJS with SSR here https://github.com/telenko/node-mf-example/blob/master/host/next.config.js

Usage with runtime uris

If you do not know the real uri during the build step, you can use a promise which will be executed at runtime.

Here is an example based on telenko/node-mf-example to have an url according to the dev or CDN host. On the CDN, the assets are stored in a path according to the remote app name. The environment variable CDN_URL is only known on the production server so we set an alternative for the development env.

// host
module.exports = {
    webpack: (config, options) => {
        // [...]
        if (isServer) {
            // Set the public path of the remote as auto
            mfConf.remotes.remoteLib = "remoteLib@auto";
            // Parse the remote uri and return a promise template string with the runtime data
            mfConf.getRemoteUri = function (remoteUri) {
                const remoteName = remoteUri.split("@")[0];
                return `new Promise(r => {
                    const uri = "${process.env.CDN_URL ? `${process.env.CDN_URL}/${remoteName}` : 'http://localhost:3002'}";
                    r("${remoteName}@${uri}/node/remoteEntry.js");
                })`
            }
        }
        // [...]
    }
};
// remoteLib
const getConfig = (target) => ({
    plugins: [
        ...(target === "web"
            ? [
                new HtmlWebpackPlugin({
                    template: "./public/index.html"
                })
            ]
            : [
                new NodeAsyncHttpRuntime({
                    getBaseUri = function () {
                        // Return a promise template string with the runtime data
                        return `new Promise(r => {
                            const uri = "${process.env.CDN_URL ? `${process.env.CDN_URL}/remoteLib` :  'http://localhost:3002'}";
                            r("${uri}/${target}/");
                        })`
                    }
                })
            ]
        )
    ]
});

Risks

Aren't 2 build makes build-time 2 times longer?

Yes, if we speak about NextJS and SSR - we need both builds: for web and for node, and we have to start entire build separately without sharing built chunks. That will increase build time 2 times.

Security

Since NodeJS out of the box doesn't support remote scripts execution (like in browser we can add script tag) it looks like a hack calling http request then 'eval'-ing it. I'm not sure if it is safe to add remote script execution on server side, but it is the only one possible way to make it on server.

Roadmap

  1. https support
  2. Next.js support for NodeAsyncHttpRuntime plugin