库构建失败并显示 Angular 9

Library build fails with Angular 9

我正在尝试将此库 https://github.com/flauc/angular2-notifications 从 Angular 2+ 迁移到 Angular 9.

最初的错误是关于 ModuleWithProviders 已成为通用类型,所以我修复了它。我也遇到了此处描述的错误 https://github.com/angular/angular/issues/32352,我用 require('@angular/compiler-cli'); 修复了它,现在我遇到了另一个错误:

../node_modules/@angular/common/common.d.ts:115:22 - error NG6002: Appears in the NgModule.imports of SimpleNotificationsModule, but could not be resolved to an NgModule class

我很难理解发生了什么,因为我以前从未构建过库,并且使用 gulp 构建似乎有点老套,因为这一行 ngc = require('@angular/compiler-cli/src/main').main 指的是不属于 public API.

的函数

编辑:

根据评论中的想法(和我自己的感觉),我尝试在没有 gulp:

的情况下进行构建

但我仍然有同样的错误...

我的尝试:https://github.com/youkouleley/angular2-notifications 我尝试用 ng build 构建它,package.json 中的脚本尚未更新

编辑 2

现在 Angular9 发布了:

https://angular.io/guide/creating-libraries#publishing-your-library

It is not recommended to publish Ivy libraries to NPM repositories. Before publishing a library to NPM, build it using the --prod flag which will use the older compiler and runtime known as View Engine instead of Ivy.


编辑

https://next.angular.io/guide/ivy

If you are a library author, you should keep using the View Engine compiler as of version 9. By having all libraries continue to use View Engine, you will maintain compatibility with default v9 applications that use Ivy, as well as with applications that have opted to continue using View Engine.

See the Creating Libraries guide for more on how to compile or bundle your Angular library. When you use the tools integrated into the Angular CLI or ng-packagr, your library will always be built the right way automatically.


我通过对您发布的尝试的回购协议进行了一些更改,设法使它正常工作。 截图如下:

所做的更改如下:

package.json 中的更改无关紧要。

enableIvy:false 意味着您的库将不会使用 Ivy 渲染引擎构建(目前推荐用于库),但不会阻止您的库在启用 Ivy 的应用程序中使用。

值得注意的是,IVY 仍处于某种试验模式

annotateClosureCompiler: false:与此问题有关https://github.com/angular/angular/pull/26738

我知道出了什么问题,我有一个 Angular 8 项目(包含一个库项目),我已经升级到 Angular 9(还有一个库:ng generate library core,我首先创建了一个新的 angular 9 项目来比较与我用 ng 8 制作的项目的差异,我发现缺少一些东西,这是我遵循的步骤: - 删除 node_modules - 删除 dist 文件夹 - 在 Angular 9 项目库中有一个名为:tsconfig.lib.prod.json

的新文件
{
  "extends": "./tsconfig.lib.json",
  "angularCompilerOptions": {
    "enableIvy": false
  }
}
  • 在您的 angular.json 中添加以下内容:
{
  ...
  "projects": {
     ...
     "configurations": {
            "production": {
              "tsConfig": "projects/core/tsconfig.lib.prod.json"
             }
      }
  ...
  }
}
  • 运行 ng build --prod,一切正常 :D

PS:在这种情况下,库称为 "core"