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

jigsaw-mf

v1.0.0

Published

a micro-frontend solution

Downloads

4

Readme

A micro-frontend solution.

npm versionnpm downloads

Introduction

jigsaw is a micro-frontend solution for assembling mutiple micro applications into the master application to make the site perform like a Single-Page application! Or by leveraging jigsaw, you can split your huge application into small parts to improve maintablity!

  • support any JavaScript user interface librarys. such as React, vue etc... as long as you can control when to mount/unmout your application!
  • support comunications between micro-applications.

Terminology

micro-application a small application that can be assembled into a large application.

master-application the main application that host one or many micro-application

Installations

npm install jigsaw --save

How to use

Adapt existing application to a micro-application

  1. add a config or update your application config file.
// src/config/application.json
{
    // applicaion must be built as a library, and this is the library name. [used by webpack]
    "library": "reactfather",
    // assets must be linked by absolute path. [used by webpack]
    "publicPath": "http://localhost:8082"
}
  1. add a new entry file
// src/index-app.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
// adding line 1,2,3 if your application serve both as master-application and micro-application!
// otherwise omit them.
import { appPool } from "./global"; // line 1

export default {
  // triggered when your code is executed but before mount
  bootstrap() {
    console.log("react app bootstraped");
  },
  mount(contain, baseUrl) {
    appPool.baseUrl = baseUrl; // line 2. current application MUST inherit its parent's baseUrl
    ReactDOM.render(<App baseUrl={baseUrl} />, contain);
  },
  unmount(contain) {
    appPool.unregisterAllApps(); // line 3
    ReactDOM.unmountComponentAtNode(contain);
  }
};
  1. update webapck config
{
    /**  omit the other config  **/
    entry: {
      /** your other entry **/
      app: './src/index-app.js'
    },
    output: {
      /** your other config **/
      publicPath: config.publicPath,
      libraryTarget: 'umd',
      library: config.library
    }
    /**  omited  **/
    new HtmlWebpackPlugin({
        inject: true,
        chunks: ["app"],
        filename: 'app.html'
    })

    // postcss-selector-namespace
}

NOTE: The css and js assets will be accessed by master-application via ajax, so those assets need to support CORS request

Adapt existing application to a master-application

  1. add a config or update your application config file
// src/config/application.json
{
    // the url base path your site serves  e.g. /your/path.
    "baseUrl": "/"
}
  1. create jigsaw instance. It's a good convention to put your global variables into one single module instead of assigning it to window
// src/global.js
import Jigsaw from "jigsaw";
import appConfig from "../config/application.json"; // created by step 1

export const appPool = new Jigsaw(appConfig);
export const other_global_var = "your data";
  1. resgister micro-application
// add this code to any position as long as `container1` exists. usually after `componentDidMount` if your are using react.

import { appPool } from "./global";

const appinfo = [
  {
    // the unique name amount the micro-applications. [required]
    name: "a49",
    // the library name of the micro-application. eg. config.library. [required]
    applicationName: "reactfather",
    routerMode: 'history', // hash | history. default: 'history'. [optional]
    // webpack.entry.app. [required]
    entry: "http://localhost:8082/app.html",
    contain: document.getElementById("container1"), // or use refs to gain dom reference. [required]
    // the base path allocated to this micro-application, relative to `appPool.baseUrl`. [required]
    baseUrl: "/reactchild",
    // to determine if to mount this micro-application. [optional]
    canActive = (baseUrl, basePath) => {
      return window.location.pathname.startsWith(basePath) && window.location.hash.startsWith("#" + baseUrl)
    }; // default for `hash` mode
    canActive = (baseUrl, basePath) => window.location.pathname.startsWith(baseUrl); // default for `history` mode
  }
];

appPool.registerApps(appinfo);

Now, run both your master-application and micro-application, and you will see it works.

Comunication

event based comunication via eventemitter2

// application internal comunication
import { appPool } from "./global";
appPool.on("event", function(data) {
  console.log(data); // output: internal message
});
appPool.emit("event", "internal message");

// cross micro-application comunication
// application 1
import { globalEvent } from "east-mfs";
globalEvent.on("event", function(data) {
  console.log(data); // output: global message
});

// application 2
import { globalEvent } from "east-mfs";
globalEvent.emit("event", "global message");

About css isolation

try postcss-selector-namespace

Run Example

git clone this repertory
cd jigsaw
npm install
npm run init
npm run run:fragment
// open http://localhost:9100

Html Entry

By default, jigsaw will use the last js file as the execution entry. but you can change this behavior by adding attribute entry.

<script src='http://localhost:3000/a.js' entry>

And by adding attribute ignore, you can tell jigsaw to ignore this file.

<script src='http://localhost:3000/a.js' ignore>

Tips

An application can be adapted to serve both as master-application and micro-application!

  1. DO NOT adding exact prop to route when the corresponding component will register some micro-applications, it might prevent your micro-application from showing!
  2. All the JS and CSS resources linked by your micro-application must use absolute path. e.g. http://localhost:9001/your/resource/path
  3. Micro-application MUST be packed through umd mode with unique library name.
  4. Micro-application must support CORS request for the JS/CSS files.

Known Issues

  1. Registering micro-application that under development mode with hot reload enabled will cause white screen.
  2. When the master application is using hash router mode, the micro-application beneath can NOT be history mode.

License

MIT