简体   繁体   English

Angular 库 Monorepo:是否可以为每个库使用不同的版本?

[英]Angular Libraries Monorepo: Is it possible to use different versions for every library?

just a simple question I can't seem to find an answer for.只是一个简单的问题,我似乎无法找到答案。

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-cli v8 的 monorepo,但仍然能够编译和构建为 v2、v3、v4 等制作的库(主要是组件和服务)。

Many things have changed between versions, so let's take an example:版本之间发生了很多变化,让我们举个例子:

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

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

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.我已经尝试在库的 package.json 中应用特定版本,但它似乎无论如何都不起作用,并且在构建期间(甚至在 IDE 中),它告诉我它找不到@angular/http ,因为它自 Angular V5 以来已被弃用。

Is there any possible way to provide us the functionality we need?有什么方法可以为我们提供我们需要的功能吗?

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. go 不是我为这样的 monorepo 选择的,所以请避免评论“多么糟糕的模式”。我只是想知道是否有机会让它按照我期望的方式工作。

Sadly, the answer is No.可悲的是,答案是否定的。

When working with Angular source code you are tightly coupled to a specific version of Angular.使用 Angular 源代码时,您将紧密耦合到 Angular 的特定版本。 So most projects on GitHub maintain separate working branches in Git for each Angular version.因此,GitHub 上的大多数项目在 Git 中为每个 Angular 版本维护单独的工作分支。

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. 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.这意味着 8 的 Angular CLI 不能用于为 Angular 6 构建项目。

The above applies mainly to Angular Libraries.以上主要适用于 Angular 库。 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.因此,如果您为 Angular 7 构建组件库,并且还希望支持 Angular 8。您需要为这些版本构建的该库的两个分支。

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-cli v8 的 monorepo,但仍然能够编译和构建为 v2、v3、v4 等制作的库(主要是组件和服务)。

It doesn't really matter.这并不重要。 You can not consume an Angular v2 Library in Angular v8 application.您不能在 Angular v8 应用程序中使用 Angular v2 库。 Angular is the peer dependency here. Angular 是这里的对等依赖。 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.这意味着,您的所有库都必须使用将使用的应用程序编译的相同 Angular 版本进行编译。

NPM will install dependency packages that differ by minor versions. NPM 将安装次要版本而异的依赖包。 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.因此 Angular 8.0.0 库将与 Angular 8.9.9 应用程序一起安装,但从技术上讲,如果该库与该版本一起使用,那就太好了。 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.在您测试之前您不知道,但是 NPM 不会为使用 9.0.0 的项目安装 8.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 (ie the router recently changed in how lazy modules are loaded as an example). Angular 有很长的历史,不是纯粹的 API 与以前的版本兼容(即路由器最近更改了如何加载惰性模块作为示例)。

I hope this answers your question.我希望这回答了你的问题。 I might have gone a little off topic here.我可能在这里跑题了。

@Reactgular is right in regards to this not being possible. @Reactgular认为这是不可能的。 However the rest is not completely correct.然而 rest 并不完全正确。 I suggest you build your library with Angular 7. This way it would usually be compatible with Angular 6-8.我建议您使用 Angular 7 构建您的库。这样它通常与 Angular 6-8 兼容。 If you are not using rxjs — then it could even work with Angular 4-5 (Angular 6 moved to rxjs 6 and there were breaking changes).如果您不使用 rxjs - 那么它甚至可以与 Angular 4-5 一起使用(Angular 6 移动到 rxjs 6 并且发生了重大变化)。 Not a lot of the API has changed so that really depends on what you use from the framework. API 的变化并不多,因此这实际上取决于您从框架中使用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular MonoRepo Nx - 在应用程序中使用库 - Angular MonoRepo Nx - use library in application 是否可以使用由不同版本的 Angular 构建的不同角度元素 - Is it possible to use different angular elements built with different versions of Angular 如何在不构建的情况下为库(Angular MonoRepo)使用 tsconfig 路径别名 - How to use tsconfig path aliases for libraries (Angular MonoRepo) without build Angular 2:根据数据加载不同版本的库 - Angular 2: Load different versions of the libraries based on the data 库依赖性问题:不同的库取决于同一库的不同版本 - Problem with library dependencies: different libraries depends on different versions of the same library Angular 8:使用库构建 monorepo 应用程序的更好方法 - Angular 8: A better way to build monorepo app with libraries 无法在 Nx monorepo 上创建 Angular 库 - Can't create an Angular Library on Nx monorepo Angular 库与 Angular 模块在 monorepo 中的优势? NX架构 - Benefits of Angular libraries vs Angular modules in a monorepo? NX architecture 两种不同角度应用的 Monorepo 模式? - Monorepo pattern for two different angular applications? 在启用 Ivy 的情况下构建 monorepo Angular9 库是不是一个坏主意 - Is it a bad idea to build monorepo Angular9 libraries with Ivy enabled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM