├── .babelrc ├── .gitignore ├── LICENSE ├── README.md ├── example.png ├── index.html ├── package-lock.json ├── package.json ├── src ├── angular │ ├── index.component.ts │ ├── index.js │ └── main-module.ts ├── event-bus │ └── index.js ├── react │ ├── index.js │ └── root.component.js └── root-application │ └── root-application.js ├── tsconfig.json └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["env", { 4 | "targets": { 5 | "browsers": ["last 2 versions"] 6 | } 7 | }], 8 | ["react"] 9 | ], 10 | "plugins": [ 11 | "syntax-dynamic-import" 12 | ] 13 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ivan Jovanovic 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React and Angular single-spa example 2 | This is the code for tutorial posted on [https://ivanjov.com/micro-frontends-how-i-built-a-spa-with-angular-and-react/](https://ivanjov.com/micro-frontends-how-i-built-a-spa-with-angular-and-react/) 3 | 4 | ## Running the app 5 | #### Clone the code 6 | ```bash 7 | git clone https://github.com/IvanJov/react-angular-single-spa.git && cd react-angular-single-spa 8 | ``` 9 | 10 | #### Install dependencies 11 | ```bash 12 | npm install 13 | ``` 14 | 15 | #### Run app 16 | ```bash 17 | npm start 18 | ``` 19 | 20 | #### Open `http://localhost:9090/#/` in browser 21 | 22 | You should see something like this: 23 | 24 | ![example](https://raw.githubusercontent.com/IvanJov/react-angular-single-spa/master/example.png) -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IvanJov/react-angular-single-spa/adad311de9637a9836a96baee3e29cae3b8677fe/example.png -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | React/Angular Single SPA 7 | 8 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "single-spa-example", 3 | "scripts": { 4 | "start": "webpack-dev-server --port 9090", 5 | "build": "webpack --config webpack.config.js -p" 6 | }, 7 | "devDependencies": { 8 | "babel-core": "^6.25.0", 9 | "babel-loader": "^7.1.1", 10 | "babel-plugin-syntax-dynamic-import": "^6.18.0", 11 | "babel-preset-env": "^1.6.0", 12 | "babel-preset-react": "^6.24.1", 13 | "clean-webpack-plugin": "^0.1.16", 14 | "http-server": "^0.10.0", 15 | "ts-loader": "^2.3.2", 16 | "typescript": "^2.4.2", 17 | "webpack": "^3.4.1", 18 | "webpack-dev-server": "^2.8.2" 19 | }, 20 | "dependencies": { 21 | "@angular/common": "^4.3.3", 22 | "@angular/compiler": "^4.3.3", 23 | "@angular/core": "^4.3.3", 24 | "@angular/platform-browser": "^4.3.3", 25 | "@angular/platform-browser-dynamic": "^4.3.3", 26 | "@angular/router": "^4.3.6", 27 | "core-js": "^2.4.1", 28 | "eev": "^0.1.0", 29 | "es6-promise": "^4.1.1", 30 | "react": "^15.6.1", 31 | "react-dom": "^15.6.1", 32 | "reflect-metadata": "^0.1.10", 33 | "rxjs": "^5.4.2", 34 | "single-spa": "^3.5.0", 35 | "single-spa-angular2": "^1.1.0", 36 | "single-spa-react": "^2.0.4", 37 | "zone.js": "^0.8.16" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/angular/index.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, ChangeDetectorRef, Inject } from '@angular/core' 2 | import e from '../event-bus' 3 | 4 | @Component({ 5 | selector: 'AngularApp', 6 | template: ` 7 |
8 |

This was written in Angular

9 |

{{message}}

10 |
11 | `, 12 | }) 13 | export default class AngularApp { 14 | message: string = "Message from React should appear here 😱" 15 | 16 | constructor(@Inject(ChangeDetectorRef) private changeDetector: ChangeDetectorRef) {} 17 | 18 | ngAfterContentInit() { 19 | e.on('message', message => { 20 | this.message = message.text 21 | this.changeDetector.detectChanges() 22 | this.returnMessageToReactWhenReceived() 23 | }) 24 | } 25 | 26 | returnMessageToReactWhenReceived() { 27 | e.emit('received', { text: 'Woohoo! Hello from Angular! 🎉' }) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/angular/index.js: -------------------------------------------------------------------------------- 1 | import 'zone.js' 2 | import 'reflect-metadata' 3 | import singleSpaAngular from 'single-spa-angular2' 4 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic' 5 | import mainModule from './main-module.ts' 6 | import { Router } from '@angular/router' 7 | 8 | const domElementGetter = () => { 9 | let el = document.getElementById('angular') 10 | if (!el) { 11 | el = document.createElement('div') 12 | el.id = 'angular' 13 | document.body.appendChild(el) 14 | } 15 | 16 | return el 17 | } 18 | 19 | const ngLifecycles = singleSpaAngular({ 20 | domElementGetter, 21 | mainModule, 22 | angularPlatform: platformBrowserDynamic(), 23 | template: ``, 24 | Router, 25 | }) 26 | 27 | export const bootstrap = props => ngLifecycles.bootstrap(props) 28 | 29 | export const mount = props => ngLifecycles.mount(props) 30 | 31 | export const unmount = props => ngLifecycles.unmount(props) 32 | -------------------------------------------------------------------------------- /src/angular/main-module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core' 2 | import { BrowserModule } from '@angular/platform-browser' 3 | import AngularApp from './index.component.ts' 4 | import { enableProdMode } from '@angular/core' 5 | import { APP_BASE_HREF } from "@angular/common" 6 | 7 | enableProdMode() 8 | 9 | @NgModule({ 10 | imports: [ 11 | BrowserModule, 12 | ], 13 | providers: [{ provide: APP_BASE_HREF, useValue: '/angular/' }], 14 | declarations: [ 15 | AngularApp 16 | ], 17 | bootstrap: [AngularApp] 18 | }) 19 | export default class MainModule { 20 | } 21 | -------------------------------------------------------------------------------- /src/event-bus/index.js: -------------------------------------------------------------------------------- 1 | import Eev from 'eev' 2 | 3 | export const e = new Eev() 4 | 5 | export default e 6 | -------------------------------------------------------------------------------- /src/react/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import ReactDOM from 'react-dom' 3 | import singleSpaReact from 'single-spa-react' 4 | import Root from './root.component.js' 5 | 6 | const domElementGetter = () => { 7 | let el = document.getElementById('react') 8 | if (!el) { 9 | el = document.createElement('div') 10 | el.id = 'react' 11 | document.body.appendChild(el) 12 | } 13 | 14 | return el 15 | } 16 | 17 | const reactLifecycles = singleSpaReact({ 18 | React, 19 | ReactDOM, 20 | rootComponent: Root, 21 | domElementGetter, 22 | }) 23 | 24 | export const bootstrap = props => reactLifecycles.bootstrap(props) 25 | 26 | export const mount = props => reactLifecycles.mount(props) 27 | 28 | export const unmount = props => reactLifecycles.unmount(props) 29 | -------------------------------------------------------------------------------- /src/react/root.component.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import e from '../event-bus' 3 | 4 | export default class Root extends React.Component { 5 | constructor(props) { 6 | super(props) 7 | 8 | this.state = { 9 | message: 'When Angular receives message, we should see a confirmation here 😎' 10 | } 11 | 12 | this.messageHandler = this.messageHandler.bind(this) 13 | } 14 | 15 | componentDidMount() { 16 | e.on('received', this.messageHandler) 17 | } 18 | 19 | componentDidUnmount() { 20 | e.off('received', this.messageHandler) 21 | } 22 | 23 | messageHandler(message) { 24 | this.setState({ 25 | message: message.text 26 | }) 27 | } 28 | 29 | sendMessage() { 30 | e.emit('message', { text: 'Hello from React 👋' }) 31 | } 32 | 33 | render() { 34 | return ( 35 |
36 |

This was written in React

37 | 38 |

39 | 42 |

43 | 44 |

45 | {this.state.message} 46 |

47 |
48 | ) 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/root-application/root-application.js: -------------------------------------------------------------------------------- 1 | import { start, registerApplication } from 'single-spa' 2 | 3 | const hashPrefix = prefix => location => location.hash.startsWith(`#${prefix}`) 4 | 5 | registerApplication('react', () => import('../react/index.js'), hashPrefix('/')) 6 | registerApplication('angular', () => import('../angular/index.js'), hashPrefix('/')) 7 | 8 | start() 9 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": true, 4 | "sourceMap": true, 5 | "target": "es5", 6 | "module": "commonjs", 7 | "moduleResolution": "node", 8 | "lib": ["es5", "es6", "dom"], 9 | "experimentalDecorators": true 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const webpack = require('webpack'); 3 | const CleanWebpackPlugin = require('clean-webpack-plugin') 4 | const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin'); 5 | 6 | module.exports = { 7 | entry: { 8 | 'root-application': 'src/root-application/root-application.js', 9 | 'common-dependencies': [ 10 | 'core-js/client/shim.min.js', 11 | '@angular/common', 12 | '@angular/compiler', 13 | '@angular/core', 14 | '@angular/platform-browser-dynamic', 15 | '@angular/router', 16 | 'reflect-metadata', 17 | 'react', 18 | 'react-dom', 19 | ], 20 | }, 21 | output: { 22 | publicPath: '/dist/', 23 | filename: '[name].js', 24 | path: path.resolve(__dirname, 'dist'), 25 | }, 26 | module: { 27 | rules: [ 28 | { 29 | test: /\.js?$/, 30 | exclude: [path.resolve(__dirname, 'node_modules')], 31 | loader: 'babel-loader', 32 | }, 33 | { 34 | test: /\.tsx?$/, 35 | loader: 'ts-loader', 36 | }, 37 | ], 38 | }, 39 | node: { 40 | fs: 'empty' 41 | }, 42 | resolve: { 43 | modules: [ 44 | __dirname, 45 | 'node_modules', 46 | ], 47 | }, 48 | plugins: [ 49 | new CleanWebpackPlugin(['dist']), 50 | new webpack.optimize.CommonsChunkPlugin({ 51 | name: 'common-dependencies', 52 | }), 53 | new ContextReplacementPlugin( 54 | /(.+)?angular(\\|\/)core(.+)?/, 55 | path.resolve(__dirname, '../src') 56 | ) 57 | ], 58 | devtool: 'source-map', 59 | externals: [ 60 | ], 61 | }; 62 | --------------------------------------------------------------------------------