Skip to content

Commit

Permalink
Initial commit, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Feb 29, 2024
0 parents commit 956c5f4
Show file tree
Hide file tree
Showing 57 changed files with 7,439 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These are supported funding model platforms

github: [smapiot, FlorianRappl]
custom: ['https://www.paypal.me/FlorianRappl']
31 changes: 31 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Publish Demo

on:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: "20.x"
registry-url: "https://registry.npmjs.org"
- name: Install Yarn and gh-pages
run: |
yarn install
npm install -g gh-pages@3.0.0
- name: Build Website
run: |
npx lerna run build
echo "cloud-module-federation.samples.piral.cloud" > packages/host-indirect/dist/CNAME
- name: Deploy Website
run: |
git remote set-url origin https://git:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
gh-pages -d "packages/host-indirect/dist" -u "github-actions-bot <support+actions@github.com>"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 piral-samples

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[![Piral Logo](https://github.com/smapiot/piral/raw/develop/docs/assets/logo.png)](https://piral.io)

# [Piral Cloud Sample](https://piral.cloud) &middot; [![GitHub License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/smapiot/piral/blob/main/LICENSE) [![Gitter Chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/piral-io/community)

> Sample project to illustrate a micro frontend discovery with Module Federation.
:zap: Use the Piral Feed Service for generic micro frontend discovery in the context of Module Federation-based micro frontends.

Feel free to play around with the code using StackBlitz.

[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/piral-samples/piral-cloud-module-federation-demo)

You can also visit this demo at [cloud-module-federation.samples.piral.cloud/](https://cloud-module-federation.samples.piral.cloud/).

## Structure

This repository contains the following packages / code elements:

- [host-direct](./packages/host-direct/): the host with direct MF integration - for local debugging
- [host-indirect](./packages/host-indirect/): the host with indirect MF integration through a MF discovery service - for production purposes
- [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

## Publishing to a Discovery Service

Following the same principles as outlined in [this article](https://dev.to/florianrappl/micro-frontend-discovery-the-driver-for-scalability-oai) the repository uses the `publish-microfrontend` helper CLI to publish individual packages to the Piral Feed Service.

The Piral Feed Service recognizes the format and shows the micro frontends already as Module Federation modules in the feed.

![Feed with Module Federation modules](./module-feed.png)

As feed representation we use the "Native Federation Remote". In this example the representation will look like this:

```json
{
"mf-blue": "https://assets.piral.cloud/pilets/native-federation-demo/mf-blue/1.0.0/remoteEntry.js",
"mf-green": "https://assets.piral.cloud/pilets/native-federation-demo/mf-green/1.0.0/remoteEntry.js",
"mf-red": "https://assets.piral.cloud/pilets/native-federation-demo/mf-red/1.0.3/remoteEntry.js"
}
```

The feed is inserted [in the root module of the host-indirect package](./packages/host-indirect/src/index.ts).

## License

Piral and this sample code is released using the MIT license. For more information see the [license file](./LICENSE).
6 changes: 6 additions & 0 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "./node_modules/lerna/schemas/lerna-schema.json",
"npmClient": "yarn",
"useWorkspaces": true,
"version": "1.0.0"
}
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "root",
"private": true,
"workspaces": [
"packages/*"
],
"scripts": {
"build": "lerna run build",
"publish:mf": "lerna run publish:mf",
"run:feed": "lerna run preview --stream --scope host-indirect",
"run:local": "lerna run start --stream --ignore host-indirect"
},
"devDependencies": {
"lerna": "^6.4.1"
}
}
28 changes: 28 additions & 0 deletions packages/blue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "blue",
"version": "1.0.0",
"description": "Micro frontend of the blue team",
"keywords": [],
"author": "Florian Rappl",
"license": "MIT",
"main": "dist/remoteEntry.js",
"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",
"build": "rspack build"
},
"dependencies": {
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@types/node": "18.11.18",
"@types/react": "18.0.27",
"@types/react-dom": "18.0.10",
"@rspack/core": "^0.5.5",
"@rspack/cli": "^0.5.5",
"publish-microfrontend": "1.5.0",
"typescript": "^5"
}
}
58 changes: 58 additions & 0 deletions packages/blue/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const rspack = require("@rspack/core");
const path = require("path");

const mf = new rspack.container.ModuleFederationPlugin({
name: "blue",
filename: "remoteEntry.js",
exposes: {
"./basketInfo": "./src/basket-info.tsx",
"./buyButton": "./src/buy-button.tsx",
},
remotes: {},
shared: {
react: { singleton: true },
"react-dom": { singleton: true },
},
});

module.exports = {
entry: {
main: "./src/main.ts",
},
output: {
path: path.resolve(__dirname, "dist"),
},
module: {
rules: [
{
test: /\.png$/,
type: "asset",
},
{
test: /\.jpg$/,
type: "asset",
},
{
test: /\.tsx?$/,
exclude: [/node_modules/],
loader: "builtin:swc-loader",
options: {
sourceMap: true,
jsc: {
parser: {
syntax: "typescript",
},
},
},
type: "javascript/auto",
},
],
},
resolve: {
extensions: [".js", ".ts", ".tsx", ".json", ".wasm"],
},
devServer: {
port: 2002,
},
plugins: [mf],
};
26 changes: 26 additions & 0 deletions packages/blue/src/basket-info.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "./style/basket-info.css";
import * as React from "react";
import { render } from "react-dom";

const BasketInfo = ({ sku = "porsche" }) => {
const [items, setItems] = React.useState([]);
const count = items.length;

React.useEffect(() => {
const handler = () => {
setItems((items) => [...items, sku]);
};
window.addEventListener("add-item", handler);
return () => window.removeEventListener("add-item", handler);
}, [sku]);

return (
<div className={count === 0 ? "empty" : "filled"}>basket: {count} item(s)</div>
);
};

export default BasketInfo;

export function renderBasketInfo(container: HTMLElement) {
render(<BasketInfo />, container);
}
31 changes: 31 additions & 0 deletions packages/blue/src/buy-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import "./style/buy-button.css";
import * as React from "react";
import { render } from "react-dom";

const defaultPrice = "0,00 €";
const prices = {
porsche: "66,00 €",
fendt: "54,00 €",
eicher: "58,00 €",
};

const BuyButton = ({ sku = "porsche" }) => {
const price = prices[sku] || defaultPrice;

return (
<form
onSubmit={(e) => {
e.preventDefault();
window.dispatchEvent(new CustomEvent("add-item", { detail: price }));
}}
>
<button>buy for {price}</button>
</form>
);
};

export default BuyButton;

export function renderBuyButton(container: HTMLElement) {
render(<BuyButton />, container);
}
9 changes: 9 additions & 0 deletions packages/blue/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { renderBasketInfo } from "./basket-info";
import { renderBuyButton } from "./buy-button";

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

renderBasketInfo(basketInfo);
renderBuyButton(buyButton);
Loading

0 comments on commit 956c5f4

Please sign in to comment.