打包 Angular 库以支持 Angular 8、9 和 10 的正确方法

Mic*_*ael 5 typescript angular angular-library ng-packagr

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

\n

一般来说,这一切进展顺利,并且该库仍然与 Angular 的早期版本兼容,但此版本似乎并非如此(针对 Angular 9 的先前版本构建与 Angular 8 运行良好)。

\n

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

\n
import * as \xc9\xb5ngcc0 from \'@angular/core\';\n\n...\n\nstatic \xc9\xb5fac: \xc9\xb5ngcc0.\xc9\xb5\xc9\xb5FactoryDef<QrCodeComponent, never>;\nstatic \xc9\xb5cmp: \xc9\xb5ngcc0.\xc9\xb5\xc9\xb5ComponentDefWithMeta<QrCodeComponent, "qr-code", never, { "value": "value"; "size": "size"; "errorCorrectionLevel": "errorCorrectionLevel"; }, {}, never, never>;\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
 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 \'\xc9\xb5\xc9\xb5FactoryDef\'.\n    node_modules/ng-qrcode/lib/qr-code.component.d.ts(8,18): error TS2314: Generic type \'\xc9\xb5\xc9\xb5ComponentDefWithMeta\' requires 6 type argument(s).\n    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 \'\xc9\xb5\xc9\xb5FactoryDef\'.\n
Run Code Online (Sandbox Code Playgroud)\n

我使用以下命令新创建了测试 Angular 8 项目:

\n
npx @angular/cli@^8 new ngqrcode-ng8-test\n
Run Code Online (Sandbox Code Playgroud)\n

您可以在此处查看我对此更改的 WIP 拉取请求:https://github.com/mnahkies/ng-qrcode/pull/8

\n

注意:我的编译器选项中已经设置了enableIvy false:

\n
"angularCompilerOptions": {\n    "enableIvy": false\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

Abd*_*tar 1

Angular 10 有 2 个重大变化:

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

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