Created by: raspy8766
Module Federation Support
Description
This PR aims to support the Webpack 5 module federation plugin natively in CRA. Micro-frontends are becoming a more prevalent form of UI development for the enterprise environment. We love what create-react-app provides, but in order to get it working at the moment, we rely on these forked changes. Hopefully this PR has what you're looking for, if not I'm happy to make additional accommodations and open to suggestions.
Proposed Changes
- Add a new optional
setupModuleFederation.jsfile that manages the webpack Module Federation plugin - When the
setupModuleFederation.jsfile is present, use the Module Federation plugin - Since Micro-frontend apps do not typically have their own index.html (unless they're using iframes), the index.html file along with the
publicfolder are now optional.
Testing
Setup
- Execute the following bash script which performs the following:
- Initializes a new CRA mfe app repo
- Clones down and installs the forked repo
- Installs local
react-scriptsfrom forked repo
yarn create react-app mfe-app && \
git clone https://github.com/HewlettPackard/create-react-app.git && \
yarn --cwd="./create-react-app" && \
yarn --cwd="./mfe-app" add file:../create-react-app/packages/react-scripts
Negative Test
- In the
mfe-apprepo, runyarn start - Navigate to
http://localhost:3000/webpack-dev-serverand note the files listed appear as normal
Positive Test
- Create a
src/setupModuleFederation.jsfile with the following contents:
module.exports = {
name: 'test',
library: { type: 'var', name: 'test' },
filename: 'remoteEntry.js',
exposes: { '.': './src/App.js' }
}
- Navigate to the
mfe-apprepo - Delete the
public/index.htmlfile (optional) - Run
yarn start - Navigate to
http://localhost:3000/webpack-dev-serverand notice theremoteEntry.jsfile
Screenshots
Webpack served assets:
remoteEntry.js file: