【问题标题】:Angular 12 warning Critical dependency: the request of a dependency is an expressionAngular 12 警告严重依赖:依赖的请求是一个表达式
【发布时间】:2021-10-12 08:19:33
【问题描述】:

我在 ng build (Angular 12) 后收到这些警告:

./node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:35289:13-34 - Warning: Critical dependency: the request of a dependency is an expression

./node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:35301:13-100 - Warning: Critical dependency: the request of a dependency is an expression

我正在使用 @angular-builders/custom-webpack 从初始包中提取 moment.js。

如果我禁用了@angular-builders/custom-webpack,那么一切顺利,警告就会消失。

此外,如果我在 custom-webpack.config.js 中添加以下内容,警告就会消失:

new webpack.ContextReplacementPlugin(
    /\@angular(\\|\/)core(\\|\/)__ivy_ngcc__(\\|\/)fesm2015/,
    path.join(__dirname, './src'),
    {}
),

那么好心,究竟是什么导致了这些警告?是否有任何其他解决方案来处理它而不像上面那样将插件添加到 webpack 配置中?谢谢。

【问题讨论】:

    标签: angular webpack angular12 angular-builder


    【解决方案1】:

    我从 Angular 开发团队得到以下反馈:

    这来自已弃用的 SystemJsNgModuleLoader 中的代码,并且是 通常在using the CLI builders 时被抑制,这也使用 上下文替换插件。我认为没有其他选择 压制他们,除了不压制他们并忽略 警告。

    https://github.com/angular/angular/issues/43092#issuecomment-895848535

    所以我最终在 custom-webpack.config.js 中使用了上述 CLI 构建器代码,如下所示:

    // Always replace the context for the System.import in angular/core to prevent warnings.
    new ContextReplacementPlugin(
        /\@angular(\\|\/)core(\\|\/)/,
        path.join(__dirname, '$_lazy_route_resources'),
        {}
    ),
    

    【讨论】:

    【解决方案2】:

    在 Angular 12 中使用自定义 ContextReplacementPlugin 时,take care about this:

    如果您在自定义配置中指定的插件已经 由 Angular CLI 添加,那么默认情况下这两个实例将被合并。

    我使用 ContextReplacementPlugin 到exclude moment.js locales。升级到 Angular 12 后,所有语言环境都被删除,因为它的配置已与 $_lazy_route_resources 合并。我通过将replaceDuplicatePlugins 设置为true 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-13
      • 2016-08-28
      • 2017-08-11
      • 2020-06-15
      • 1970-01-01
      • 2022-11-10
      • 2020-10-11
      • 2022-08-15
      相关资源
      最近更新 更多