Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Feb 27, 2024
0 parents commit d2710ff
Show file tree
Hide file tree
Showing 77 changed files with 6,672 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-native-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.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[![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 Native Federation.
:zap: Use the Piral Feed Service for generic micro frontend discovery in the context of Native 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-native-federation-demo)

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

## Structure

This repository contains the following packages / code elements:

- [esbuild](./packages/esbuild/): the build adapter to use esbuild for the bundling
- [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
- [shared](./packages/shared/): the shared loader that enables cross-MF component sharing

## 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 Native Federation modules in the feed.

![Feed with Native Federation modules](./native-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.json",
"mf-green": "https://assets.piral.cloud/pilets/native-federation-demo/mf-green/1.0.0/remoteEntry.json",
"mf-red": "https://assets.piral.cloud/pilets/native-federation-demo/mf-red/1.0.3/remoteEntry.json"
}
```

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"
}
Binary file added native-feed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 preview --stream --ignore host-indirect"
},
"devDependencies": {
"lerna": "^6.4.1"
}
}
20 changes: 20 additions & 0 deletions packages/esbuild/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "native-federation-esbuild",
"version": "1.0.0",
"type": "commonjs",
"scripts": {
"build": "tsc"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"dependencies": {
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-replace": "^4.0.0",
"rollup": "^2.79.0",
"rollup-plugin-node-externals": "^4.1.1",
"esbuild": "0.18.20",
"npmlog": "^6.0.2",
"acorn": "^8.8.1"
}
}
82 changes: 82 additions & 0 deletions packages/esbuild/src/collect-exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as fs from "fs";
import { parse } from "acorn";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Node = any;

type visitFn = (node: Node) => void;

export function collectExports(path: string) {
const src = fs.readFileSync(path, "utf8");

const parseTree = parse(src, {
ecmaVersion: "latest",
allowHashBang: true,
sourceType: "module",
});

let hasDefaultExport = false;
let hasFurtherExports = false;
let defaultExportName = "";
const exports = new Set<string>();

traverse(parseTree, (node) => {
if (
node.type === "AssignmentExpression" &&
node?.left?.object?.name === "exports" // &&
) {
exports.add(node.left.property?.name);
return;
}

if (hasDefaultExport && hasFurtherExports) {
return;
}

if (node.type !== "ExportNamedDeclaration") {
return;
}

if (!node.specifiers) {
hasFurtherExports = true;
return;
}

for (const s of node.specifiers) {
if (isDefaultExport(s)) {
defaultExportName = s?.local?.name;
hasDefaultExport = true;
} else {
hasFurtherExports = true;
}
}
});

return {
hasDefaultExport,
hasFurtherExports,
defaultExportName,
exports: [...exports],
};
}

function traverse(node: Node, visit: visitFn) {
visit(node);
for (const key in node) {
const prop = node[key];
if (prop && typeof prop === "object") {
traverse(prop as Node, visit);
} else if (Array.isArray(prop)) {
for (const sub of prop) {
traverse(sub, visit);
}
}
}
}

function isDefaultExport(exportSpecifier: Node) {
return (
exportSpecifier.exported?.type === "Identifier" &&
exportSpecifier.exported?.name === "default"
);
}
Loading

0 comments on commit d2710ff

Please sign in to comment.