Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading to Angular 10 - Fix CommonJS or AMD dependencies can cause optimization bailouts

I am trying to upgrade my Angular 9 app to Angular 10 version, but I am getting the below warning after the upgrade

rxjs\BehaviorSubject.js depends on rxjs-compat/BehaviorSubject

How can I fix this?

like image 743
Ora Avatar asked Jun 26 '20 10:06

Ora


People also ask

What is CommonJS angular?

CommonJS is a specification which helps to load modules from one file to other file. Two JavaScript developers will follow CommonJS specifications in order make their module communicate with each other. In order to make it work practically, one file gets exposed while the other file will be using that exposed file.

What are CommonJS modules?

Getting Started. From a structure perspective, a CommonJS module is a reusable piece of JavaScript that exports specific objects made available to any dependent code. Unlike AMD, there are typically no function wrappers around such modules (so we won't see define here, for example).


4 Answers

When you use a dependency that is packaged with CommonJS, it can result in larger slower applications

Starting with version 10, Angular now warns you when your build pulls in one of these bundles. If you’ve started seeing these warnings for your dependencies, let your dependency know that you’d prefer an ECMAScript module (ESM) bundle.

Here is an official documentation - Configuring CommonJS dependencies

In your angular.json file look for the build object and add

allowedCommonJsDependencies

as shown below -

"build": {
  "builder": "@angular-devkit/build-angular:browser",
  "options": {
     "allowedCommonJsDependencies": [
        "rxjs-compat",
         ... few more commonjs dependencies ... 
     ]
     ...
   }
   ...
},
like image 163
Gunjan Khanwilkar Avatar answered Oct 17 '22 08:10

Gunjan Khanwilkar


Try replacing the rxjs imports rxjs/internal/operators with rxjs/operators.

Example:

import { catchError, retry } from 'rxjs/internal/operators';

with

import { catchError, retry } from 'rxjs/operators';
like image 39
Janardhan Burle Avatar answered Oct 17 '22 08:10

Janardhan Burle


It is recommended that you avoid depending on CommonJS modules in your Angular applications. Depending on the CommonJS modules, they can prevent bundlers and minifiers from optimizing your application, which results in larger bundle sizes. Instead, it is recommended that you use ECMAScript modules in your entire application.

Still, if you don't care about your bundling size, to disable these warnings, you can add the CommonJS module name to allowedCommonJsDependencies option in the build options located in the angular.json file.

"build": {
  "builder": "@angular-devkit/build-angular:browser",
  "options": {
     "allowedCommonJsDependencies": [
        "rxjs-compat"
     ]
     ...
   }
   ...
},

Source

like image 42
Arun Kumar Avatar answered Oct 17 '22 06:10

Arun Kumar


For the RXJS library you can do the following changes.

For imports, such as 'rxjs/internal/<anything>' and 'rxjs/index', replace it with just 'rxjs'.

For imports such as 'rxjs/internal/operators', replace it with 'rxjs/operators', which is mentioned in Janardhan Burle's answer.

Or replace just rxjs.

like image 38
ymssa___ Avatar answered Oct 17 '22 06:10

ymssa___