can you give me source code example of ui module federation using webpack 5?
- User
can you give me source code example of ui module federation using webpack 5?
- Assistant
can you give me source code example of ui module federation using webpack 5?
Certainly! Webpack 5 introduced Module Federation, which allows for sharing of code (like UI components) across multiple applications at runtime. Below is a simplified example of how you can set up UI Module Federation using Webpack 5.
Let's create two applications: host and remote.
/my-apps
/host
/src
index.js
App.js
webpack.config.js
package.json
/remote
/src
index.js
Button.js
webpack.config.js
package.json
/package.json
package.json{
"name": "host",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "webpack serve"
},
"dependencies": {
"webpack": "^5.0.0",
"webpack-cli": "^4.0.0",
"webpack-dev-server": "^3.11.0",
"react": "^17.0.0",
"react-dom": "^17.0.0"
},
"devDependencies": {
"html-webpack-plugin": "^5.0.0",
"webpack": "^5.0.0"
}
}
webpack.config.jsconst path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: 'http://localhost:3000/',
},
devServer: {
port: 3000,
historyApiFallback: true,
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
}),
],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react']
}
}
}
]
},
resolve: {
extensions: ['.js', '.jsx'],
},
experiments: {
syncWebAssembly: true,
outputModule: true,
topLevelAwait: true,
},
optimization: {
runtimeChunk: 'single',
},
output: {
filename: '[name].js',
publicPath: 'auto',
},
};
src/index.jsimport React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
src/App.jsimport React from 'react';
const App = () => {
const loadRemoteButton = async () => {
const { default: Button } = await import('remote/Button');
return <Button />;
};
const [RemoteButton, setRemoteButton] = React.useState(null);
React.useEffect(() => {
loadRemoteButton().then(setRemoteButton);
}, []);
return (
<div>
<h1>Host App</h1>
{RemoteButton && <RemoteButton />}
</div>
);
};
export default App;
package.json{
"name": "remote",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "webpack serve"
},
"dependencies": {
"webpack": "^5.0.0",
"webpack-cli": "^4.0.0",
"webpack-dev-server": "^3.11.0",
"react": "^17.0.0",
"react-dom": "^17.0.0"
},
"devDependencies": {
"html-webpack-plugin": "^5.0.0",
"webpack": "^5.0.0"
}
}
webpack.config.jsconst path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'remoteEntry.js',
path: path.resolve(__dirname, 'dist'),
publicPath: 'http://localhost:3001/',
},
devServer: {
port: 3001,
historyApiFallback: true,
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
}),
],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react']
}
}
}
]
},
resolve: {
extensions: ['.js', '.jsx'],
},
experiments: {
syncWebAssembly: true,
outputModule: true,
topLevelAwait: true,
},
moduleFederation: {
name: "remote",
filename: "remoteEntry.js",
exposes: {
'./Button': './src/Button',
},
},
};
src/index.jsimport React from 'react';
import ReactDOM from 'react-dom';
const RemoteButton = () => <button>Remote Button</button>
ReactDOM.render(<RemoteButton />, document.getElementById('root'));
remote directory and run the following commands:npm install
npm start
This will start the remote application on http://localhost:3001.
host directory and run:npm install
npm start
This will start the host application on http://localhost:3000.
Now when you navigate to http://localhost:3000, you should see the host application rendering, and it should dynamically import and display the button from the remote application. Remember that you will need to have both applications running concurrently for everything to work.
You can enhance this implementation further with additional configurations or optimizations as per your requirements. Happy coding!
Not yet included in a published release.