A modular, independently deployable game center application built with React that can be integrated into your portfolio website using micro frontend architecture.
This Game Center is built as a standalone micro frontend that can be:
- Developed and tested independently
- Deployed to its own hosting environment
- Integrated seamlessly into your main portfolio application
- Extended with new games without modifying the host application
- 🎮 Modular game architecture
- 🔄 Shared game resources (asset loading, audio, game engine)
- 🏆 Persistent high scores across games
- 🚀 Lazy-loaded game components for optimal performance
- 🔌 Exposes components via Webpack Module Federation
- 🖥️ Works as both standalone app and integrated component
- React + Create React App (with CRACO for configuration)
- Webpack 5 Module Federation
- React Router for game navigation
- Canvas-based game rendering
- Docker + Nginx for deployment
game-center/
├── craco.config.js # CRACO webpack customization
├── package.json
├── public/
│ ├── assets/ # Game assets (sprites, sounds)
│ └── index.html
├── src/
│ ├── components/
│ │ └── GameCenter/ # Main Game Center component
│ │ ├── index.jsx # Entry point exposed to host apps
│ │ ├── components/ # Shared UI components
│ │ │ ├── Loading.jsx
│ │ │ └── GameSelection.jsx
│ │ ├── shared/ # Shared resources
│ │ │ ├── GameContext.jsx # Context for shared state
│ │ │ ├── AssetLoader.js # Asset loading utility
│ │ │ ├── GameEngine.js # Shared game engine
│ │ │ └── AudioManager.js # Audio management
│ │ └── games/ # Individual game components
│ │ ├── PuzzleGame.jsx
│ │ └── ArcadeGame.jsx
│ └── index.js # Main app entry point
├── Dockerfile # Production Docker build
└── nginx.conf # Nginx configuration for production
- Node.js 16+ and npm
- Basic knowledge of React and JavaScript
-
Clone this repository:
git clone https://github.com/HoseaCodes/GameCenter.git cd game-center -
Install dependencies:
npm install
-
Start the development server:
npm start
-
Visit http://localhost:3001 to view the Game Center in standalone mode.
To add a new game to the Game Center:
- Create a new game component in the
src/components/GameCenter/games/directory - Utilize the shared context via
useGameResources()hook for assets, audio, and game engine - Register the game in the main GameCenter component by adding it to the lazy-loaded imports and routes
- Add the game to the selection screen in
GameSelection.jsx
Example new game:
// src/components/GameCenter/games/SpaceGame.jsx
import React, { useEffect, useRef } from 'react';
import { useGameResources } from '../shared/GameContext';
export default function SpaceGame() {
const canvasRef = useRef(null);
const { assetLoader, gameEngine, audioManager, setScores } = useGameResources();
useEffect(() => {
// Load game-specific assets
const loadAssets = async () => {
await assetLoader.loadImage('space-bg', '/assets/space-bg.png');
await audioManager.loadSound('laser', '/assets/laser.mp3');
};
loadAssets();
// Initialize game with shared engine
const canvas = canvasRef.current;
const game = gameEngine.createGame(canvas, {
type: 'space',
onScore: (score) => {
setScores(prev => ({...prev, space: Math.max(prev.space || 0, score)}));
}
});
// Start game loop
game.start();
return () => game.destroy();
}, [assetLoader, gameEngine, audioManager, setScores]);
return (
<div className="space-game">
<h2>Space Adventure</h2>
<canvas ref={canvasRef} width={800} height={600} />
</div>
);
}To integrate the Game Center into your portfolio:
-
Install required dependencies in your portfolio project:
npm install @craco/craco @module-federation/nextjs-mf
-
Create a CRACO configuration file (
craco.config.js) in your portfolio project:const { ModuleFederationPlugin } = require('webpack').container; module.exports = { webpack: { configure: (webpackConfig) => { webpackConfig.output.publicPath = 'auto'; webpackConfig.plugins.push( new ModuleFederationPlugin({ name: 'portfolio', remotes: { gameCenter: process.env.NODE_ENV === 'production' ? 'gameCenter@https://your-game-center-url.com/remoteEntry.js' : 'gameCenter@http://localhost:3001/remoteEntry.js', }, shared: { react: { singleton: true, requiredVersion: require('./package.json').dependencies.react }, 'react-dom': { singleton: true, requiredVersion: require('./package.json').dependencies['react-dom'] }, }, }) ); return webpackConfig; }, }, };
-
Update your portfolio's
package.jsonscripts to use CRACO:"scripts": { "start": "craco start", "build": "craco build", "test": "craco test" }
-
Create a wrapper component in your portfolio:
// src/components/GameCenterWrapper.jsx import React, { Suspense, useState, useEffect } from 'react'; const GameCenterWrapper = () => { const [GameCenter, setGameCenter] = useState(null); const [error, setError] = useState(null); useEffect(() => { import('gameCenter/GameCenter') .then((module) => { setGameCenter(() => module.default); }) .catch((err) => { console.error('Failed to load Game Center:', err); setError('Failed to load Game Center. Please try again later.'); }); }, []); if (error) { return <div className="error-message">{error}</div>; } return ( <div className="game-center-wrapper"> <Suspense fallback={<div>Loading Game Center...</div>}> {GameCenter ? <GameCenter /> : <div>Initializing...</div>} </Suspense> </div> ); }; export default GameCenterWrapper;
-
Add the Game Center to your portfolio routes:
<Route path="/games/*" element={<GameCenterWrapper />} />
Build and deploy the Game Center using Docker:
# Build the Docker image
docker build -t your-name-game-center .
# Run the container
docker run --name your-game-center-c -p 3002:3002 -d your-name-game-centerFor manual deployment:
-
Build the production bundle:
npm run build
-
Deploy the contents of the
builddirectory to your hosting provider. -
Ensure your web server is configured to handle client-side routing (similar to the provided
nginx.conf).
The Game Center can be configured through environment variables:
# .env file
REACT_APP_PUBLIC_PATH=https://your-game-center-url.com/
REACT_APP_PORT=3001
-
Module Federation connection error:
- Check that both applications are running
- Verify URLs in the CRACO configuration
- Ensure version compatibility between shared dependencies
-
Game resources not loading:
- Check that assets are in the correct location
- Verify paths in the asset loader
-
Route not found in production:
- Ensure your web server is configured for client-side routing (see nginx.conf)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by Dominique Hosea's article on micro frontend architecture
- Built with React and WebPack Module Federation