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

@epam/microfrontends

v1.0.6

Published

Provides a set of helpers and CLI tools to facilitate development of micro-frontend widgets.

Downloads

932

Readme

@epam/microfrontends

This package contains utilities and scripts which simplify development of micro-frontends.

Definitions

  • Remote app - the application which hosts remote widgets.
  • Host app - the application which renders remote widgets.
  • Remote facade (widgets.js) - the unified interface which is hosted on "Remote app" and is used by "Host app" to render remote widgets.

Main idea

"Host App" retrieves remote widgets from "Remote app" using "Remote facade" which is located at well known URL.

It's better described by example. Let's assume that the "Remote app" is hosted on this origin: "http://127.0.0.1:4000" Host knows that "Remote facade" is located at well known URL which looks like this: "http://127.0.0.1:4000/widgets.js" There several other resources which are hosted by the "Remote app", and the "Host" doesn't know about presence of such resources. Though, they are used implicitly (by widgets.js) during the mount of the remote widgets. Here the list of resources:

  • Remote Assets Manifest (http://127.0.0.1:4000/<path_to_build_manifest>)
  • Remote App JS bundle (http://127.0.0.1:4000/<path_to_main_js_bundle>)
  • Remote App CSS bundle (http://127.0.0.1:4000/<path_to_main_css_bundle>)

Below is the sequence of actions which are performed by the Host in order to mount remote widget.

  1. LOAD
    // The "Remote facade" is loaded using well-known URL. Internally, the Host loads the widget like this.
    const remote = await import('http://127.0.0.1:4000/widgets.js');
  2. INIT
    /**
    * Several IMPLICIT actions are done during the init(). Host doesn't know anything about the actions, they are incapsulated inside the "widgets.js".
    * 1) "Remote Assets Manifest" file is loaded
    * 2) URL to the "Remote App JS bundle" is determined from the manifest
    * 3) URL to the "Remote App CSS bundle" is determined from the manifest
    * 4) window.Widgets global variable is created (unless already defined). It acts as a simple registry which helps to define/require widgets
    * 5) "Remote App JS bundle" is loaded via window.Widgets.require(/../) Note: "script" tag is used under the hood to load the JS bundle. It's loaded only ones and cached.
    * 6) widgets which are defined during the load (via the window.Widgets.define(...)) are collected.
    * Note: The "Remote App CSS bundle" is NOT loaded on this step. It will be loaded later, during widget "mount".
    */
    const widgets = await remote.init();
  3. MOUNT
    // The "Remote App CSS bundle" is loaded as part of this call (right before actual mount occurs)
    const { unmount } = widgets['demoWidget'].mount(/*...*/);
  4. UNMOUNT
    // The "Remote App CSS bundle" is unloaded after this step.
    unmount();

Usage example

(Remote) Generate remote facade (widgets.js)

Run the command below from the "remote" project's root folder. It creates "Remote facade" files and puts them to the ./public folder. NOTE: it needs to be run at the very first time and can only be run again when the @epam/microfrontends package version has changed.

# Example for CRA
# Option 1 (when "@epam/microfrontends" is installed locally)
npx epam-microfrontends-init --app-type=webpack
# Option 2
npx --yes @epam/microfrontends@latest epam-microfrontends-init --app-type=webpack

Next files will be created:

  • widgets.js (+ widgets.js.map) - The minified version of the "Remote facade" which is hosted on the remote side. The Host app downloads and runs this file in order to retrieve remote widgets.
  • widgets.dev.js and widgets-dev.html - The non-minified version of the "Remote facade". It might be used as a starting point to test remote widgets locally in isolation. NOTE: It's safe to remove these files unless you plan to locally test your widgets via "widgets-dev.html".

All options:

(Remote) Implement remote widgets

import { createRoot } from 'react-dom/client';
import { IHistoryMicroFe } from '@epam/microfrontends/helpers/history';
const demoWidget = {
    mount: (target: HTMLElement, params: any) => {
        /*
         * If a remote widget uses routing, then it's necessary to do all routing via the "params.historyMicroFe".
         * Use "params.historyMicroFe" directly or via converters exported from "@epam/microfrontends/helpers/history"
         */
        const root = createRoot(target);
        root.render(<DemoWidgetComponent />);
        return {
            unmount: () => {
                root.unmount();
            }
        }
    }
};
if (isPartOfTheNormalApp) {
    // render app
}
(window as any).Widgets?.define(() => ({
    demoWidget,
}));

(Host) Consume the remote widgets

import { RemoteWidget, RemoteWidgetContext } from '@epam/microfrontends/helpers/react';
import { IHistory4Full_to_IHistoryMicroFe } from '@epam/microfrontends/helpers/history';
import { createBrowserHistory } from 'history';
//...//
const history = createBrowserHistory();
const historyMicroFe = IHistory4Full_to_IHistoryMicroFe(history as any);
//...//
function App() {
    // Somewhere in the app:
    const params = { historyMicroFe };
    return (
        <RemoteWidgetContext.Provider value={params}>
            <RemoteWidget
                url="http://172.20.0.1:4001/widgets.js"
                name="demoWidget"
            />
            <RemoteWidget
                url="http://172.20.0.1:4002/widgets.js"
                name="demoWidget"
            />
        </>
    )
}

Routing in remote widgets

If some remote widget uses routing, then it's expected that it should use "historyMicroFe" standard interface which is provided by the "Host app". In general case, host and remote could use different routers, e.g.: (HOST: unknown 1) ---> (IHistoryMicroFe) --> (REMOTE: unknown 2)

In such situation, some adapters will be required:

  1. unknown 1 -> IHistoryMicroFe
  2. IHistoryMicroFe -> unknown 2

There are several adapters available OOTB in this package. They cover some typical uses cases:

  • IHistory4Full_to_IHistoryMicroFe
  • IRouter6_to_IHistoryMicroFe
  • IHistoryMicroFe_to_IHistoryRouter6
  • IHistoryMicroFe_to_IHistory4Full

Usage of "basename"

  • It's expected that "Host app" doesn't use "basename" feature of router. So that full location path is exposed to "Remote app".
  • The "Remote app" may optionally use "basename" for its routing if needed. See the example here: packages/examples/remotes/cra-router5/src/widgets/demoWidget/demoWidget.tsx

Features of the "Remote facade" (widgets.js)

  • Possibility to customize mount, possible options:
    • shadowRoot
    • global
    • skip
  • Provides "Widgets" module system which supports loading bundles of next formats: iife, esm
  • Processed with Babel to guarantee compatibility with browsers
  • Error handling
  • Corner cases handling
  • Encapsulates logic of parsing different types of assets manifest files
  • It's hosted only on remote side, because it contains logic specific to the remote app.