Skip to content

Commit

Permalink
feat(nextjs-mf): update module share for rsc
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptedAlchemy committed Jan 3, 2025
1 parent 7a178c0 commit 10f2b73
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 118 deletions.
3 changes: 3 additions & 0 deletions apps/next-app-router-4000/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { demos } from '#/lib/demos';
import Link from 'next/link';
import dynamic from 'next/dynamic';
const Button = dynamic(() => import('remote_4001/Button'), { ssr: true });

export default function Page() {
return (
<div className="space- y-8">
<Button>Button from remote</Button>
<h1 className="text-xl font-medium text-gray-300">Examples</h1>

<div className="space-y-10 text-white">
Expand Down
35 changes: 18 additions & 17 deletions apps/next-app-router-4000/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ const nextConfig = {
// See: https://github.com/gregberge/svgr
svgr: false,
},
port: 4000,
webpack(config, options) {
const { isServer } = options;
config.watchOptions = {
ignored: ['**/node_modules/**', '**/@mf-types/**'],
};
// used for testing build output snapshots
const remotes = {
checkout: `checkout@http://localhost:3002/_next/static/${
remote_4001: `remote_4001@http://localhost:4001/_next/static/${
isServer ? 'ssr' : 'chunks'
}/remoteEntry.js`,
home_app: `home_app@http://localhost:3000/_next/static/${
checkout: `checkout@http://localhost:4000/_next/static/${
isServer ? 'ssr' : 'chunks'
}/remoteEntry.js`,
shop: `shop@http://localhost:3001/_next/static/${
home_app: `home_app@http://localhost:4000/_next/static/${
isServer ? 'ssr' : 'chunks'
}/remoteEntry.js`,
shop: `shop@http://localhost:4000/_next/static/${
isServer ? 'ssr' : 'chunks'
}/remoteEntry.js`,
};
Expand All @@ -33,28 +37,25 @@ const nextConfig = {
name: 'home_app',
filename: 'static/chunks/remoteEntry.js',
remotes: {
remote_4001: remotes.remote_4001,
shop: remotes.shop,
checkout: remotes.checkout,
},
// exposes: {
// './SharedNav': './components/SharedNav',
// './menu': './components/menu',
// },
shared: {
// 'lodash/': {},
// antd: {
// requiredVersion: '5.19.1',
// version: '5.19.1',
// },
// '@ant-design/': {
// 'react': {
// singleton: true,
// requiredVersion: false
// },
// 'react-dom': {
// singleton: true,
// requiredVersion: false
// }
},
extraOptions: {
debug: false,
exposePages: true,
enableImageLoaderFix: true,
enableUrlLoaderFix: true,
// debug: false,
// exposePages: true,
// enableImageLoaderFix: true,
// enableUrlLoaderFix: true,
},
}),
);
Expand Down
2 changes: 1 addition & 1 deletion apps/next-app-router-4000/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"scripts": {
"build": "next build",
"dev": "NEXT_PRIVATE_LOCAL_WEBPACK=true next dev",
"dev": "NEXT_PRIVATE_LOCAL_WEBPACK=true next dev -p 4000",
"lint": "next lint",
"lint-staged": "lint-staged",
"prettier": "prettier --write --ignore-unknown .",
Expand Down
20 changes: 3 additions & 17 deletions apps/next-app-router-4000/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,10 @@
]
},
"serve": {
"executor": "@nx/next:server",
"defaultConfiguration": "development",
"executor": "nx:run-commands",
"options": {
"buildTarget": "next-app-router-4000:build",
"dev": true,
"port": 4000
},
"configurations": {
"development": {
"buildTarget": "next-app-router-4000:build:development",
"dev": true,
"port": 4000
},
"production": {
"buildTarget": "next-app-router-4000:build:production",
"dev": false,
"port": 4000
}
"command": "pnpm dev",
"cwd": "apps/next-app-router-4000"
},
"dependsOn": [
{
Expand Down
80 changes: 80 additions & 0 deletions apps/next-app-router-4001/app/demo/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//@ts-check
'use client';

import dynamic from 'next/dynamic';

// Dynamically import remote components
const Button = dynamic(() => import('remote_4001/Button'), { ssr: true });
const Header = dynamic(() => import('remote_4001/Header'), { ssr: true });
const ProductCard = dynamic(() => import('remote_4001/ProductCard'), { ssr: true });
const TabGroup = dynamic(() => import('remote_4001/TabGroup'), { ssr: true });
const TabNavItem = dynamic(() => import('remote_4001/TabNavItem'), { ssr: true });
const CountUp = dynamic(() => import('remote_4001/CountUp'), { ssr: true });
const RenderingInfo = dynamic(() => import('remote_4001/RenderingInfo'), { ssr: true });

export default function DemoPage() {
return (
<div className="p-4">
<Header />

<main className="max-w-4xl mx-auto mt-8">
<h1 className="text-2xl font-bold mb-6">Remote Components Demo</h1>

<section className="mb-8">
<h2 className="text-xl font-semibold mb-4">Basic UI Components</h2>
<div className="space-x-4">
<Button>Primary Button</Button>
<Button>Secondary Button</Button>
</div>
</section>

<section className="mb-8">
<h2 className="text-xl font-semibold mb-4">Navigation Components</h2>
<TabGroup>
<TabNavItem href="/demo/tab1" isActive={true}>Tab 1</TabNavItem>
<TabNavItem href="/demo/tab2" isActive={false}>Tab 2</TabNavItem>
<TabNavItem href="/demo/tab3" isActive={false}>Tab 3</TabNavItem>
</TabGroup>
</section>

<section className="mb-8">
<h2 className="text-xl font-semibold mb-4">Product Components</h2>
<div className="grid grid-cols-2 gap-4">
<ProductCard
product={{
name: 'Demo Product',
price: 99.99,
description: 'This is a demo product to showcase the ProductCard component',
image: 'https://via.placeholder.com/300',
rating: 4.5,
}}
/>
<ProductCard
product={{
name: 'Another Product',
price: 149.99,
description: 'Another demo product with different details',
image: 'https://via.placeholder.com/300',
rating: 5,
}}
/>
</div>
</section>

<section className="mb-8">
<h2 className="text-xl font-semibold mb-4">Interactive Components</h2>
<div className="space-y-4">
<div className="p-4 border rounded">
<h3 className="font-medium mb-2">Count Up Animation</h3>
<CountUp start={0} end={1000} />
</div>
<div className="p-4 border rounded">
<h3 className="font-medium mb-2">Rendering Information</h3>
<RenderingInfo />
</div>
</div>
</section>
</main>
</div>
);
}
56 changes: 29 additions & 27 deletions apps/next-app-router-4001/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,42 @@ const nextConfig = {
config.watchOptions = {
ignored: ['**/node_modules/**', '**/@mf-types/**'],
};
// used for testing build output snapshots
const remotes = {
checkout: `checkout@http://localhost:3002/_next/static/${
isServer ? 'ssr' : 'chunks'
}/remoteEntry.js`,
home_app: `home_app@http://localhost:3000/_next/static/${
isServer ? 'ssr' : 'chunks'
}/remoteEntry.js`,
shop: `shop@http://localhost:3001/_next/static/${
isServer ? 'ssr' : 'chunks'
}/remoteEntry.js`,
};

config.plugins.push(
new NextFederationPlugin({
name: 'home_app',
name: 'remote_4001',
filename: 'static/chunks/remoteEntry.js',
remotes: {
shop: remotes.shop,
checkout: remotes.checkout,
exposes: {
// Core UI Components
'./Button': './ui/button',
// './Header': './ui/header',
// './Footer': './ui/footer',
// './GlobalNav': './ui/global-nav',
//
// // Product Related Components
// './ProductCard': './ui/product-card',
// './ProductPrice': './ui/product-price',
// './ProductRating': './ui/product-rating',
// './ProductDeal': './ui/product-deal',
//
// // Navigation Components
// './TabGroup': './ui/tab-group',
// './TabNavItem': './ui/tab-nav-item',
//
// // Utility Components
// './Boundary': './ui/boundary',
// './CountUp': './ui/count-up',
// './RenderedTimeAgo': './ui/rendered-time-ago',
// './RenderingInfo': './ui/rendering-info'
},
// exposes: {
// './SharedNav': './components/SharedNav',
// './menu': './components/menu',
// },
shared: {
// 'lodash/': {},
// antd: {
// requiredVersion: '5.19.1',
// version: '5.19.1',
// },
// '@ant-design/': {
// 'react': {
// singleton: true,
// requiredVersion: false
// },
// 'react-dom': {
// singleton: true,
// requiredVersion: false
// }
},
extraOptions: {
debug: false,
Expand Down
2 changes: 1 addition & 1 deletion apps/next-app-router-4001/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"scripts": {
"build": "next build",
"dev": "NEXT_PRIVATE_LOCAL_WEBPACK=true next dev",
"dev": "NEXT_PRIVATE_LOCAL_WEBPACK=true next dev -p 4001",
"lint": "next lint",
"lint-staged": "lint-staged",
"prettier": "prettier --write --ignore-unknown .",
Expand Down
20 changes: 3 additions & 17 deletions apps/next-app-router-4001/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,10 @@
]
},
"serve": {
"executor": "@nx/next:server",
"defaultConfiguration": "development",
"executor": "nx:run-commands",
"options": {
"buildTarget": "next-app-router-4001:build",
"dev": true,
"port": 4001
},
"configurations": {
"development": {
"buildTarget": "next-app-router-4001:build:development",
"dev": true,
"port": 4001
},
"production": {
"buildTarget": "next-app-router-4001:build:production",
"dev": false,
"port": 4001
}
"command": "pnpm dev",
"cwd": "apps/next-app-router-4001"
},
"dependsOn": [
{
Expand Down
5 changes: 4 additions & 1 deletion packages/nextjs-mf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "Module Federation helper for NextJS",
"repository": {
"type": "git",
"url": "https://github.com/module-federation/core/",
"url": "git+https://github.com/module-federation/core.git",
"directory": "packages/nextjs-mf"
},
"author": "Zack Jackson <[email protected]>",
Expand Down Expand Up @@ -48,6 +48,9 @@
"@module-federation/node": "workspace:*",
"@module-federation/webpack-bundler-runtime": "workspace:*"
},
"devDependencies": {
"@types/btoa": "^1.2.5"
},
"peerDependencies": {
"webpack": "^5.40.0",
"next": "^12 || ^13 || ^14 || ^15",
Expand Down
31 changes: 30 additions & 1 deletion packages/nextjs-mf/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"projectType": "library",
"tags": ["type:pkg"],
"targets": {
"build": {
"build-tsc": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
Expand All @@ -21,6 +21,35 @@
}
]
},
"build": {
"executor": "nx:run-commands",
"options": {
"parallel": false,
"commands": [
{
"command": "nx run nextjs-mf:build-tsc",
"forwardAllArgs": false
},
{
"command": "nx run nextjs-mf:rename-dist-files",
"forwardAllArgs": false
}
]
}
},
"rename-dist-files": {
"executor": "nx:run-commands",
"options": {
"commands": [
{
"command": "mv packages/nextjs-mf/dist/src/federation-noop.js packages/nextjs-mf/dist/src/federation-noop.cjs"
},
{
"command": "mv packages/nextjs-mf/dist/src/plugins/container/runtimePlugin.js packages/nextjs-mf/dist/src/plugins/container/runtimePlugin.cjs"
}
]
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
Expand Down
Loading

0 comments on commit 10f2b73

Please sign in to comment.