对“hammerjs”的依赖。CommonJS 或 AMD 依赖项可能会导致优化救援

J.F*_*.F. 7 hammer.js angular

我正在使用 Angular CLI 11.0.2 开发一个角度应用程序,并在应用程序中使用HammerJS来处理滑动事件。

当我编译应用程序时,出现以下警告消息:

Warning: F:\Programs\GoodRiddles\git\angular\src\app\config\hammerjs.config.ts depends on 'hammerjs'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
Run Code Online (Sandbox Code Playgroud)

根据我在这里的理解,我应该用 ECMA 版本的库替换 HammerJS。

我的问题是: 是否有正确的方法(而不是简单地隐藏消息)来修复它?我需要使用另一个库吗?

如果它可以帮助,这就是我使用它的方式:

import { Injectable } from '@angular/core';
import { HammerGestureConfig } from "@angular/platform-browser";
import * as hammer from "hammerjs";

@Injectable()
export class HammerConfig extends HammerGestureConfig {
    overrides = <any>{
        swipe: { direction: hammer.DIRECTION_HORIZONTAL },
        pinch: { enable: false },
        rotate: { enable: false }
    };
}
Run Code Online (Sandbox Code Playgroud)

小智 7

将 Hammerjs 添加到 angular.json 中的 allowedCommonJsDependencies 中

"architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "allowedCommonJsDependencies": [
          "hammerjs" <== here
        ],...
Run Code Online (Sandbox Code Playgroud)

  • 谢谢塔里克的回答。但这正是我不想要的答案^^。这只会移动允许的 CommonJS 依赖项中的 Hammerjs。我正在寻找一种替代方案,它首先不会创建此 CommonJs 依赖项。 (3认同)