首页 > 解决方案 > 打包 Angular 库以支持 Angular 8、9 和 10 的正确方法

问题描述

随着 Angular 10 的发布,我将用于构建库 + 演示应用程序的 Angular 版本更新到版本 10。

一般来说,这很顺利,并且该库仍然与以前版本的 Angular 兼容,但是这个版本似乎不是这种情况(针对 Angular 9 的先前版本构建与 Angular 8 配合得很好)。

输出打字稿定义文件包括:

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>;

在 Angular 8 项目中使用时会导致这样的错误:

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

我使用以下方法新创建了测试 angular 8 项目:

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

你可以在这里看到我的 WIP 拉取请求:https ://github.com/mnahkies/ng-qrcode/pull/8

注意:我的编译器选项中已经有 enableIvy false :

"angularCompilerOptions": {
    "enableIvy": false
  }

有没有办法使用 Angular v10 构建一个与 Angular v8 保持兼容性的库?

标签: angulartypescriptangular-libraryng-packagr

解决方案


角度 10 有 2 个重大的重大变化:

  1. 10 需要 tslib 2.0.0 与 <10 需要 tslib < 2.0.0,
  2. 10 使用 typescript 3.9 vs. < 10 需要 typescript < 3.9

我认为您不能将 8、9 和 10 的库打包在一个包中 :-)


推荐阅读