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

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

WARNING in calendar.reducer.ts depends on lodash/keys. CommonJS or AMD dependencies can cause optimization bailouts.

I have added below line to my angular.json file but issue is not resolved

"allowedCommonJsDependencies": ["lodash"]

How can I fix above issue.

69413 次浏览

Npm 包 lodash本身不是 ECMAScript 模块,因此会产生警告。 有多种方法可以解决这个问题:

替换为 ES 模块化库(推荐)

Some libraries offer ES modulized builds. In case of lodash, you can replace it with 懒散.

运行 npm install --save lodash-es

现在用 lodash-es替换从 lodash的所有导入。

还要确保导入带有 ES 导入声明的图书馆:

import { keys } from 'lodash-es';

白名单 CommonJS 依赖关系

如果您的库中没有可用的 ES 模块化构建,或者由于某种原因您不关心,您可以在 angular.json文件中允许特定的 CommonJS 依赖关系:

"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"allowedCommonJsDependencies": ["lodash"]
}
}
}


自从 Angular CLI Version 10.0.1以来,您可以在 allowedCommonJsDependencies中使用 globs。 这意味着如果您传递 lodash,子路径(例如 lodash/keys)也将被允许。

参考文献: https://angular.io/guide/build#configuring-commonjs-dependencies