|
| 1 | += Framework-agnostic Micro-frontends with Angular, React, and Vue |
| 2 | + |
| 3 | +This article explores the technical implementation of injecting React and Vue remote modules into an Angular shell app using Webpack 5's ModuleFederationPlugin. |
| 4 | + |
| 5 | +== Case for framework-agnostic Micro-frontends |
| 6 | + |
| 7 | +Consider a scenario where multiple applications share common components, such as a PROFILE page and a SETTINGS page. Micro-frontends offer compelling advantages in such cases: |
| 8 | + |
| 9 | +- Decentralized Development: By delegating the development of PROFILE and SETTINGS modules to standalone teams, they can work independently and potentially at a faster pace. |
| 10 | +- Framework Agnosticism: If the main app uses a different framework or version, Module Federation enables the PROFILE and SETTINGS modules to be developed and maintained independently, even if they use a different framework. |
| 11 | +- Modularity: In the case of a large application, Module Federation helps break it down into smaller, more manageable pieces. This makes it easier to upgrade one module at a time without affecting the rest of the app. |
| 12 | +- Faster Deployment: Deploying the PROFILE and SETTINGS modules separately from the main app allows for more flexibility and faster deployment times. The main app doesn't need to be rebuilt and redeployed every time a change is made to one of these modules. |
| 13 | + |
| 14 | +Now, let's dive into the technical implementation of using Module Federation to achieve these benefits. |
| 15 | + |
| 16 | +=== Developing the React "Profile" Remote Module |
| 17 | + |
| 18 | +The React "Profile" module contains a simple form where users can edit their name and email. The implementation is straightforward, but it's worth noting that the bootstrap.js file is mandatory due to a https://stackoverflow.com/questions/71228191/shared-module-is-not-available-for-eager-consumption-angular-13[bug]. |
| 19 | + |
| 20 | +To run the Profile module, navigate to the app folder and execute the following commands: |
| 21 | + |
| 22 | +[source, bash] |
| 23 | +---- |
| 24 | +yarn |
| 25 | +yarn start |
| 26 | +---- |
| 27 | + |
| 28 | +You can access the form at http://localhost:3001. |
| 29 | + |
| 30 | +==== ModuleFederationPlugin Settings (React) |
| 31 | + |
| 32 | +In the webpack.config.js file, the ModuleFederationPlugin settings for the React "Profile" module are as follows: |
| 33 | + |
| 34 | +[source, javascript] |
| 35 | +---- |
| 36 | +new ModuleFederationPlugin({ |
| 37 | + name: 'profile_user', |
| 38 | + filename: 'remoteEntry.js', |
| 39 | + exposes: { |
| 40 | + './ProfileReactComponent': './src/ProfileReactComponent', |
| 41 | + }, |
| 42 | + shared: { |
| 43 | + react: { |
| 44 | + singleton: true, |
| 45 | + requiredVersion: deps.react, |
| 46 | + eager: true, |
| 47 | + }, |
| 48 | + 'react-dom': { |
| 49 | + singleton: true, |
| 50 | + requiredVersion: deps['react-dom'], |
| 51 | + eager: true, |
| 52 | + }, |
| 53 | + }, |
| 54 | +}), |
| 55 | +---- |
| 56 | + |
| 57 | +In this context, the `ReactProfileComponent` was made available for external use, and the React libraries were shared. It's important to highlight that any imported code, including elements like CSS styles (`profile-style.css` in our example), will be packaged within the exposes module alongside the `ReactProfileComponent.tsx`. |
| 58 | + |
| 59 | +=== Developing the Vue Settings Remote Module |
| 60 | + |
| 61 | +The Vue "Settings" module is created to run as an isolated element within the shadow DOM to avoid conflicts between React and Vue in the Angular shell app. |
| 62 | + |
| 63 | +To run the Vue Settings module, navigate to the app folder and execute the following commands: |
| 64 | + |
| 65 | +[source, bash] |
| 66 | +---- |
| 67 | +yarn |
| 68 | +yarn start |
| 69 | +---- |
| 70 | + |
| 71 | +You can access the module at `http://localhost:3002.` |
| 72 | + |
| 73 | +==== ModuleFederationPlugin Settings (Vue) |
| 74 | + |
| 75 | +In the `webpack.config.js` file for the Vue Settings component, the ModuleFederationPlugin settings are as follows: |
| 76 | + |
| 77 | +[source, javascript] |
| 78 | +---- |
| 79 | +new ModuleFederationPlugin({ |
| 80 | + name: 'settings_user', |
| 81 | + filename: 'remoteEntry.js', |
| 82 | + exposes: { |
| 83 | + './Settings': './src/components/Settings', |
| 84 | + }, |
| 85 | + shared: { |
| 86 | + vue: { |
| 87 | + eager: true, |
| 88 | + requiredVersion: deps.vue, |
| 89 | + }, |
| 90 | + }, |
| 91 | +}), |
| 92 | +---- |
| 93 | + |
| 94 | +The configuration ensures that the Vue module can be used within the Angular shell app. |
| 95 | + |
| 96 | +NOTE: To mitigate potential conflicts between React and Vue in the Angular shell app, Vue 3 introduces the use of `defineCustomElement`. This feature enables the creation of Vue components as self-contained elements within the shadow DOM. |
| 97 | + |
| 98 | +=== Integrating React and Vue into the Angular Shell |
| 99 | + |
| 100 | +In the Angular shell application, we'll integrate the React "Profile" module and Vue "Settings" module as remote components. |
| 101 | + |
| 102 | +The timing of when these remote components should be loaded into the Angular shell app varies based on the specific app's needs. In our scenario, we want the React and Vue components to be visible on the homepage of the Angular shell app, so we run the `LoadRemoteModule` methods as part of the app's initialization process. |
| 103 | + |
| 104 | +In the app.module.ts file, the initialization process is defined: |
| 105 | + |
| 106 | +[source, typescript] |
| 107 | +---- |
| 108 | +export function initializeApp(): () => void { |
| 109 | +return () => { |
| 110 | + loadRemoteModule({ |
| 111 | + remoteEntry: "http://localhost:3001/remoteEntry.js", |
| 112 | + remoteName: "profile_user", |
| 113 | + exposedModule: "./ProfileReactComponent", |
| 114 | + }); |
| 115 | + loadRemoteModule({ |
| 116 | + remoteEntry: "http://localhost:3002/remoteEntry.js", |
| 117 | + remoteName: "settings_user", |
| 118 | + exposedModule: "./Settings", |
| 119 | + }); |
| 120 | +}; |
| 121 | +} |
| 122 | +
|
| 123 | +@NgModule({ |
| 124 | +declarations: [AppComponent], |
| 125 | +imports: [BrowserModule, RouterModule.forRoot(routes)], |
| 126 | +providers: [ |
| 127 | + { |
| 128 | + provide: APP_INITIALIZER, |
| 129 | + useFactory: initializeApp, |
| 130 | + multi: true, |
| 131 | + }, |
| 132 | +], |
| 133 | +---- |
| 134 | + |
| 135 | +Webpack config settings in `webpack.config.ts` for the Angular shell define remotes and shared React libraries, allowing the shell to be a federated module |
| 136 | + |
| 137 | +[source, typescript] |
| 138 | +---- |
| 139 | +new container.ModuleFederationPlugin({ |
| 140 | + name: "angular-shell", |
| 141 | + filename: "remoteEntry.js", |
| 142 | + remotes: { |
| 143 | + profile_user: `profile_user@http://localhost:3001/remoteEntry.js`, |
| 144 | + settings_user: `settings_user@http://localhost:3002/remoteEntry.js`, |
| 145 | + }, |
| 146 | + shared: { |
| 147 | + react: { |
| 148 | + singleton: true, |
| 149 | + requiredVersion: deps.react, |
| 150 | + }, |
| 151 | + "react-dom": { |
| 152 | + singleton: true, |
| 153 | + requiredVersion: deps["react-dom"], |
| 154 | + }, |
| 155 | + }, |
| 156 | +}), |
| 157 | +---- |
| 158 | + |
| 159 | +Additionally, Webpack devServer headers with `"Access-Control-Allow-Origin": "*"` are set to avoid CORS errors. |
| 160 | + |
| 161 | +To adapt the Angular application, you'll need to modify the `angular.json` file, replacing the default Webpack configuration with a custom one. |
| 162 | + |
| 163 | +The changes are the following: |
| 164 | + |
| 165 | +[source, json] |
| 166 | +---- |
| 167 | +... |
| 168 | + "builder": "@angular-builders/custom-webpack:browser", |
| 169 | +... |
| 170 | + "scripts": [], |
| 171 | + "customWebpackConfig": { |
| 172 | + "path": "webpack.config.ts", |
| 173 | + "replaceDuplicatePlugins": true |
| 174 | + } |
| 175 | +... |
| 176 | + "cli": { |
| 177 | + "packageManager": "yarn" |
| 178 | + } |
| 179 | +---- |
| 180 | + |
| 181 | +Next, we'll focus on injecting the React component into the Angular shell. To achieve this, a `profile-user.component.ts` container has been created to integrate the React component. The ProfileReactComponent is loaded asynchronously within the `ngAfterViewInit` lifecycle hook. Additionally, a `decl.d.ts` file has been introduced to inform Angular that `"profile_user"` is a valid import directory. |
| 182 | + |
| 183 | +Here's the code from `angular-shell/src/app/profile-user/profile-user.component.ts``: |
| 184 | + |
| 185 | +[source, typescript] |
| 186 | +---- |
| 187 | +ngAfterViewInit() { |
| 188 | + this.root = createRoot(this.containerRef.nativeElement); |
| 189 | + this.root.render("Loading script..."); |
| 190 | + try { |
| 191 | + import("profile_user/ProfileReactComponent").then((val) => { |
| 192 | + this.root.render( |
| 193 | + React.createElement(val.ProfileReactComponent, { |
| 194 | + ...this.user, |
| 195 | + onClick: this.updateCurrentUser, |
| 196 | + }) |
| 197 | + ); |
| 198 | + }); |
| 199 | + } catch (error) { |
| 200 | + console.log("Error", error); |
| 201 | + } |
| 202 | +} |
| 203 | +---- |
| 204 | + |
| 205 | +The React Profile component interacts with the Angular shell through the `onClick` function. This allows the Angular shell app to update user data when changes are made to the name and email in the React component. |
| 206 | + |
| 207 | +Similarly, the Vue Settings component is injected into the `settings.component.ts` wrapper. The approach closely resembles that of the React Profile component. Here's the code from angular-shell/src/app/settings/settings.component.ts: |
| 208 | + |
| 209 | +[source, typescript] |
| 210 | +---- |
| 211 | +import("settings_user/Settings").then((val) => { |
| 212 | + this.renderer.appendChild( |
| 213 | + this.containerVueRef.nativeElement, |
| 214 | + new val.default() |
| 215 | + ); |
| 216 | +}); |
| 217 | +---- |
| 218 | + |
| 219 | +To run the Angular shell app, please take note of using `yarn`. This is necessary to override the webpack version for the Angular CLI. In the `angular-shell` folder, execute the following commands: |
| 220 | + |
| 221 | +1. Create a new Angular app (skip the installation step): |
| 222 | ++ |
| 223 | +[source, bash] |
| 224 | +---- |
| 225 | +ng new --skip-install |
| 226 | +---- |
| 227 | ++ |
| 228 | +2. Configure the CLI to use `yarn` as the package manager: |
| 229 | ++ |
| 230 | +[source, bash] |
| 231 | +---- |
| 232 | +ng config cli.packageManager yarn |
| 233 | +---- |
| 234 | ++ |
| 235 | +3. Install the dependencies: |
| 236 | ++ |
| 237 | +[source, bash] |
| 238 | +---- |
| 239 | +yarn install |
| 240 | +---- |
| 241 | + |
| 242 | +The app can be accessed via http://localhost:4201. |
0 commit comments