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

mf-cra

v0.0.5

Published

ModuleFederation support for create-create-app

Downloads

792

Readme

MF-CRA

Module Federation support for Create-React-App without using "eject" or creating a fork of "react-scripts", just by adding a single configuration file moduleFederation.config.js at the root of your application

Usage

  1. Install the latest version of the package form npm as a dependency

    npm i mf-cra
  2. Create a MF configuration file in your project's root directory

    my-app
      ├── node_modules
    + ├── moduleFederation.config.js
      └── package.json
  3. Update the existing calls to react-scripts in the scripts section of your package.json to use the mf-cra CLI:

    "scripts": {
    -  "start": "react-scripts start",
    +  "start": "mf-cra start",
    -  "build": "react-scripts build",
    +  "build": "mf-cra build",
       "test": "react-scripts test",
       "eject": "react-scripts eject"
    }

Sample moduleFederation.config.js file

const { dependencies } = require('./package.json');

module.exports = {
  name: 'app_name', // change me
  filename: 'remoteEntry.js',
  exposes: {
    './hello': './src/hello.tsx',
  },
  remotes: {
    remote1: 'remote@http://route.to.remote/remoteEntry.js',
  },
  shared: {
    ...dependencies,
    react: {
      singleton: true,
      import: 'react',
      shareScope: 'default',
      requiredVersion: dependencies.react,
    },
    'react-dom': {
      singleton: true,
      requiredVersion: dependencies['react-dom'],
    },
    'react-router-dom': {
      singleton: true,
      requiredVersion: dependencies['react-router-dom'],
    },
  },
};

TIP: To statically import federated components, webpack needs a top level promise, so that it can pause the execution there and load the remote components before it can be rendered. To achieve this, rename src/index.js to src/bootstrap.js, and create a new file src/index.js and import bootstrap.js file in it with JavaScript's import function. This function returns a promise. e.g. import('bootstrap.js')

Dynamic Module Federation

You can use dynamic module federation feature to define remotes at run-time and load modules from them dynamically.

Example:

import useFederatedComponent from 'mf-cra';

export default function DynamicRemoteComponent() {
  const { Component, isError } = useFederatedComponent({
    remoteUrl: 'http://localhost:3001/remoteEntry.js',
    moduleToLoad: './hello',
    remoteName: 'remote_app',
  });

  if (isError) return <div>Error loading remote component</div>;

  return Component ? <Component /> : null;
}

Usage with typescript

import useFederatedComponent, { IFederatedComponent } from 'mf-cra';

interface Props {
  remote: IFederatedComponent;
}

export default function DynamicRemoteComponent({ remote }: Props) {
  const { Component, isError } = useFederatedComponent(remote);

  if (isError) return <div>Error loading remote component</div>;

  return Component ? <Component /> : null;
}

Troubleshoot

  • Page refresh on nested routes doesn't work with react-router-dom?

    Webpack config output.publicPath defaults to "auto" while using this library, Override this value with your application URL. Please define an env variable PUBLIC_URL=http://url.where.your.application.is.or.hosted, e.g. in local you can use PUBLIC_URL="http://localhost:3000", That should fix the issue