Giter VIP home page Giter VIP logo

10-spa-composition's Introduction

Chapter 10

Prerequisites

The following software is required to run the sample:

  • Git
  • Node.js
  • NPM
  • Bash

Running

Clone the repository:

git clone https://github.com/ArtOfMicrofrontends/10-spa-composition.git

Go to the repository's directory and start the application, which also installs all required package dependencies:

./run.sh

Steps

Follow these steps to implement the same from scratch.

  1. Start with the app shell, initialize a new Node.js project and install the dependencies
npm init -y
npm install bootstrap bootstrap-icons regenerator-runtime --save
npm install parcel-bundler --save-dev
  1. Create an index.html file with the skeleton design of the application with a placeholder for the content (e.g., #app-content)

  2. Add the script (app.js) and stylesheet (style.css) reference

  3. In the script load all configured MF scripts coming from the scripts.json:

[
    "/mfes/balance/balance.js",
    "/mfes/tax/tax.js",
    "/mfes/settings/settings.js"
]

with the loading logic being as simple as

import("./scripts.json").then((scripts) =>
  scripts.forEach((url) => {
    const script = document.createElement("script");
    script.src = url;
    document.body.appendChild(script);
  })
);
  1. Hook on to the hashchange and popstate listeners to introduce routing

  2. Introduce lifecycle functions for bootrap, mount, and unmount of components - hooked up in the renderComponent function:

window.renderComponent = (name, componentName, target, props = {}) => {
  const id = makeId(name, componentName);
  const info = registry[id];

  if (info !== undefined) {
    const user = {
      target,
      props,
      data: undefined,
      state: "init",
    };
    info.used.push(user);
    info.wait = bootstrapComponent(info, user);
    info.wait = mountComponent(info, user);
  }
};
  1. Create the MFs, e.g., for the tax MF you'll need to initialize and set up the project
npm init -y
npm install file-loader svelte svelte-loader webpack webpack-cli --save-dev

where you'll need to add a proper Webpack configuration for the framework of your choice

  1. The index.js of each MF will have boilerplate code to register the lifecycle of the exposed components; for instance:
window.registerComponent("<namespace-name>", "<component-name>", {
  bootstrap: () => {
    // load component
  },
  mount: (target, props) => {
    // mount component
  },
  unmount: (target, info) => {
    // unmount component
  },
});

10-spa-composition's People

Contributors

florianrappl avatar

Watchers

James Cloos avatar  avatar

Forkers

aregee

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.