This project demonstrates a micro-frontend architecture using Vite Module Federation, consisting of a host application and multiple remote applications.
βββ apps/
β βββ host/ # Main application
β βββ remote1/ # Remote application 1
β βββ remote2/ # Remote application 2
βββ .env.development # Development environment variables
βββ .env.staging # Staging environment variables
βββ .env.production # Production environment variables
βββ batch/ # Utility batch scripts
- π Module Federation with Vite
- βοΈ React with TypeScript support
- π£οΈ React Router for navigation
- π¦ Redux Toolkit for state management
- π§ Environment-specific configurations
- π Batch scripts for easy deployment
- Node.js 18 or higher
- npm 8 or higher
- Run the installation script:
install-all.bat
This will install dependencies for all applications (host and remotes).
Start the development servers:
start-dev.bat
This will run all applications concurrently:
- Host: http://localhost:5000
- Remote1: http://localhost:5001
- Remote2: http://localhost:5002
Build scripts are available for different environments:
build-dev.bat # Development build
build-staging.bat # Staging build
build-prod.bat # Production build
-
Create a new application in the
apps
directory -
Update environment files with the new remote's URL:
VITE_REMOTE3_URL=http://localhost:5002
-
Update the host's vite.config.js to include the new remote:
federation({ remotes: { remote3: `${env.VITE_REMOTE3_URL}/assets/remoteEntry.js`, }, });
-
Add the new route in
apps/host/src/routes/index.jsx
-
Update batch scripts to include the new remote
To integrate a paid template:
-
Create a new remote application
-
Copy the template files into the remote's src directory
-
Update the remote's vite.config.js to expose needed components:
federation({ exposes: { "./TemplateComponent": "./src/components/TemplateComponent.jsx", }, });
-
Import and use the template components in the host application
VITE_API_URL
: Backend API URLVITE_REMOTE*_URL
: URLs for remote applications
Redux store is configured in apps/host/src/store
:
appSlice.js
: Global application state- Add new slices for additional features
Routes are defined in apps/host/src/routes/index.jsx
:
- Protected routes can be added using route guards
- Lazy loading is implemented for remote applications
Common utilities are available in apps/host/src/utils
:
constants.js
: Application constantshelpers.js
: Helper functions
- Keep remote applications focused and independent
- Share common dependencies through Module Federation
- Use TypeScript for better type safety
- Implement proper error boundaries
- Follow consistent coding standards
- Choose the appropriate build script for your environment
- Update environment variables in the corresponding .env file
- Run the build script
- Deploy the built assets to your hosting service
Common issues:
-
Module Federation connection errors:
- Check if remote applications are running
- Verify remote URLs in environment files
-
Build failures:
- Ensure all dependencies are installed
- Check for environment-specific configurations
-
Runtime errors:
- Verify shared dependencies versions
- Check browser console for detailed errors
Dependencies installed in the root package.json
are shared across all applications (host and remotes) through npm workspaces. This provides several benefits:
- Common dependencies are installed once and shared
- Consistent versions across all applications
- Reduced disk space usage
- Simplified dependency management
To add a shared dependency:
npm install <package-name> -w
Each application (host/remotes) can have its own dependencies in its respective package.json
. To add a dependency to a specific application:
npm install <package-name> -w <app-name>
Example:
npm install @mui/material -w host
apps/host/
βββ src/
β βββ components/ # Reusable UI components
β βββ pages/ # Page components
β βββ routes/ # Routing configuration
β βββ store/ # Redux store and slices
β βββ utils/ # Shared utilities
β βββ hooks/ # Custom React hooks
β βββ services/ # API services
β βββ types/ # TypeScript type definitions
apps/remote*/
βββ src/
β βββ components/ # Components to be exposed
β βββ utils/ # Remote-specific utilities
β βββ types/ # TypeScript type definitions
-
Start development servers:
start-dev.bat
-
Make changes to your applications
- Host application changes in
apps/host
- Remote application changes in respective directories
- Host application changes in
-
Test your changes:
- Host: http://localhost:5000
- Remote1: http://localhost:5001
- Remote2: http://localhost:5002
-
Build for deployment:
build-prod.bat # For production # or build-staging.bat # For staging
- Keep components small and focused
- Use TypeScript for better type safety
- Implement proper prop validation
- Follow consistent naming conventions
- Use Redux for global state
- Keep Redux slices organized by feature
- Implement proper error handling
- Use TypeScript for type-safe actions and reducers
- Organize routes by feature
- Implement proper route guards
- Use lazy loading for better performance
- Handle 404 pages properly
- Keep utility functions pure
- Implement proper error handling
- Write unit tests for critical functions
- Document complex logic
-
Code Splitting
- Use lazy loading for routes
- Split vendor chunks appropriately
- Optimize bundle sizes
-
Caching
- Implement proper cache strategies
- Use service workers when appropriate
- Cache static assets effectively
-
Loading States
- Implement loading indicators
- Use skeleton screens
- Handle errors gracefully
-
Environment Variables
- Never commit sensitive data
- Use appropriate environment files
- Implement proper validation
-
Authentication
- Implement proper auth flows
- Use secure session management
- Handle token expiration
-
Data Handling
- Validate all inputs
- Sanitize outputs
- Implement proper error boundaries