Skip to content

Commit

Permalink
Added shared part
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Mar 4, 2024
1 parent 956c5f4 commit a53eaa1
Show file tree
Hide file tree
Showing 25 changed files with 129 additions and 57 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This repository contains the following packages / code elements:
- [mf-blue](./packages/mf-blue/): the MF with the buy and basket components from the blue team
- [mf-green](./packages/mf-green/): the MF with the recommendations component from the green team
- [mf-red](./packages/mf-red/): the MF with the product details component from the red team
- [shared](./packages/shared/): the shared loader that enables cross-MF component sharing

## Publishing to a Discovery Service

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "lerna run build",
"publish:mf": "lerna run publish:mf",
"run:feed": "lerna run preview --stream --scope host-indirect",
"run:feed": "lerna run start --stream --scope host-indirect",
"run:local": "lerna run start --stream --ignore host-indirect"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/blue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"start": "rspack serve",
"preview": "npx http-server dist --port 2002 --cors",
"publish:mf": "npx publish-microfrontend --url http://localhost:9000/api/v1/pilet/module-demo --interactive",
"publish:mf": "npx publish-microfrontend --url https://feed.dev.piral.cloud/api/v1/pilet/module-demo --api-key 156ec80b17bb31b6cc304667af6bfaec97c8d2be77dd51f38edb85b6f04017ba",
"build": "rspack build"
},
"dependencies": {
Expand Down
3 changes: 3 additions & 0 deletions packages/blue/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ module.exports = {
},
resolve: {
extensions: [".js", ".ts", ".tsx", ".json", ".wasm"],
alias: {
'@shared/loader': path.resolve(__dirname, '../shared/loader.ts'),
},
},
devServer: {
port: 2002,
Expand Down
3 changes: 3 additions & 0 deletions packages/blue/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { setup } from "@shared/loader";
import { renderBasketInfo } from "./basket-info";
import { renderBuyButton } from "./buy-button";

setup();

const app = document.querySelector('#app');
const basketInfo = app.appendChild(document.createElement('div'));
const buyButton = app.appendChild(document.createElement('div'));
Expand Down
5 changes: 4 additions & 1 deletion packages/blue/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"esModuleInterop": true,
"jsx": "react",
"allowJs": true,
"checkJs": true
"checkJs": true,
"paths": {
"@shared/*": ["../shared/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx"]
}
2 changes: 1 addition & 1 deletion packages/green/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"start": "rspack serve",
"preview": "npx http-server dist --port 2003 --cors",
"publish:mf": "npx publish-microfrontend --url http://localhost:9000/api/v1/pilet/module-demo --interactive",
"publish:mf": "npx publish-microfrontend --url https://feed.dev.piral.cloud/api/v1/pilet/module-demo --api-key 156ec80b17bb31b6cc304667af6bfaec97c8d2be77dd51f38edb85b6f04017ba",
"build": "rspack build"
},
"dependencies": {
Expand Down
3 changes: 3 additions & 0 deletions packages/green/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ module.exports = {
},
resolve: {
extensions: [".js", ".ts", ".tsx", ".json", ".wasm"],
alias: {
'@shared/loader': path.resolve(__dirname, '../shared/loader.ts'),
},
},
devServer: {
port: 2003,
Expand Down
3 changes: 3 additions & 0 deletions packages/green/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { setup } from "@shared/loader";
import { renderRecommendations } from "./product-recommendations";

setup();

const app = document.querySelector('#app');
const recommendations = app.appendChild(document.createElement('div'));

Expand Down
5 changes: 4 additions & 1 deletion packages/green/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"esModuleInterop": true,
"jsx": "react",
"allowJs": true,
"checkJs": true
"checkJs": true,
"paths": {
"@shared/*": ["../shared/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx"]
}
6 changes: 3 additions & 3 deletions packages/host-direct/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ const rspack = require("@rspack/core");
const path = require("path");

const mf = new rspack.container.ModuleFederationPlugin({
remotes: {
red: 'red@http://localhost:2001/remoteEntry.js'
},
shared: {
react: { singleton: true },
"react-dom": { singleton: true },
Expand Down Expand Up @@ -50,6 +47,9 @@ module.exports = {
},
resolve: {
extensions: [".js", ".ts", ".tsx", ".json", ".wasm"],
alias: {
'@shared/loader': path.resolve(__dirname, '../shared/loader.ts'),
},
},
devServer: {
port: 1234,
Expand Down
21 changes: 20 additions & 1 deletion packages/host-direct/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { setup } from "@shared/loader";
import "./style.css";

import("red/product-page").then(({ renderProductPage }) => {
setup({
name: "host",
remotes: [
{
name: "red",
entry: "http://localhost:2001/remoteEntry.js",
},
{
name: "blue",
entry: "http://localhost:2002/remoteEntry.js",
},
{
name: "green",
entry: "http://localhost:2003/remoteEntry.js",
},
],
});

window.loadRemote("red/product-page").then(({ renderProductPage }) => {
const root = document.querySelector("#app");
renderProductPage(root);
});
5 changes: 4 additions & 1 deletion packages/host-direct/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"esModuleInterop": true,
"jsx": "react",
"allowJs": true,
"checkJs": true
"checkJs": true,
"paths": {
"@shared/*": ["../shared/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx"]
}
3 changes: 3 additions & 0 deletions packages/host-indirect/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ module.exports = {
},
resolve: {
extensions: [".js", ".ts", ".tsx", ".json", ".wasm"],
alias: {
'@shared/loader': path.resolve(__dirname, '../shared/loader.ts'),
},
},
devServer: {
port: 1234,
Expand Down
11 changes: 0 additions & 11 deletions packages/host-indirect/src/bootstrap.tsx

This file was deleted.

18 changes: 0 additions & 18 deletions packages/host-indirect/src/federation.ts

This file was deleted.

14 changes: 9 additions & 5 deletions packages/host-indirect/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import "./style.css";
import { init, loadRemote } from "@module-federation/runtime";
import { setup } from "@shared/loader";

fetch("http://localhost:9000/api/v1/pilet/microfrontends")
fetch("https://module-demo.my.dev.piral.cloud/api/v1/importmap")
.then((res) => res.json())
.then(async ({ remotes }) => {
init({
.then(async ({ imports }) => {
const remotes = Object.entries(imports).map(
([name, entry]: [string, string]) => ({ name, entry })
);

setup({
name: "host",
remotes, // array of objects: { name, entry }
});

const { renderProductPage }: any = await loadRemote("red/product-page");
const { renderProductPage }: any = await window.loadRemote("red/product-page");
const root = document.querySelector("#app");
renderProductPage(root);
});
5 changes: 4 additions & 1 deletion packages/host-indirect/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"esModuleInterop": true,
"jsx": "react",
"allowJs": true,
"checkJs": true
"checkJs": true,
"paths": {
"@shared/*": ["../shared/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx"]
}
4 changes: 2 additions & 2 deletions packages/red/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "red",
"version": "1.0.0",
"version": "1.0.2",
"description": "Micro frontend of the red team",
"keywords": [],
"author": "Florian Rappl",
Expand All @@ -9,7 +9,7 @@
"scripts": {
"start": "rspack serve",
"preview": "npx http-server dist --port 2001 --cors",
"publish:mf": "npx publish-microfrontend --url http://localhost:9000/api/v1/pilet/module-demo --interactive",
"publish:mf": "npx publish-microfrontend --url https://feed.dev.piral.cloud/api/v1/pilet/module-demo --api-key 156ec80b17bb31b6cc304667af6bfaec97c8d2be77dd51f38edb85b6f04017ba",
"build": "rspack build"
},
"dependencies": {
Expand Down
8 changes: 4 additions & 4 deletions packages/red/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ const mf = new rspack.container.ModuleFederationPlugin({
exposes: {
"./product-page": "./src/product-page.tsx",
},
remotes: {
blue: 'blue@http://localhost:2002/remoteEntry.js',
green: 'green@http://localhost:2003/remoteEntry.js',
},
remotes: {},
shared: {
react: { singleton: true },
"react-dom": { singleton: true },
Expand Down Expand Up @@ -52,6 +49,9 @@ module.exports = {
},
resolve: {
extensions: [".js", ".ts", ".tsx", ".json", ".wasm"],
alias: {
'@shared/loader': path.resolve(__dirname, '../shared/loader.ts'),
},
},
devServer: {
port: 2001,
Expand Down
19 changes: 17 additions & 2 deletions packages/red/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { setup } from "@shared/loader";
import { renderProductPage } from "./product-page";

const app = document.querySelector('#app');
const page = app.appendChild(document.createElement('div'));
setup({
name: "red",
remotes: [
{
name: "blue",
entry: "http://localhost:2002/remoteEntry.js",
},
{
name: "green",
entry: "http://localhost:2003/remoteEntry.js",
},
],
});

const app = document.querySelector("#app");
const page = app.appendChild(document.createElement("div"));

renderProductPage(page);
8 changes: 5 additions & 3 deletions packages/red/src/product-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ const product = {
],
};

const BasketInfo = React.lazy(() => import('blue/basketInfo'));
const BuyButton = React.lazy(() => import('blue/buyButton'));
const ProductRecommendations = React.lazy(() => import('green/recommendations'));
const BasketInfo = React.lazy(() => window.loadRemote("blue/basketInfo"));
const BuyButton = React.lazy(() => window.loadRemote("blue/buyButton"));
const ProductRecommendations = React.lazy(() =>
window.loadRemote("green/recommendations")
);

function getCurrent(sku: string) {
return product.variants.find((v) => v.sku === sku) || product.variants[0];
Expand Down
5 changes: 4 additions & 1 deletion packages/red/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"esModuleInterop": true,
"jsx": "react",
"allowJs": true,
"checkJs": true
"checkJs": true,
"paths": {
"@shared/*": ["../shared/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx"]
}
15 changes: 15 additions & 0 deletions packages/shared/loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { init, loadRemote } from "@module-federation/runtime";

export type SetupOptions = Parameters<typeof init>["0"];

export function setup(options?: SetupOptions) {
init(options);

window.loadRemote = (moduleId) => loadRemote(moduleId);
}

declare global {
interface Window {
loadRemote(moduleId: string): Promise<any>;
}
}
15 changes: 15 additions & 0 deletions packages/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"strict": false,
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"jsx": "react",
"allowJs": true,
"checkJs": true
},
"include": ["*.ts", "*.tsx"]
}

0 comments on commit a53eaa1

Please sign in to comment.