简体   繁体   English

将多个现有 Angular 项目添加到 NX 工作区/monorepo

[英]Adding multiple existing Angular projects into an NX workspace/monorepo

We are and Angular shop and are considering using a NX to manage a monorepo.我们和 Angular 商店正在考虑使用 NX 来管理 monorepo。 I've seen many guides and articles on converting and existing Angular CLI workspace to an NX workspace.我看过许多关于将现有 Angular CLI 工作区转换为 NX 工作区的指南和文章。

So our approach would be to take our most complicated project and convert that to an NX workspace.因此,我们的方法是采用我们最复杂的项目并将其转换为 NX 工作区。 The next step would then to add our other Angular projects to this new workspace.下一步是将我们的其他 Angular 项目添加到这个新工作区。

How would I go about doing this?我将如何 go 这样做? Would I have to basically use the NX cli to create a blank app and then copy all of our code into the blank app?我是否必须基本上使用 NX cli 创建一个空白应用程序,然后将我们所有的代码复制到空白应用程序中? Then we would have to npm install all of the packages that this new project needs?那么我们必须 npm 安装这个新项目需要的所有包吗?

I would actually start with a simple project and try to find some best practices that you want to use in your organization.我实际上会从一个简单的项目开始,并尝试找到一些您想在您的组织中使用的最佳实践。 Like Angular itself, Nx Workspace is opinionated but only to a certain extend.就像 Angular 本身一样,Nx Workspace 是固执己见的,但只是在一定程度上。 You still have the responsibility to decide how separate your code into logical units.您仍然有责任决定如何将代码分成逻辑单元。

One main thing to keep in mind is that Nx works with the idea of apps and libs .要记住的一件主要事情是 Nx 与appslibs的想法一起工作。 Ideally an app is very small, and most if not all the actual functionality lives in libs .理想情况下,一个app非常小,并且大多数(如果不是全部)实际功能都存在于libs中。 In Angular that generally means you lazy-load components that live in libs.在 Angular 中,这通常意味着您延迟加载位于库中的组件。

With regards to how to structure the libs, there are different ways to go about it.关于如何构建库,go 有不同的方法。 You can read this blogpost to get some ideas.你可以阅读这篇博文以获得一些想法。

Also, keep in mind that working a mono repo does not mean you have to have all of your companies code in that single repo.此外,请记住,使用 mono 存储库并不意味着您必须将所有公司代码都放在该单个存储库中。 For example, I work with a lot of different client projects and it makes way more sense to have a Nx Workspace for each of the client projects.例如,我与许多不同的客户项目合作,为每个客户项目拥有一个 Nx 工作区更有意义。 Then if I need to share code between projects I make sure to publish it to npm and consider it a third-party library.然后,如果我需要在项目之间共享代码,我确保将其发布到 npm 并将其视为第三方库。

How would I go about doing this?我将如何 go 这样做? Would I have to basically use the NX cli to create a blank app and then copy all of our code into the blank app?我是否必须基本上使用 NX cli 创建一个空白应用程序,然后将我们所有的代码复制到空白应用程序中? Then we would have to npm install all of the packages that this new project needs?那么我们必须 npm 安装这个新项目需要的所有包吗?

Yes, and I would personally start with a complete blank slate.是的,我个人会从一个完整的白板开始。

You can do so by creating an Nx Workspace with the empty preset, and make sure to select the Angular CLI:您可以通过使用empty预设创建 Nx 工作区来做到这一点,并确保 select Angular CLI:

$ yarn create nx-workspace <project-name> 

Next, move into your directory and add the Nrwl Angular package:接下来,进入您的目录并添加 Nrwl Angular package:

$ cd <project-name>
$ yarn add -D @nrwl/angular

From that moment on, you can generate apps and libs like so:从那一刻起,您可以像这样生成应用程序和库:

ng g @nrwl/angular:app <app-name>
ng g @nrwl/angular:lib <lib-name>

You can now start moving over code from your previous project into the lib folder, and reference that code from your app .您现在可以开始将之前项目中的代码移动到lib文件夹中,并从您的app中引用该代码。 If you already have an existing module structure in Angular that works well, you can simply create a lib for each of those modules.如果您在 Angular 中已经有一个运行良好的现有模块结构,您可以简单地为每个模块创建一个库。

I hope this helps.我希望这有帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM