javascriptangulartypescriptwebpackangular-module-federation

Angular federated module can not be loaded when route would be activated


I am trying to achieve a very simple module federation with shell (host) and travel (remote). Whenever I try do dynamically load the AbcModule from travel I get the following error in the shell:

ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'init') TypeError: Cannot read properties of undefined (reading 'init') at angular-architects-module-federation-runtime.js:26:1 at Generator.next () at fulfilled (tslib.es6.js:73:43) at _ZoneDelegate.invoke (zone.js:372:1) at Object.onInvoke (core.mjs:25634:33) at _ZoneDelegate.invoke (zone.js:371:1) at Zone.run (zone.js:134:1) at zone.js:1275:1 at _ZoneDelegate.invokeTask (zone.js:406:1) at Object.onInvokeTask (core.mjs:25621:33) at resolvePromise (zone.js:1211:1) at zone.js:1118:1 at fulfilled (tslib.es6.js:73:86) at _ZoneDelegate.invoke (zone.js:372:1) at Object.onInvoke (core.mjs:25634:33) at _ZoneDelegate.invoke (zone.js:371:1) at Zone.run (zone.js:134:1) at zone.js:1275:1 at _ZoneDelegate.invokeTask (zone.js:406:1) at Object.onInvokeTask (core.mjs:25621:33)

ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'get') TypeError: Cannot read properties of undefined (reading 'get') at angular-architects-module-federation-runtime.js:9:1 at Generator.next () at tslib.es6.js:76:1 at new ZoneAwarePromise (zone.js:1427:1) at __awaiter (tslib.es6.js:72:1) at lookupExposedModule (angular-architects-module-federation-runtime.js:7:21) at angular-architects-module-federation-runtime.js:106:1 at Generator.next () at fulfilled (tslib.es6.js:73:43) at _ZoneDelegate.invoke (zone.js:372:1) at resolvePromise (zone.js:1211:1) at resolvePromise (zone.js:1165:1) at zone.js:1278:1 at _ZoneDelegate.invokeTask (zone.js:406:1) at Object.onInvokeTask (core.mjs:25621:33) at _ZoneDelegate.invokeTask (zone.js:405:1) at Zone.runTask (zone.js:178:1) at drainMicroTaskQueue (zone.js:585:1) at ZoneTask.invokeTask [as invoke] (zone.js:49

app routes from shell:

import { loadRemoteModule } from "@angular-architects/module-federation";
import { Routes } from "@angular/router";
import { TestComponent } from "./test/test.component";

export const APP_ROUTES: Routes = [
  {
    path: '',
    component: TestComponent,
    pathMatch: 'full'
  },
  {
    path: 'abc',
    loadChildren: () => loadRemoteModule({
      remoteEntry: 'http://localhost:4201/remoteEntry.js',
      remoteName: 'travel',
      exposedModule: './Module'
    })
    .then(m => m.AbcModule)
  }
];

webpack.config.js from shell:

const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const mf = require("@angular-architects/module-federation/webpack");
const path = require("path");
const share = mf.share;

const sharedMappings = new mf.SharedMappings();
sharedMappings.register(
  path.join(__dirname, '../../tsconfig.base.json'),
  [/* mapped paths to share */]);

module.exports = {
  output: {
    uniqueName: "shell",
    publicPath: "auto"
  },
  optimization: {
    runtimeChunk: false
  },
  resolve: {
    alias: {
      ...sharedMappings.getAliases(),
    }
  },
  experiments: {
    outputModule: true
  },
  plugins: [
    new ModuleFederationPlugin({
        library: { type: "module" },


        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()
        })

    }),
    sharedMappings.getPlugin()
  ],
};

webpack.config.js from travel:

const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const mf = require("@angular-architects/module-federation/webpack");
const path = require("path");
const share = mf.share;

const sharedMappings = new mf.SharedMappings();
sharedMappings.register(
  path.join(__dirname, '../../tsconfig.base.json'),
  [/* mapped paths to share */]);

module.exports = {
  output: {
    uniqueName: "travel",
    publicPath: "auto"
  },
  optimization: {
    runtimeChunk: false
  },
  resolve: {
    alias: {
      ...sharedMappings.getAliases(),
    }
  },
  experiments: {
    outputModule: true
  },
  plugins: [
    new ModuleFederationPlugin({
        library: { type: "module" },

       name: "travel",
       filename: "remoteEntry.js",
       exposes: {
           './Module': './apps/travel/src/app/abc/abc.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()
        })

    }),
    sharedMappings.getPlugin()
  ],
};

AbcModule from travel:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AbcComponent } from './abc/abc.component';
import { RouterModule } from '@angular/router';

@NgModule({
  declarations: [AbcComponent],
  imports: [CommonModule, RouterModule.forChild([
    {
      path: '',
      component: AbcComponent
    }
  ])],
  exports: [RouterModule]
})
export class AbcModule {}

Solution

  • I got it working by changing the loadRemoteModule parameter to this:

    import { loadRemoteModule } from "@angular-architects/module-federation";
    import { Routes } from "@angular/router";
    import { TestComponent } from "./test/test.component";
    
    export const APP_ROUTES: Routes = [
      {
        path: '',
        component: TestComponent,
        pathMatch: 'full'
      },
      {
        path: 'abc',
        loadChildren: () => loadRemoteModule({
          remoteEntry: 'http://localhost:4201/remoteEntry.js',
          type: 'module',
          exposedModule: './Module'
        })
        .then(m => m.AbcModule)
      }
    ];