angular 生产应用中未导入 NPM 包

NPM package is not imported in angular production app

目标

我正在尝试将 rxjs 的 typescript 模块扩充作为 npm 包提供,以便在 Angular 项目中使用。

问题

在 Angular 应用程序中使用包在本地开发模式下工作正常,但是一旦为生产构建应用程序,包就不会在最终分发中导入。

详情

很难判断问题是否来自 typescript、angular-cli、webpack 甚至 ng-packagr 无法识别扩充并将其正确导入最终 dist。

非常感谢任何帮助!
谢谢。

您是在尝试 ng serve --prod 还是 ng build --prod?

自己找到了答案,但并非没有困难..

快答

将以下 属性 添加到 npm 包中:

package.json {
    sideEffects: true
} 

说明

该问题与 ng-packagr 有关,它在 [ 的 dist 版本中设置了 sideEffects:false =37=] 文件.

这是 Angular Package Format v6 推荐的设置以优化分发包:

"Packages that contain this property set to false will be processed by webpack more aggressively than those that don't. The end result of these optimizations should be smaller bundle size and better code distribution in bundle chunks after code-splitting" [source]

设置为 false 时,只会捆绑真正使用过的代码部分。然而,在我的例子中,扩充被识别为 "declared but never read" 因此在构建过程中被忽略。将 sideEffects 设置为 true webpack 将毫无疑问地简单地捆绑整个包。