Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to package an Angular library to support Angular 8, 9 and 10

With Angular 10 being released, I am updating the version of Angular used to build a library + demo application to version 10.

Generally this goes smoothly, and the library remains compatible with previous versions of Angular, but this doesn't seem to be the case with this release (prior version build against Angular 9 works fine with Angular 8).

The output typescript definition files include:

import * as ɵngcc0 from '@angular/core';

...

static ɵfac: ɵngcc0.ɵɵFactoryDef<QrCodeComponent, never>;
static ɵcmp: ɵngcc0.ɵɵComponentDefWithMeta<QrCodeComponent, "qr-code", never, { "value": "value"; "size": "size"; "errorCorrectionLevel": "errorCorrectionLevel"; }, {}, never, never>;

Which causes errors like this when consuming in an Angular 8 project:

 ERROR in node_modules/ng-qrcode/lib/qr-code.component.d.ts(7,25): error TS2694: Namespace '"/ngqrcode-ng8-test/node_modules/@angular/core/core"' has no exported member 'ɵɵFactoryDef'.
    node_modules/ng-qrcode/lib/qr-code.component.d.ts(8,18): error TS2314: Generic type 'ɵɵComponentDefWithMeta' requires 6 type argument(s).
    node_modules/ng-qrcode/lib/qr-code.directive.d.ts(13,25): error TS2694: Namespace '"/ngqrcode-ng8-test/node_modules/@angular/core/core"' has no exported member 'ɵɵFactoryDef'.

I created the test angular 8 project freshly using:

npx @angular/cli@^8 new ngqrcode-ng8-test

You can see my WIP pull request for this change here: https://github.com/mnahkies/ng-qrcode/pull/8

Note: I already have enableIvy false in my compiler options:

"angularCompilerOptions": {
    "enableIvy": false
  }

Is there a way to build a library using Angular v10 that maintains compatibility with Angular v8?

like image 425
Michael Avatar asked Nov 07 '22 06:11

Michael


1 Answers

There are 2 big breaking changes in angular 10:

  1. 10 requires tslib 2.0.0 vs. <10 requires tslib < 2.0.0,
  2. 10 uses typescript 3.9 vs. < 10 requires typescript < 3.9

I don't think you can package a lib for 8, 9, and 10 in one single package :-)

like image 80
Abdessattar Avatar answered Nov 14 '22 22:11

Abdessattar