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

南楼画角 提交于 2020-07-18 09:16:04

问题


I am trying to upgrade my angular 9 ap 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.


回答1:


The npm package lodash itself is not an ECMAScript module and therefore produces the warning. There are multiple ways to fix this:

Replace with ES modulized library (recommended)

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

Run npm install --save lodash-es.

Now replace all imports from lodash with lodash-es.

Also make sure to import the library with ES import statements:

import { keys } from 'lodash-es';

Whitelist CommonJS dependency

If there is no ES modulized build available for your library, or if you for some reason don't care, you can add can allow specific CommonJS dependencies in the angular.json file:

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

Since Angular CLI Version 10.0.1 you can use globs in allowedCommonJsDependencies. This means that if you pass lodash, the sub-paths (e.g. lodash/keys) will also be allowed.



来源:https://stackoverflow.com/questions/62589229/angular-10-upgrade-fix-commonjs-or-amd-dependencies-can-cause-optimization-bai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!