Angular 库 Monorepo:是否可以为每个库使用不同的版本? [英] Angular Libraries Monorepo: Is it possible to use different versions for every library?

查看:20
本文介绍了Angular 库 Monorepo:是否可以为每个库使用不同的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个我似乎找不到答案的简单问题.

just a simple question I can't seem to find an answer for.

我正在尝试构建一个 monorepo,它应该能够使用 angular-cli v8,但仍然能够编译和构建为 v2、v3、v4 等制作的库(主要是组件和服务).

I'm trying to build a monorepo which should be able to use angular-cli v8, but still being able to compile and build libraries (mainly components and services) made for v2, v3, v4, etc..

版本之间有很多变化,让我们举个例子:

Many things have changed between versions, so let's take an example:

V5 之前的 Angular 使用 @angular/http.

Angular prior to V5 uses @angular/http.

我们当前的版本(8)使用 @angular/common/http

Our current version(8) uses @angular/common/http

我已经尝试在库的 package.json 中应用特定版本,但它似乎无论如何都不起作用,并且在构建期间(甚至在 IDE 中),它告诉我它找不到 @angular/http,因为它从 Angular V5 开始被弃用.

I already tried to apply a specific version in the library's package.json, but it doesn't seem to work anyway and, during build time (And even in the IDE), it tells me it can't find @angular/http, as it is deprecated since Angular V5.

是否有任何可能的方式为我们提供所需的功能?

Is there any possible way to provide us the functionality we need?

选择这样的 monorepo 不是我的选择,所以请避免评论多么糟糕的模式!"..我只想知道是否有机会让它按照我期望的方式工作.

It was not my choice to go for such a monorepo, so please avoid comments about "what a bad pattern!".. I'd just like to know if there's a chance to make it work the way I expect it to.

推荐答案

遗憾的是,答案是否定的.

Sadly, the answer is No.

在使用 Angular 源代码时,您与特定版本的 Angular 紧密耦合.因此,GitHub 上的大多数项目在 Git 中为每个 Angular 版本维护单独的工作分支.

When working with Angular source code you are tightly coupled to a specific version of Angular. So most projects on GitHub maintain separate working branches in Git for each Angular version.

然后您必须通过本地 node_modules 中的 NPM 安装 Angular 的编译链,并通过 npm build 执行构建,以便使用正确版本的 Angular 来编译该代码,因为您全局安装的 Angular CLI 可能会跟踪最新的稳定版本.这意味着 Angular CLI for 8 不能用于为 Angular 6 构建项目.

You have to then install Angular's compiling chain via NPM in the local node_modules and perform builds via npm build so that the correct versions of Angular are used to compile that code, because your global install of Angular CLI will likely be tracking the latest stable release. That means that Angular CLI for 8 can not be used to build a project for Angular 6.

以上主要适用于 Angular 库.因此,如果您为 Angular 7 构建一个组件库,并且还希望支持 Angular 8.您需要为这些版本构建的该库的两个分支.

The above applies mainly to Angular Libraries. So if you build a component library for Angular 7, and want to also support Angular 8. You need two branches of that library that build for those versions.

我正在尝试构建一个 monorepo,它应该能够使用 angular-cli v8,但仍然能够编译和构建为 v2、v3、v4 等制作的库(主要是组件和服务).

I'm trying to build a monorepo which should be able to use angular-cli v8, but still being able to compile and build libraries (mainly components and services) made for v2, v3, v4, etc..

其实也无所谓.您不能在 Angular v8 应用程序中使用 Angular v2 库.Angular 是这里的 peer 依赖项.所以组件库不会捆绑它们自己对框架库的依赖.相反,它们被编译为加入拥有应用程序使用的对等依赖项.这意味着,您的所有库都必须使用将由将使用的应用程序编译的相同 Angular 版本进行编译.

It doesn't really matter. You can not consume an Angular v2 Library in Angular v8 application. Angular is the peer dependency here. So component libraries do not bundle their own dependency of the framework library. Instead, they are compiled to join the peer dependency used by the owning application. That means, that all your libraries must be compiled with the same Angular version compiled by the application that will use.

NPM 将安装依赖包,这些包因次要版本而异.因此 Angular 8.0.0 库将与 Angular 8.9.9 应用程序一起安装,但技术上如果该库与该版本一起工作,那只是幸运.在测试之前您不知道,但 NPM 不会为使用 9.0.0 的项目的 8.0.0 对等安装库.实现这一点的唯一方法是更改​​组件库定义的对等依赖项,但幸运的是将其与应用程序一起编译并查看它是否有效.Angular 长期以来一直不与以前的版本完全兼容 API(例如,路由器最近更改了惰性模块的加载方式).

NPM will install dependency packages that differ by minor versions. So Angular 8.0.0 libraries will be installed with Angular 8.9.9 applications, but technically it's just good fortune if the library works with that version. You don't know until you've tested it, but NPM will not install the library for a peer of 8.0.0 with a project that is using 9.0.0. The only way to make that happen is to change the peer dependency defined by the component library, but the luck is in compiling it with the application and seeing if it works. Angular has a long history of not being purely API compatible with previous versions (i.e. the router recently changed in how lazy modules are loaded as an example).

我希望这能回答您的问题.我在这里可能有点跑题了.

I hope this answers your question. I might have gone a little off topic here.

这篇关于Angular 库 Monorepo:是否可以为每个库使用不同的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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