Webpack Module Federation: React

๐Ÿ’ก Webpack Module Federation: React โ€” Executive Summary

Technical architectural analysis, practical configuration manifests, and deployment instructions for Webpack Module Federation: React.

Common
Enable dynamic loading: bootstrap.tsx
ts
import React from 'react';
import { createRoot } from 'react-dom/client';
import App from './App';

const container = document.getElementById('root');

if (container) {
  const root = createRoot(container);
  root.render(
    <React.StrictMode>
      <App />
    </React.StrictMode>,
  );
}
Enable dynamic loading: index.ts
ts
import('./bootstrap');
Producer (Provider)
App.tsx
ts
import LocalButton from './Button';

const App = () => (
  <div>
    <h1>Basic Host-Remote</h1>
    <h2>Remote</h2>
    <LocalButton />
  </div>
);

export default App;
Component: Button.tsx
ts
export default function Button() {
  return <div>Provider button</div>;
}
Consumer
App.css
css
body {
  margin: 0;
  color: #fff;
  font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
  background-image: linear-gradient(to bottom, #020917, #101725);
}

.content {
  display: flex;
  min-height: 100vh;
  line-height: 1.1;
  text-align: center;
  flex-direction: column;
  justify-content: center;
}

.content h1 {
  font-size: 3.6rem;
  font-weight: 700;
}

.content p {
  font-size: 1.2rem;
  font-weight: 400;
  opacity: 0.5;
}
App.tsx
ts
import './App.css';
// The remote component provided by federation_provider
import ProviderButton from 'federation_provider/button';

const App = () => {
  return (
    <div className="content">
      <h1>Rsbuild with React</h1>
      <p>Start building amazing things with Rsbuild.</p>
      <div>
        <ProviderButton />
      </div>
    </div>
  );
};

export default App;

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *