Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
timothymalcham committed Jun 17, 2020
0 parents commit c285820
Show file tree
Hide file tree
Showing 28 changed files with 3,989 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Demo of webpack 5 module federation micro frontends.

do `yarn && yarn start` to spin up all three apps.
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "micro-frontend-module-federation-demo",
"private": true,
"workspaces": [
"packages/*"
],
"scripts": {
"start": "concurrently \"wsrun --parallel start\" \"wsrun --parallel serve\""
},
"devDependencies": {
"wsrun": "^5.2.0"
},
"dependencies": {
"concurrently": "^5.1.0"
}
}
Binary file added packages/.DS_Store
Binary file not shown.
22 changes: 22 additions & 0 deletions packages/01-host/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "01-host",
"version": "0.0.0",
"private": true,
"dependencies": {
"@babel/core": "^7.8.6",
"@babel/preset-react": "^7.8.3",
"babel-loader": "^8.0.6",
"html-webpack-plugin": "git://github.com/ScriptedAlchemy/html-webpack-plugin#master",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-router-dom": "^5.1.2",
"serve": "^11.3.0",
"webpack": "git://github.com/webpack/webpack.git#dev-1",
"webpack-cli": "^3.3.11"
},
"scripts": {
"start": "webpack --watch",
"build": "webpack --mode production",
"serve": "serve dist -p 3001"
}
}
10 changes: 10 additions & 0 deletions packages/01-host/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://localhost:3002/remoteEntry.js"></script>
<script src="http://localhost:3003/remoteEntry.js"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
40 changes: 40 additions & 0 deletions packages/01-host/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import { Link } from "react-router-dom";

import Page1 from "./pages/page1";
import Page2 from "./pages/page2";

function App() {
return (
<BrowserRouter>
<div>
<ul>
<li style={{ display: "block" }}>
<Link to="/">Host / App #1</Link>
</li>
<li style={{ display: "block" }}>
<Link to="/page1">Page 1 / App #2</Link>
</li>
<li style={{ display: "block" }}>
<Link to="/page2">Page 2 / App #3</Link>
</li>
</ul>
</div>
<hr />
<Switch>
<Route path="/">
This is the container from the host app
<Route path="/page1">
<Page1 />
</Route>
<Route path="/page2">
<Page2 />
</Route>
</Route>
</Switch>
</BrowserRouter>
);
}

export default App;
4 changes: 4 additions & 0 deletions packages/01-host/src/bootstrap.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import App from "./App";
import React from "react";
import ReactDOM from "react-dom";
ReactDOM.render(<App />, document.getElementById("root"));
1 change: 1 addition & 0 deletions packages/01-host/src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import("./bootstrap");
16 changes: 16 additions & 0 deletions packages/01-host/src/pages/page1.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";

const Page = React.lazy(() => import("app_two/Page"));

const Page1 = () => {
return (
<div>
<h1>Page 1</h1>
<React.Suspense fallback="Loading page from App #2">
<Page />
</React.Suspense>
</div>
);
};

export default Page1;
14 changes: 14 additions & 0 deletions packages/01-host/src/pages/page2.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";

const Message = React.lazy(() => import("app_three/Message"));

const Page2 = () => (
<div>
<h1>Page 2</h1>
<React.Suspense fallback="Loading message from app #3">
<Message />
</React.Suspense>
</div>
);

export default Page2;
59 changes: 59 additions & 0 deletions packages/01-host/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");

module.exports = {
entry: "./src/index",
cache: false,

mode: "development",
devtool: "source-map",

optimization: {
minimize: false,
},

output: {
publicPath: "http://localhost:3001/",
},

resolve: {
extensions: [".jsx", ".js", ".json"],
},

module: {
rules: [
{
test: /\.jsx?$/,
loader: require.resolve("babel-loader"),
options: {
presets: [require.resolve("@babel/preset-react")],
},
},
],
},

plugins: [
new ModuleFederationPlugin({
// define the host app
name: "app_one",
library: { type: "var", name: "app_one" },
// this file will be automatically generated and put in /dist
// this is the file that index.html will use to render the app
filename: "remoteEntry.js",
// define the other micro-frontends
remotes: {
app_two: "app_two",
app_three: "app_three",
},
// determines what the other apps can load/share
exposes: {
AppContainer: "./src/App",
},
// shared dependencies, helpful to make builds smaller
shared: ["react", "react-dom", "react-router-dom"],
}),
new HtmlWebpackPlugin({
template: "./public/index.html",
}),
],
};
21 changes: 21 additions & 0 deletions packages/02-another-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "02-another-app",
"version": "0.0.0",
"private": true,
"dependencies": {
"@babel/core": "^7.8.6",
"@babel/preset-react": "^7.8.3",
"babel-loader": "^8.0.6",
"html-webpack-plugin": "git://github.com/ScriptedAlchemy/html-webpack-plugin#master",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"serve": "^11.3.0",
"webpack": "git://github.com/webpack/webpack.git#dev-1",
"webpack-cli": "^3.3.11"
},
"scripts": {
"start": "webpack --watch",
"build": "webpack --mode production",
"serve": "serve dist -p 3002"
}
}
9 changes: 9 additions & 0 deletions packages/02-another-app/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://localhost:3001/remoteEntry.js"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
15 changes: 15 additions & 0 deletions packages/02-another-app/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react";
import Routes from "./Routes";
const AppContainer = React.lazy(() => import("app_one/AppContainer"));

const App = () => {
return (
<div>
<React.Suspense fallback="Working on loading the container for the app from the host app">
<AppContainer routes={Routes} />
</React.Suspense>
</div>
);
};

export default App;
41 changes: 41 additions & 0 deletions packages/02-another-app/src/Page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from "react";

export default function Page() {
return (
<div>
<h2>I'm a page from App #2!!</h2>
<iframe
src="https://giphy.com/embed/YlPeYXasYEPpC"
width="480"
height="270"
frameBorder="0"
class="giphy-embed"
allowFullScreen
></iframe>
<iframe
src="https://giphy.com/embed/VelhDI6pdStJ4fvAtJ"
width="480"
height="270"
frameBorder="0"
class="giphy-embed"
allowFullScreen
></iframe>
<iframe
src="https://giphy.com/embed/oYtVHSxngR3lC"
width="480"
height="347"
frameBorder="0"
class="giphy-embed"
allowFullScreen
></iframe>
<iframe
src="https://giphy.com/embed/5VKbvrjxpVJCM"
width="480"
height="384"
frameBorder="0"
class="giphy-embed"
allowFullScreen
></iframe>
</div>
);
}
14 changes: 14 additions & 0 deletions packages/02-another-app/src/Routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";
import { Route, Switch } from "react-router-dom";

import Page from "./Page";

const Routes = () => (
<Switch>
<Route path="/">
<Page />
</Route>
</Switch>
);

export default Routes;
5 changes: 5 additions & 0 deletions packages/02-another-app/src/bootstrap.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import App from "./App";
import React from "react";
import ReactDOM from "react-dom";

ReactDOM.render(<App />, document.getElementById("root"));
1 change: 1 addition & 0 deletions packages/02-another-app/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import("./bootstrap");
53 changes: 53 additions & 0 deletions packages/02-another-app/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");

module.exports = {
entry: "./src/index",
cache: false,

mode: "development",
devtool: "source-map",

optimization: {
minimize: false,
},

output: {
publicPath: "http://localhost:3002/",
},

resolve: {
extensions: [".jsx", ".js", ".json"],
},

module: {
rules: [
{
test: /\.jsx?$/,
loader: require.resolve("babel-loader"),
options: {
presets: [require.resolve("@babel/preset-react")],
},
},
],
},

plugins: [
new ModuleFederationPlugin({
name: "app_two",
library: { type: "var", name: "app_two" },
filename: "remoteEntry.js",
exposes: {
Page: "./src/Page",
},
remotes: {
app_one: "app_one",
},
shared: ["react", "react-dom", "react-router-dom"],
}),
new HtmlWebpackPlugin({
template: "./public/index.html",
chunks: ["main"],
}),
],
};
21 changes: 21 additions & 0 deletions packages/03-another-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "03-another-app",
"version": "0.0.0",
"private": true,
"dependencies": {
"@babel/core": "^7.8.6",
"@babel/preset-react": "^7.8.3",
"babel-loader": "^8.0.6",
"html-webpack-plugin": "git://github.com/ScriptedAlchemy/html-webpack-plugin#master",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"serve": "^11.3.0",
"webpack": "git://github.com/webpack/webpack.git#dev-1",
"webpack-cli": "^3.3.11"
},
"scripts": {
"start": "webpack --watch",
"build": "webpack --mode production",
"serve": "serve dist -p 3003"
}
}
6 changes: 6 additions & 0 deletions packages/03-another-app/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE html>
<html lang="en">
<body>
<div id="root"></div>
</body>
</html>
Loading

0 comments on commit c285820

Please sign in to comment.