After successfully upgrading my application from Angular 9 to Angular 10, I encountered some warnings when running the ng serve
command.
WARNING in src\app\auth\guard\auth.guard.ts depends on 'lodash'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
WARNING in src\app\shared\services\api.service.ts depends on 'rxjs/Observable'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
WARNING in src\app\auth\guard\auth.guard.ts depends on 'rxjs/observable/fromPromise'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
WARNING in src\app\shared\services\localforage.service.ts depends on 'rxjs/observable/forkJoin'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
WARNING in src\app\auth\guard\auth.guard.ts depends on 'rxjs/add/operator/map'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
Suggestions have been made to whitelist commonJS as a workaround, but this may increase bundle size. How can I resolve the common JS issue while still maintaining an optimized bundle size?
Note: The final code includes the map
operator
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { switchMap, tap, map } from 'rxjs/operators';
import { Observable, from} from 'rxjs';
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.getToken().map((token: any) => {
if ((token !==null) && !this.jwtHelper.isTokenExpired(token.data)) {
// this.proactiveTokenRefresh(token);
return true;
}
// Store the attempted URL for redirecting
this.localForage.setItem('redirectUrl', state.url).then(() => {
// Navigate to the login page
this.router.navigate(['/login']);
return false;
});
});
}
private getToken(): Observable<{}> {
const token = this.localForage.getItem('id_token');
return from(token);
}`