打包 Angular 库以支持 Angular 8、9 和 10 的正确方法 [英] Correct way to package an Angular library to support Angular 8, 9 and 10

查看:66
本文介绍了打包 Angular 库以支持 Angular 8、9 和 10 的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

总的来说,这进展顺利,并且该库仍然与以前版本的 Angular 兼容,但此版本似乎并非如此(针对 Angular 9 的先前版本构建适用于 Angular 8).

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

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

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

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

I created the test angular 8 project freshly using:

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

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

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

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

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

"angularCompilerOptions": {
    "enableIvy": false
  }

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

推荐答案

angular 10 有 2 个重大变化:

There are 2 big breaking changes in angular 10:

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

我认为您无法将 8、9 和 10 的库打包到一个包中:-)

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

这篇关于打包 Angular 库以支持 Angular 8、9 和 10 的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆