Angular 10 升级 - 修复 CommonJS 或 AMD 依赖项可能导致优化救助

Ren*_*.N. 86 javascript angular-upgrade angular angular10

我正在尝试将我的 Angular 9 应用程序升级到 Angular 10 版本,但在升级后低于警告

WARNING in calendar.reducer.ts depends on lodash/keys. CommonJS or AMD dependencies can cause optimization bailouts.
Run Code Online (Sandbox Code Playgroud)

我已将以下行添加到我的angular.json文件中,但问题未解决

"allowedCommonJsDependencies": ["lodash"]
Run Code Online (Sandbox Code Playgroud)

我该如何解决上述问题。

JSO*_*ulo 143

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

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

一些库提供 ES 模块化构建。如果是lodash,您可以将其替换为lodash-es

运行npm install --save lodash-es

现在,从替代进口的所有lodashlodash-es

还要确保使用 ES 导入语句导入库:

import { keys } from 'lodash-es';
Run Code Online (Sandbox Code Playgroud)

白名单 CommonJS 依赖

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

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

Run Code Online (Sandbox Code Playgroud)

从 Angular CLI 版本 10.0.1 开始,您可以在allowedCommonJsDependencies. 这意味着如果你通过lodash,子路径(例如lodash/keys)也将被允许。

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

  • `不允许使用 CommonJsDependency 属性。` (7认同)
  • @Cleancode你把它放在正确的地方了吗?(`architect` - `build` - `options`) 更新了我的答案,现在应该更清楚把它放在哪里。 (3认同)
  • 尝试过但问题仍然存在 (2认同)
  • 是的,您需要安装“lodash-es”。我正在更新我的答案以使其更清楚 (2认同)
  • @Basheer Kharoti 确保将其放入 "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": {} (2认同)