代码之家  ›  专栏  ›  技术社区  ›  chuckd

不确定如何将ts文件添加到Angular中的allowedCommonJsDependencies以抑制警告

  •  0
  • chuckd  · 技术社区  · 3 年前

    我在angular.json JS依赖部分添加了几个模块,如下所示:

    "allowedCommonJsDependencies": ["angular2-wizard", "hammerjs", "moment-timezone"]
    

    但我刚刚升级了所有的东西,我又收到了一个关于我创建的管道的警告:

    [webpack-dev-server] WARNING
    /Users/mysite/client/src/app/pipes/time-zone.pipe.ts depends on 'moment'. CommonJS or AMD dependencies can cause optimization bailouts.
    

    我尝试将.ts文件名添加到下面的列表中,但仍然收到警告。

    "allowedCommonJsDependencies": ["angular2-wizard", "hammerjs", "moment-timezone", "time-zone"]
    

    我还尝试过:“timezone.pipe.ts”和“TimeZonePipe”,这是类的名称,但似乎都没有删除警告。

    我该怎么解决这个问题?

    import {
      Pipe,
      PipeTransform
    } from '@angular/core';
    import * as moment from 'moment';
    
    @Pipe({
      name: 'timeZone'
    })
    export class TimeZonePipe implements PipeTransform {
    
      transform(dateUtc: Date, timeZone: string, momentFormat: string, convertJustTimeZone = false): string {
        const tempDateUtc = moment(dateUtc).utc(true);
        tempDateUtc.tz(timeZone, convertJustTimeZone);
        return tempDateUtc.format(momentFormat);
      }
    
    }
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   kemsky    3 年前

    如前所述 momentjs 是一个CommonJS模块,树抖动在CommonJS模块上不能很好地工作,这就是为什么您收到警告的原因。请参阅Angular文档中的官方链接 How CommonJS is making your bundles larger

    因此,您只有两个选项:忽略此警告并继续使用 momentjs 希望将来他们能转到es6模块,或者转到另一个类似的库 luxon

    推荐文章