Closed paynoattn closed 1 year ago
@paynoattn
Have you tried with type: script
in the route config for remote?
import { WebComponentWrapper, WebComponentWrapperOptions } from '@angular-architects/module-federation-tools';
export const APP_ROUTES: Routes = [
[...]
{
path: 'react',
component: WebComponentWrapper,
data: {
type: 'script',
remoteEntry: 'https://witty-wave-0a695f710.azurestaticapps.net/remoteEntry.js',
remoteName: 'react',
exposedModule: './web-components',
elementName: 'react-element'
} as WebComponentWrapperOptions
},
[...]
]
If this doesn't work, is there any way you could provide a sample repo that reproduces this?
Hello, I added the type: script
to the app and am still experiencing the issue.
I'll create an example repo if there is no other suggestions.
I created a repo with the just the basic HTML / components of my two apps here.
I apologize if there is any confusion about the directory structure as i'm trying to smash two monorepos together. Right now for business reasons, I cannot migrate the angular app into the nx workspace.
@paynoattn, I tried to find the root cause of this issue and it seems like the issue is coming from @nrwl/webpack which is creating esm build (due to scriptType: module) for the remoteEntry.js.
Here is my test code which i used to debug this issue: https://github.com/SandeepThomas/angular-react-mfe
I had tested with both nx-react and also normal react app build using create-react-app/craco. The nx-react is generating esm for the remoteEntry.js while the craco one creates normal script file which works fine.
related issue: https://github.com/nrwl/nx/issues/13628
Update: nx 15.4.6 added support to modify webpack config for react apps, but is having an open issue when creating react apps https://github.com/nrwl/nx/issues/14344.
For angular 13 and higher, type should be ‘module’. Even so, I’m still getting stick on InitRemote(), where it calls container.init()...saying “container.init() is not a function”.
Based on @SandeepThomas I tried upgrading to nx 15.4.6, which did not work, I got the same error.
Based on @keithcarter5's recommendation, I tried setting library: { type: module }
in the options for ModuleFederationPlugin
(as well asexperiment: { outputModule: true }
in the base webpack config), and am now getting an error: SyntaxError: export declarations may only appear at top level of a module
@paynoattn
With the latest nx update (15.6.3) i was able to override the webpack config for react remoteEntry file to be normal script instead of module.
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
const { composePlugins, withNx } = require('@nrwl/webpack');
const { withReact } = require('@nrwl/react');
const { withModuleFederation } = require('@nrwl/react/module-federation');
const baseConfig = require('./module-federation.config');
const config = {
...baseConfig,
};
const changeModuleFederationToScript = () => {
return (config, ctx) => {
for (let plugin of config.plugins) {
if (plugin instanceof ModuleFederationPlugin) {
plugin._options.library = {
type: 'var',
name: plugin._options.name,
};
}
}
config.output.scriptType = 'text/javascript';
return config;
};
};
// Nx plugins for webpack to build config object from Nx options and context.
module.exports = composePlugins(
withNx(),
withReact(),
withModuleFederation(config),
changeModuleFederationToScript()
);
You can checkout my github repo to see the full configuration.
Thanks to @bryantcj52 for the idea (https://github.com/nrwl/nx/issues/10110#issuecomment-1256336849)
Thanks @SandeepThomas this was trully a huge help. I was able to get this working as welll.
Another bug I found related to this. You still get the container is undefined bug
once you do a nx build --prod. You have to turn optimization to false
in the project.json of your production build configuration.
Hello,
I am trying to dynamically render a react microfrontend into my angular host app following this guide - https://www.angulararchitects.io/aktuelles/multi-framework-and-version-micro-frontends-with-module-federation-your-4-steps-guide/
When I actually load the hosted microfrontend in the article, it works. When I try to load the app using
React.lazy
and stating the remote explicitly in the remotes section of my host application, it also works. When I try dynamically load my react app, I get several errors and the app doesnt load:Uncaught SyntaxError: import.meta may only appear in a module styles.js:8202:28
Here is my host webpack.config.js
My guest webpack.config.js:
And my custom-elements.tsx
Package.json of host angular app:
Package.json of guest React app:
Based on other issues posted here, I have tried the following things:
scriptType: 'text/javascript',
to outputpublicPath
fromauto
to the absolute url