You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the above code block, we have prefixed both the manifest and remoteEntry.js exported by Module Federation with `static`. This is because Modern.js requires all resources that need to be exposed to be placed in the `static/` directory, and Modern.js's server will only host the `static/` directory in production environments.
73
+
:::
74
+
68
75
Additionally, modify `modern.config.ts` to provide a development environment port for the producer, allowing the consumer to access the producer's resources through this port:
69
76
70
77
```ts title="modern.config.ts"
@@ -99,7 +106,7 @@ import { createModuleFederationConfig } from '@module-federation/modern-js';
@@ -173,8 +180,45 @@ After modifying the producer's code, the consumer will automatically fetch the p
173
180
174
181
Access `http://localhost:8080/remote`, and you will see that the page includes the `Button` component from the producer's remote module.
175
182
183
+
We can also execute `modern serve` locally to simulate the production environment.
184
+
185
+
Because the Module Federation plugin will automatically read Modern.js's `output.assetPrefix` configuration as the access address for remote modules, and this value defaults to `/` after building in the production environment.
186
+
187
+
If we want to simulate the production environment in local, but not configure `output.assetPrefix`, consumers will pull the entry file of the remote module from their own domain. So We can add the following configuration:
// Now this configuration is only used in the local when you run modern serve command.
205
+
// If you want to deploy the application to the platform, use your own domain name.
206
+
// Module federation will automatically write it to mf-manifest.json, which influences consumer to fetch remoteEntry.js.
207
+
assetPrefix: isLocal?'http://127.0.0.1:3051':'/',
208
+
},
209
+
plugins: [
210
+
appTools({
211
+
bundler: 'rspack', // Set to 'webpack' to enable webpack
212
+
}),
213
+
moduleFederationPlugin(),
214
+
],
215
+
});
216
+
```
217
+
218
+
Now, in the producer, run `IS_LOCAL=true modern build && modern serve`, and in the consumer, run `modern build && modern serve` to simulate the production environment locally and access the remote modules.
219
+
176
220
You can refer to this example: [Modern.js & Module Federation Basic Example](https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/module-federation/base).
177
221
178
222
## Related Documentation
179
223
180
-
-[Module Federation Official Documentation](https://module-federation.io/zh/guide/framework/modernjs.html)
224
+
-[Module Federation Official Documentation](https://module-federation.io/zh/guide/framework/modernjs.html)
0 commit comments