首页 > 解决方案 > Angular 12 警告严重依赖:依赖的请求是一个表达式

问题描述

我在 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 配置中?谢谢。

标签: angularwebpackangular12angular-builder

解决方案


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

这来自已弃用的SystemJsNgModuleLoader中的代码,并且在使用 CLI builders时通常会被抑制,它也使用 ContextReplacementPlugin。我认为除了不压制它们并忽略警告之外,没有其他方法可以压制它们。

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'),
    {}
),

推荐阅读