diff --git a/content/blog/handling-react-remote-modules-with-babel.md b/content/blog/handling-react-remote-modules-with-babel.md new file mode 100644 index 0000000..f8bb0cc --- /dev/null +++ b/content/blog/handling-react-remote-modules-with-babel.md @@ -0,0 +1,267 @@ +--- +title: Handling react remote modules with babel (wip) +description: 'WIP' +date: '2021-03-12' +published: true +--- + +As my friend and colleague David is sharing on his blogpost series, we're using Microfrontends to enable different products to be created under the same codebase. + +This is an ongoing effort to give teams the capability to iterate fast, and to have independent release cycles while sharing code and maintaining the User Experience. + +David is doing a great job documenting this whole process and architecture, and thus I'll not repeat myself here. [add-david-mfe-link]() + +As part of this initiative, we created a shared library (which works as any other MFE) and only contains the components that are shared across products. All of this lives on a single repo, and we're using Webpack's Module Federation to define this clear interface between products. + +However, some challenges came out of this, and one of them is the one I'm sharing here today. +## The challenge + +We wanted to import components from a different MFE, and we wanted this to be _code-splitted_, meaning that it should only be loaded when it is needed. We'll refer to this components as **remote components** as they live in a different location. + +Webpack allows to do this by using the _dynamic import_ syntax (`import()`). Here's what we needed to do to load a component that belongs to the **shared** library. + +- Import it using the ES6 `import` function - `import('shared/components/Button')` +- Wraps it in a React.lazy `const Button = React.lazy(() => import('shared/components/Button'))` + +That would result in something like the following: + +```js +import React from "react"; + +const Button = React.lazy(() => import("shared/components/Button")); + +const HomePage = () => { + return