question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

angular11-microfrontends-lazy-components production build not working

See original GitHub issue

Hi,

I’m facing an error when I try to load a remote component in the production build. For some reason, it works in development mode when I serve the application, but after I generate the build and try to run it from a local http server I get the following error in the console:

main.99097cdd2ad1485c22e3.js:1 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'ɵcmp' of undefined
TypeError: Cannot read property 'ɵcmp' of undefined

I have debugged and realised that the Angular NgModule that I’m remotely loading in the production build, has the ‘exports’ property set as an empty array. However in the code it’s exporting the component, and you can see it’s there in the development version. I don’t know if there’s something I should change in the webpack configuration or something, I’ve been trying out things for a few days and there’s no way I could get it to work…

Any help would be really appreciated.

Thank you

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nohannacommented, Nov 19, 2021

@noxborough If you are using AOT compilation, adding a static field will not change anything about emod.declarations, it will be still empty. It will simply add a new field (that will not be tree-shaked) to your module. So you have to retrieve your components from your static field not from emod.declarations.

0reactions
noxboroughcommented, Nov 19, 2021

@nohanna I saw your other recommendation to use exportAs as a way to reference your components by a “known” name after optimization. This does work, but for my use case I need to maintain the class names even after uglify/minify process.

So in my custom webpack.config.js I include this section (see below). The keep_classnames is the key there. But after I included this, I also had to set concatenateModules: false, because for some reason the class names ended up being doubled up (i.e. if class name was ClassAComponent, it would end up as ClassAComponentClassAComponent). Very weird, but found the solution here: https://stackoverflow.com/questions/49999999/duplicate-class-names-after-using-webpack-in-production-mode-with-uglifys-keep

optimization: {
        concatenateModules: false,
        minimizer: [
            (compiler) => {
                compiler.options.optimization.minimizer.filter(({ constructor: { name } }) => name === "TerserPlugin").forEach(
                    terser => {
                        terser.options.terserOptions.keep_classnames = true;
                        terser.options.terserOptions.keep_fnames = false;
                    }
                );
            },
        ],
    },
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unanswered 'webpack-module-federation' Questions - Page 2
I've a micro frontends running on monorepo using nx CLI where in the host application ... 'next build' not respecting publicPath property in...
Read more >
How to Build Micro Frontend Based on Angular - DZone
Your web application might have features developed by different teams and sometimes it's better to release only certain features into production ...
Read more >
How to Build a Micro Frontend with Webpack's Module ... - Bitovi
Step-by-step Implementation of Module Federation In Angular · Step 1: Project Setup · Step 2: Update angular.json File · Step 3: Split files...
Read more >
How to build a micro-frontends architecture, with Angular and ...
Micro -frontends extends the microservice idea to frontend development and help to scale medium to large organizations.
Read more >
Microfrontend with Angular and Webpack Module Federation
Furtunately, Angular 11 (or rather Webpack 5) finally comes up with ... lazy-loading and dynamic component generation (e.g. when creating a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found