A Monorepo is a single repository containing multiple distinct projects, with well-defined relationships.
Monorepo is more than code and tools. A monorepo changes your organization and the way you think about code. By adding consistency, lowering the friction in creating new projects and performing large scale refactoring, by facilitating code sharing and cross-team collaboration, it'll allow your organization to work more efficiently.
Monorepos have a lot of advantages, but to make them work you need to have the right tools. Monorepo tool Nx by Nrwl (Narwhal Extension) is the most preferred next generation build system with first class monorepo support and powerful integrations.
Nx is an extension for the Angular CLI implementing monorepo-style development. It is an open source toolkit created by Ex googlers at company called Narwhal for developing enterprise or large scale applications.
Microfrontends is a front-end web development pattern in which a single application may be built from disparate builds. It is analogous to a microservices approach but for client-side single-page applications written in JavaScript. It is a solution to de-composition and routing for multiple front-end applications.
Module Federation, a powerful feature introduced in Webpack 5, allows developers to load separate bundles of code at runtime, making it an ideal choice for implementing micro frontend architecture. This article will guide you through the process of creating a React micro frontend application using Module Federation, with a focus on providing clear code examples.
// App1 webpack.config.js
module.exports = {
...
plugins: [
new ModuleFederationPlugin({
name: “app1”,
remotes: {
app2: “app2@http://localhost:3002/remoteEntry.js”,
},
shared: {
react: { singleton: true },
"react-dom": { singleton: true },
},
}),
...
],
// App2 webpack.config.js
module.exports = {
...
plugins: [
new ModuleFederationPlugin({
name: “app2”,
filename: "remoteEntry.js",
exposes: {
"./Button": "./src/Button",
},
shared: {
react: { singleton: true },
"react-dom": { singleton: true },
},
}),
...
],
...
// app1
const RemoteButton = React.lazy(() => import("app2/Button"));
const App = () => (
);
// app2
const Button = () => ;
export default Button;
// app1 & app2 webpack.config.js
new ModuleFederationPlugin({
...
shared: [
"react",
"react-dom",
{
"@shared-context/shared-library": {
import: "@shared-context/shared-library",
requiredVersion:
require("../shared-library/package.json").version,
},
},
],
}),
...
})
// app1
import { NameContext } from "@shared-context/shared-library";
const Welcome = React.lazy(() => import("app2/Welcome"));
const App = () => (
);
// app2
const Welcome = () => {
const name = React.useContext(NameContext);
return
Welcome (from app 2), {name}
;
};
// app1
const RemoteApp = React.lazy(() => import('app2/RemoteApp'));
const App = () => (
);
both apps use eager: true - the modules are not loaded async
Alin Bhattacharyya is a “Full Stack” Enterprise Architect heading the Frontend practice at Coforge, with over 20 years of experience in Software Engineering, Web and Mobile Application development, Product development, Architecture Design, Media Analysis and Technology Management. His vast experience in designing solutions, client interactions, onsite-offshore model management, and research and development of POC’s and new technologies allow him to have a well-rounded perspective of the industry.