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上的大多数项目在每个Angular版本的Git中都维护单独的工作分支.

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.

然后您必须通过NPM在本地 node_modules 中安装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 依赖项.因此,组件库不会捆绑它们自己对框架库的依赖.而是将它们编译为 join 所属应用程序使用的对等依赖项.这意味着,所有库必须使用将要使用的应用程序编译的相同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不会为使用8.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.