Giter VIP home page Giter VIP logo

micro-frontend-multi-framework's Introduction

Micro frontend with Multi framework

Important: This code is written in Angular CLI 14** and higher.I have created a simple applictaion with basic UI. In this appilication I have created a container, MF1 -> Micro frontend one (access app module) and MFE2 -> Second Micro frontend created in react with one app component and access that in container module.

##Part 1: Steps to run

  1. Clone the starterkit for this tutorial: https://github.com/harshal77/micro-frontend-multi-framework --branch development

  2. Move into the project directory and install the dependencies of each micro fronend

    cd angular-container
    npm i
    cd angular-mfe
    npm i
    cd react-mfe
    npm i
    
  3. Start the container (Port 4200) app first then other micro fronends like angular-mfe (Port 4201), react-mfe (Port 4204).

Part 2: How to add module federation

  1. Install @angular-architects/module-federation into the conatiner and into the micro frontend:

        npm i @angular-architects/module-federation
    
    

    or you can simply add module federation in each app independently using following command this will ask port number and project name.

    npm i @angular-architects/module-federation-tools  
    

    this is also require when we you use react

  2. Switch into the project angular-mfe and open the generated configuration file webpack.config.js. It contains the module federation configuration. In the exposes object you have to add whichever module you have to expose or access other MF's ex):

    const { shareAll, withModuleFederationPlugin } = require('@angular-architects/module-federation/webpack');
    
    module.exports = withModuleFederationPlugin({
    
        name: 'angular-mfe',
        filename: "remoteEntry.js",
        exposes: {
            './Module': './mfe1/src/app/app.module.ts'
        },
        shared: {
            ...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
        },
    
    });

    Here I exposes the app module under the Name ./Module. Hence, the container can use this path to load it. Note. If you want to expose app module make sure in routing file you have to use RouterModule.forChild(routes) istend of RouterModule.forRoot(routes).

  3. Switch into the project react-mfe and open the generated configuration file webpack.config.js. It contains the module federation configuration. In the exposes object you have to add whichever module you have to expose or access other MF's ex):

    const { shareAll, withModuleFederationPlugin } = require('@angular-architects/module-federation/webpack');
    const CopyWebpackPlugin = require("copy-webpack-plugin");
    
    module.exports = withModuleFederationPlugin({
    
        name: "react",
        library: {
          type: "var",
          name: "react"
        },
        filename: "remoteEntry.js", // <-- Meta Data
        exposes: {
          './web-components': './app.js',
          './react-component-1': './reactComponentOne.js',
        },
        shared: ["react", "react-dom"]
      }),
      new CopyWebpackPlugin({
        patterns: [{
          from: './*.html'
        }]
      })
    ],
    devServer: {
      port: 4204
    }
    
    });
    

4.1) Need to add some extra webpack packages for react.

 ```
 npm install --save-dev webpack webpack-cli html-webpack-plugin webpack-dev-server babel-loader
```
  1. We can access angular-mfe module two ways in container applictaion.

5.1) Switch into the angular-mfe app and open the file webpack.config.js ``` new ModuleFederationPlugin({ library: { type: "module" },

    name: "mfe1",
    filename: "remoteEntry.js",
    exposes: {
        './angularMFE': './src/app/app.module.ts',
    },          

    shared: share({
      "@angular/core": { singleton: true, strictVersion: true, requiredVersion: 'auto' }, 
      "@angular/common": { singleton: true, strictVersion: true, requiredVersion: 'auto' }, 
      "@angular/common/http": { singleton: true, strictVersion: true, requiredVersion: 'auto' }, 
      "@angular/router": { singleton: true, strictVersion: true, requiredVersion: 'auto' },

      ...sharedMappings.getDescriptors()
    })
    
}),
```

5.2) Switch into the Container app and open the file routing file

```
{
path: 'mfe-1',
loadChildren: () =>
  loadRemoteModule({
    type: 'module',
    remoteEntry: 'http://localhost:4201/remoteEntry.js',
    exposedModule: './MF1'
  })
    .then((m: any) => m.AppModule)

}, { path: 'react', component: WebComponentWrapper, data: { type: 'script', remoteEntry: 'http://localhost:4204/remoteEntry.js', remoteName: 'react', exposedModule: './web-components', elementName: 'react-element', } as WebComponentWrapperOptions, }, ```

Please note that the imported URL consists of the names defined in the configuration files above.

Part 3: Try it out

Now, let's try it out!

  1. Start the angular-container, angular-mfe and react-mfe side by side:

    ng serve angular-container -o
    ng serve angular-mfe -o
    npm start (for react app)
    
  2. The container should still be able to load the micro frontend. https://webpack.js.org/concepts/module-federation/

micro-frontend-multi-framework's People

Contributors

harshal77 avatar

Stargazers

Aditya Prakash avatar Stephen M avatar Dzmitry-Astraukh_epam avatar Deepali Lodhi avatar Cibele Martins avatar TSU avatar Bendy Zhang avatar

Watchers

James Cloos avatar  avatar

micro-frontend-multi-framework's Issues

Sharing common assets between repositories

Hi Harshal,

How can we share common assets like some CSS files and images between projects?
Is it possible to have a common project that contains the common CSS files and images that can be shared between all the applications or do we have to have the files separately inside each application?

Issue with internal routing in remote app

Hello Harshal,

Firstly the framework you developed it is really good. I really appreciate it.

I have one doubt, if I wanted to add routing in angular-mfe (remote app), how to achieve it? Can you please provide your inputs? I tried adding more routes in app.routing module of angular-mfe but it is not working.

// app-routing.module.ts
const routes: Routes = [
{ path: '', component: HomeComponent, pathMatch: 'fall },
{ path: 'profile', component: ProfileComponent }
];

// home.component.html
<a routerLink="/profile">Profile</a>

Thanks
Abhijeet

Issues In Rendering Remote App

Sir . i am getting issues in rendering remote app in host . it appends the content instead of replacing please assist

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.