Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Remote Module Federation with Webpack and React
bronifty
bronifty

Posted on

     

Remote Module Federation with Webpack and React

Module Federation Demo

yarnyarn buildyarn start
Enter fullscreen modeExit fullscreen mode
  • to demo the fallback, switch to host app and run yarn start then open browser on localhost:8080 (it serves the nav app from build via linked library instead of remote federation)
cdpackages/home&& yarn start
Enter fullscreen modeExit fullscreen mode

Summary

  • create packages directory (could be named anything) and build 2 mf-apps inside, which will be run by wsrun (a yarn workspaces task runner)
    • follow the prompts to create your app (I'm using react)
mkdirpackagescdpackagesnpx create-mf-app--name hostnpx create-mf-app--name nav
Enter fullscreen modeExit fullscreen mode
  • create the component in the nav app that you want to share and default export it
  • next we will import the exported component with module federation by configuring webpack.config.js in both apps
/* nav */{constModuleFederationPlugin=require('webpack/lib/container/ModuleFederationPlugin');...plugins:[newModuleFederationPlugin({name:'nav',library:{type:'var',name:'nav'},filename:'remoteEntry.js',remotes:{},exposes:{'./Nav':'./src/Nav',},shared:require('./package.json').dependencies,}),newHtmlWebPackPlugin({template:'./src/index.html',}),],}/* host */{constModuleFederationPlugin=require('webpack/lib/container/ModuleFederationPlugin');...plugins:[newModuleFederationPlugin({name:'home',library:{type:'var',name:'home'},filename:'remoteEntry.js',remotes:{mfNav:'nav',},exposes:{},shared:require('./package.json').dependencies,}),newHtmlWebPackPlugin({template:'./src/index.html',}),],};
Enter fullscreen modeExit fullscreen mode
  • now you can refer to the remote module as 'mfNav/Nav' in your host app (we will lazy load it into suspense to handle the async import)
/* host/src/App.jsx */constNav=React.lazy(()=>import('mfNav/Nav'));constApp=()=>(<divclassName='container'><React.Suspensefallback={<div>Loading...</div>}><Nav/></React.Suspense><h2>HOME</h2></div>);ReactDOM.render(<App/>,document.getElementById('app'));
Enter fullscreen modeExit fullscreen mode

Ref

React Error Boundaries

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

web dev etc
  • Joined

More frombronifty

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp