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

@mondora/microfrontends

v1.1.1

Published

Library for embedding frontends into one another

Downloads

521

Readme

@mondora/microfrontends

Library for embedding frontends into one another.

Install

yarn add @mondora/microfrontends

Use

In parent apps

import { ChildApp } from "@mondora/microfrontends";

// Create the iframe for the child app and attach it to the dom
const iframe = document.createElement("iframe");
iframe.src = "https://child.app";
document.body.appendChild(iframe);

// Create the child app
const childApp = new ChildApp({
  iframe: iframe,
  // Methods that the child app will be able to call
  suppliedMethods: {
    echo: input => input
  },
  // Data to pass to the child app at launch
  launchData: {
    authToken: "authToken"
  },
  // Function called when the child app is launched
  onLaunched() {
    console.log("child app launched");
  }
});

// Launch the app
childApp.launch();

Or, if you're using React:

import { ChildAppReactComponent } from "@mondora/microfrontends";

<ChildAppReactComponent
  url="https://child.app"
  suppliedMethods={{
    echo: input => input
  }}
  launchData={{
    authToken: "authToken"
  }}
  launchingPlaceholder={<span>{"Launching child app..."}</span>}
  onLaunched={() => {
    console.log("child app launched");
  }}
/>;

For child apps

import { ParentApp } from "@mondora/microfrontends";

// Get a reference to the parent app
const parentApp = new ParentApp({
  onLaunch(launchData) {
    // Start your child app with the launchData supplied by the parent. Example
    // for a React app:
    ReactDOM.render(<App data={launchData} />);
    // When the function returns, the parent app is notified that the child app has launched
  }
});

// Somewhere else in the app, after launch...

// Call the methods supplied by the parent
parentApp.call("echo", "hello").then(result => {
  console.log(result);
});

API

ChildApp

Used from the parent app, creates and launches a child app into an iframe.

Constructor options:

  • iframe (required): the iframe pointing to the child app
  • onLaunched (required): function called when the child app is launched
  • childOrigin: the origin of the child app. In most cases it's detected automatically
  • suppliedMethods: methods that the child app will be able to call
  • launchData: data to pass to the child app on launch

launch()

Launch the child app.

Returns

Nothing.

<ChildAppReactComponent />

Used from the parent app, React component that creates, launches, and renders a child app.

Accepted props:

  • url: see ChildApp constructor options
  • onLaunched: see ChildApp constructor options
  • childOrigin: see ChildApp constructor options
  • suppliedMethods: see ChildApp constructor options
  • launchData: see ChildApp constructor options
  • launchingPlaceholder: placeholder to show while the child app is launching (e.g. a spinner)
  • className: class assigned to the child app iframe

ParentApp

Used from the child app. It attaches to the parent app, receives launch data and tells the parent when the child has finished launching. It can be used to call methods supplied by the parent.

Constructor options:

  • onLaunch (required): function called when the child app is launched
  • parentOrigin: the expected origin of the parent app. If the parent's origin differs from it, the child won't attach to it. When not supplied, the child attaches to the parent regardless of origin

call(name, ...params)

Calls a method supplied by the parent app. Throws an error if the child has not launched yet.

Params
  • name (required): the name of the method to call
  • ...params: the parameters to pass to the method. They must be JSON-serializable, as they are sent through a cross-window channel
Returns

A promise to the return value of the parent method.