Skip to content

Commit

Permalink
app_consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
mlemaire committed Aug 26, 2024
1 parent 94bfa79 commit 717c64b
Show file tree
Hide file tree
Showing 21 changed files with 4,360 additions and 15 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ Module Federation in Next.js depends on <a href=« https://www.npmjs.com/package

1. run `pnpm install:all`
2. run `pnpm run start` and browse to `http://localhost:5001` for the app router consumer or `http://localhost:4001` for the page router consumer

## Module Federation implementation

Work in progress

- [x] page router consumer
- [x] page router provider
- [x] app router consumer
- [ ] app router provider
3 changes: 3 additions & 0 deletions app_consumer/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions app_consumer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
36 changes: 36 additions & 0 deletions app_consumer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
Binary file added app_consumer/app/favicon.ico
Binary file not shown.
33 changes: 33 additions & 0 deletions app_consumer/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
--foreground-rgb: 0, 0, 0;
--background-start-rgb: 214, 219, 220;
--background-end-rgb: 255, 255, 255;
}

@media (prefers-color-scheme: dark) {
:root {
--foreground-rgb: 255, 255, 255;
--background-start-rgb: 0, 0, 0;
--background-end-rgb: 0, 0, 0;
}
}

body {
color: rgb(var(--foreground-rgb));
background: linear-gradient(
to bottom,
transparent,
rgb(var(--background-end-rgb))
)
rgb(var(--background-start-rgb));
}

@layer utilities {
.text-balance {
text-wrap: balance;
}
}
22 changes: 22 additions & 0 deletions app_consumer/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
);
}
53 changes: 53 additions & 0 deletions app_consumer/app/mfe_import.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use client";
import { init, loadRemote } from "@module-federation/enhanced/runtime";
import { lazy, Suspense, useEffect, useState } from "react";
import React from "react";
import nodeRuntimePlugin from "@module-federation/node/runtimePlugin";

const isServer = typeof window === "undefined";

init({
name: "app_consumer",
remotes: [
{
name: "page_provider",
entry: `http://localhost:4000/_next/static/${
isServer ? "ssr" : "chunks"
}/remoteEntry.js`,
},
],
shared: {
react: {
version: "18.2.0",
scope: "default",
lib: () => React,
shareConfig: {
singleton: true,
requiredVersion: "^18.2.0",
},
},
},
plugins: [nodeRuntimePlugin()],
});

const LoadedRemoteComponent = lazy(() => {
return loadRemote("page_provider/RemoteComponent", {
from: "runtime",
});
});

export default function RemoteComponent() {
const [isClient, setClient] = useState(false);

useEffect(() => {
setClient(true);
}, []);

if (!isClient) return <p>burkk...</p>;

return (
<Suspense fallback={<div>Loading...</div>}>
{<LoadedRemoteComponent />}
</Suspense>
);
}
28 changes: 28 additions & 0 deletions app_consumer/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import RemoteComponent from "./mfe_import";

export default function Home() {
return (
<main>
<div className="flex flex-col items-center justify-center gap-4 py-20">
<h1 className="text-4xl font-bold text-slate-200">
Nextjs (app router)
</h1>
<p className="text-xl font-bold text-slate-200">
Application using an MFE component
</p>
<div className="border-2 border-green-800 rounded-lg p-4">
<p className="text-sm italic text-slate-400">
MFE nextjs (page router)
</p>
<RemoteComponent />
</div>
<div className="border-2 border-green-800 rounded-lg p-4">
<p className="text-sm italic text-slate-400">
MFE nextjs (app router)
</p>
{/* <RemoteComponent /> */}
</div>
</div>
</main>
);
}
4 changes: 4 additions & 0 deletions app_consumer/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
31 changes: 31 additions & 0 deletions app_consumer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "app_consumer",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev -p 5001",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@module-federation/enhanced": "^0.5.1",
"@module-federation/nextjs-mf": "^8.4.11",
"@module-federation/node": "^2.5.8",
"next": "14.2.6",
"react": "^18",
"react-dom": "^18",
"runtime": "link:@module-federation/enhanced/runtime",
"runtimePlugin": "link:@module-federation/node/runtimePlugin"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "14.2.6",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"typescript": "^5"
}
}
Loading

0 comments on commit 717c64b

Please sign in to comment.