Library build fails with Angular 9

て烟熏妆下的殇ゞ 提交于 2020-04-13 06:34:13

问题


I am trying to migrate this library https://github.com/flauc/angular2-notifications from Angular 2+ to Angular 9.

The original error was about the ModuleWithProviders that has become a generic type, so I fixed it. I also had an error described here https://github.com/angular/angular/issues/32352 which i fixed with require('@angular/compiler-cli'); and now I'm facing another error:

../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

It's pretty hard for me to understand what's going on since I've never built a library before, and the build with gulp appears to be kinda hacky, since this line ngc = require('@angular/compiler-cli/src/main').main refers to a function that does not belong to the public API.

Edit:

Following the idea in the comments (and my own feeling), I tried to build without gulp:

  • Created a angular.json file
  • Separated index.ts into public_api.ts and simple-notifications.module.ts
  • Did some changes in the files and folders structure

But I still have the same exact error...

My attempt: https://github.com/youkouleley/angular2-notifications I try to build this with ng build, the scripts in package.json have not been updated


回答1:


EDIT 2

Now that Angular 9 is released:

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.


EDIT

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.


I managed to get this working by making some alterations from the repo you posted of your attempt. Screenshot below:

The changes made were as follows:

The changes in package.json are irrelevant.

enableIvy:false means that your library will not be built with the Ivy Rendering engine (this is currently recommended for libraries), but doesn't prevent your library from being used in an Ivy enabled app.

Word of note that IVY is still in somewhat of an experimental mode

annotateClosureCompiler: false: Is related to this issue https://github.com/angular/angular/pull/26738




回答2:


I figured out what going wrong, I had a Angular 8 project (containing a library project) that I have upgraded to Angular 9 (with a lib also: ng generate library core, I created at first a new angular 9 project to compare the differences with my project made with ng 8, i figure out that there is some things missing, here is the steps I followed: - Delete node_modules - Delete dist folder - In the Angular 9 project lib there is a new file called: tsconfig.lib.prod.json

{
  "extends": "./tsconfig.lib.json",
  "angularCompilerOptions": {
    "enableIvy": false
  }
}
  • In your angular.json add the following:
{
  ...
  "projects": {
     ...
     "configurations": {
            "production": {
              "tsConfig": "projects/core/tsconfig.lib.prod.json"
             }
      }
  ...
  }
}
  • run ng build --prod, and everything will work :D

PS: in this case the library is called "core"



来源:https://stackoverflow.com/questions/58473489/library-build-fails-with-angular-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!