Setup project if you don't have a pre-existing app
Mother App Setup
Do create projects
create-react-app my-org-mother create-react-app my-org-child1Eject the configuration file
cd my-org-mother npm run ejectSetup your index file located in
public/index.htmlwith SystemJS. This template uses the latest react. To make your project work with the existing repo always use the SystemJS module generated by https://www.jsdelivr.com/package/npm/@esm-bundle/react. Always have the same version of React and ReactDOM for all repos associated with this importmap.prepare
config/webpack.config.jsto disable chunksset
libraryTargetassysteminside output object inconfig/webpack.config.js.Install systemJS plugin for webpack
npm i -S systemjs-webpack-interop npm iWrap the return output object with
systemjsInterop.modifyWebpackConfig({ ...webpackConfig })and saveconfig/webpack.config.jsRun the app
npm startCheck the
static/js/bundle.jsin network it must begin with following:Now use the external of React and ReactDOM for shared libraries and amend
static/js/bundle.jswith externals belowbailproperty:Restart with
npm start.
Observe that your React and ReactDOM packages are rendered via importmap of SystemJS. This completes the setup of your mother app to start accepting external packages.
Child App Setup
Go to the child1 app or your existing segregated child app and eject the config:
cd .. cd my-org-child1 npm run ejectRepeat steps 4 to 7 above to build a SystemJS module out of your child1 app.
Now add the externals in
src/webpack.config.jssame as above.Change the port of app by creating
.envfile and the followingPORT=3001Allow Cross Domain access on Webpack Dev Server by modifying
config/webpackDevServer.config.jswith the following:Modify
src/index.jsin my-org-child1 to export your component.Add the indication of child1 Micro Frontend App with
src/App.js:Start the app
npm startYour App now must run at port 3001. Notice that your app won't run as the SystemJS file is not present. That is fine. We will use the bundle in the mother app. Just make sure your
static/js/bundle.jsstarts with the following:
Combine child1 app with your mother app
With the setup made above, you will be able to use the import statement.
Move inside the mother app:
cd .. cd my-org-motherUpdate your in
src/index.jsimportmap with your micro frontend path:Update the externals in
config/webpack.config.jswith you app:Add a ignore rule in
.eslintrcorpackage.jsonInstall loadable to do a render wih fallback
npm install @loadable/componentModify you mother app
src/App.jsto import and run you child1 app:Run both development server with
npm start
Your Micro Frontend setup is complete with SystemJS. If you would like to access the mother app components to your child1, export component in src/index.js, include your mother app in importmap inside mother app public/index.html and external for your child1.
No comments:
Post a Comment