Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add step 4 exercises, code and slides #27

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/images/step-04-results1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/step-04-results2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/step-04-results3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
106 changes: 100 additions & 6 deletions slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ config.plugins.push(

<div class="dense">

## Create a Next.js app and inside of it:
## Create a Next.js app and inside of it:

-- Create `components/nextjs-layout-box.js` file.

Expand Down Expand Up @@ -517,7 +517,7 @@ You should see the `LayoutBox` component acting as a wrapper and the remote comp

<div class="dense">

In this step we are going to demonstrate Module Federation's bi-directional ability to share modules between multiple apps that can act both as the host and the remote at the same time by putting together what we learned in the previous two steps.
In this step we are going to demonstrate Module Federation's bi-directional ability to share modules between multiple apps that can act both as the host and the remote at the same time by putting together what we learned in the previous two steps.

</div>

Expand Down Expand Up @@ -623,7 +623,7 @@ new NextFederationPlugin({
remotes: {
remote: 'reactApp@http://localhost:8080/remoteEntry.js',
},
exposes: {
exposes: {
'./nextjs-layout-box': './components/nextjs-layout-box.js',
'./nextjs-table': './components/nextjs-table.js'
},
Expand Down Expand Up @@ -744,8 +744,8 @@ module.exports = {
filename: 'remoteEntry.js',
exposes: { './Button': './src/components/Button' },
shared: {
react: { singleton: true, requiredVersion: '18.2.0' },
'react-dom': { singleton: true, requiredVersion: '18.2.0' },
react: { singleton: true, requiredVersion: require('./package.json').dependencies.react },
'react-dom': { singleton: true, requiredVersion: require('./package.json').dependencies['react-dom'] },
},
}),
],
Expand All @@ -754,6 +754,100 @@ module.exports = {

---

# Step 4: Setting up Shared Modules Example
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we planning to write somewhere that currently MF does not support Next's app directory structure?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting, could you elaborate a little bit? is there a link I can take a look at?


<div class="dense">

In this step we are going to demonstrate sharing dependencies (React and React DOM) across federated modules.

</div>

---

# Step 4: Exercise

<div class="dense">

-- take the applications you created as part of step 3 exercises;

-- modify Webpack config of the React app to have React and React DOM as shared dependencies;

-- modify Next.js config of the Next.js app to have React and React DOM as shared dependencies;

-- set the versions of these shared dependencies to be read from respective `package.json` files;

-- make sure the dependencies are being shared as singletons;

-- try changing the version of the React and React DOM in the `package.json` file of the React app to a previous one and make sure you get a warning about version mismatch in the console.

</div>

---

# Step 4: Solution

```js
// next.config.js
// ...
new NextFederationPlugin({
// ...
shared: {
react: {
requiredVersion:
require('./package.json').dependencies.react,
singleton: true,
},
'react-dom': {
requiredVersion:
require('./package.json').dependencies['react-dom'],
singleton: true,
},
},
extraOptions: {
skipSharingNextInternals: true,
}
})
// ...
```

---

# Step 4: Solution /2

```js
// webpack.config.js
// ...
new ModuleFederationPlugin({
// ...
shared: {
react: {
requiredVersion:
require('./package.json').dependencies.react,
singleton: true,
},
'react-dom': {
requiredVersion:
require('./package.json').dependencies['react-dom'],
singleton: true,
},
},
}),
// ...
)}
```

---

# Step 4: Result

![Requests of the Next.js app](/images/step-04-results1.png "Requests of the Next.js app")

![Requests of the React app](/images/step-04-results2.png "Requests of the React app")

![Version mismatch warning](/images/step-04-results3.png "Version mismatch warning")

---

# Federated Types

<div class="dense">
Expand All @@ -770,7 +864,7 @@ When it comes to TypeScript applications, the most common problem with using ext
-- @module-federation/native-federation-typescript.

<p>
Something to keep in mind is that the host is becoming dependent on the remote types which means that each time remote changes its types, it can potentially break the host.
Something to keep in mind when using remote types in the host is that as a result the host can become dependent on them. This means that each time remote changes its types, it can potentially break the host.
</p>

</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Table = ({ data }) => {
</thead>
<tbody>
{data.map((d, i) => (
<tr key={d.i}>
<tr key={`${d}.${i}`}>
<td>{d.company}</td>
<td>{d.state}</td>
<td>{d.country}</td>
Expand Down
17 changes: 9 additions & 8 deletions src/step-03-bi-directional/nextApp/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const NextFederationPlugin = require('@module-federation/nextjs-mf/lib/NextFederationPlugin');
const NextFederationPlugin = require('@module-federation/nextjs-mf/lib/NextFederationPlugin')

module.exports = {
webpack(config, options) {
Expand All @@ -10,9 +10,10 @@ module.exports = {
remote: 'reactApp@http://localhost:8080/remoteEntry.js',
},
filename: 'static/chunks/remoteEntry.js',
exposes: {
'./nextjs-layout-box': './components/nextjs-layout-box.js',
'./nextjs-table': './components/nextjs-table.js'
exposes: {
'./nextjs-layout-box':
'./components/nextjs-layout-box.js',
'./nextjs-table': './components/nextjs-table.js',
},
shared: {
react: {
Expand All @@ -23,11 +24,11 @@ module.exports = {
extraOptions: {
skipSharingNextInternals: true,
},
}),
);
})
)
}
return config;
return config
},
// your original next.config.js export
reactStrictMode: true,
};
}
7 changes: 2 additions & 5 deletions src/step-03-bi-directional/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"name": "bi-directional",
"private": true,
"workspaces": [
"./reactApp",
"./nextApp"
],
"workspaces": ["./reactApp", "./nextApp"],
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "concurrently \"cd reactApp; npm start\" \"cd nextApp; npm run dev\""
"start": "concurrently \"cd reactApp; npm start\" \"cd nextApp; npm run dev\" --kill-others"
}
}
1 change: 0 additions & 1 deletion src/step-03-bi-directional/reactApp/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
</head>
<body>
<div id="root"></div>
<script src="main.js"></script>
</body>
</html>
30 changes: 10 additions & 20 deletions src/step-03-bi-directional/reactApp/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ModuleFederationPlugin = require('webpack').container.ModuleFederationPlugin;
const deps = require('./package.json').dependencies;
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ModuleFederationPlugin =
require('webpack').container.ModuleFederationPlugin
const deps = require('./package.json').dependencies
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this import used?

module.exports = {
entry: './src/index',
entry: {
Expand All @@ -9,22 +10,17 @@ module.exports = {
},
},
cache: false,

mode: 'development',
devtool: 'source-map',

optimization: {
minimize: false,
},

output: {
publicPath: 'auto',
},

resolve: {
extensions: ['.jsx', '.js', '.json', '.mjs'],
},

module: {
rules: [
{
Expand All @@ -42,31 +38,25 @@ module.exports = {
presets: [require.resolve('@babel/preset-react')],
},
},
{ test: /\.css$/, use: ["style-loader", "css-loader"] },
{ test: /\.css$/, use: ['style-loader', 'css-loader'] },
],
},

plugins: [
new ModuleFederationPlugin({
name: 'reactApp',
filename: 'remoteEntry.js',
remotes: {
remote: 'nextApp@http://localhost:8081/_next/static/chunks/remoteEntry.js',
remote:
'nextApp@http://localhost:8081/_next/static/chunks/remoteEntry.js',
},
exposes: {
'./Nav': './src/components/Nav',
'./Title': './src/components/Title'
},
shared: {
react: {
// Notice shared are NOT eager here.
requiredVersion: false,
singleton: true,
},
'./Title': './src/components/Title',
},
shared: {},
}),
new HtmlWebpackPlugin({
template: './public/index.html',
}),
],
};
}
Loading