Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 7381

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T03:16:09+00:00 2024-11-28T03:16:09+00:00

Repository and File Structure

  • 60k

In this article, I'll show you how to create and organize a repository for your Angular application in the most optimal way.

One Repo to rule them all

You can create your repo using different tools, and the most well-known is, of course, Angular CLI. I recommend using Nx, and I'll explain why.

No matter what tool you choose, I advise you to create a “monorepo” - a repository for multiple projects (Angular CLI also supports it out of the box).

If you will have just a single application in that repo - it is absolutely fine and will add no additional complexity or maintenance cost. But if you later will decide to create one more application in a regular (non-mono) repo - efforts for refactoring an existing non-mono repo will be huge, while in monorepo you'll do it natively. Real-world example.

Sometimes you might have to explain this to your colleagues or managers:  

  • You don't have to move your existing Angular apps into this new repo;
  • You will not move non-Angular and non-js projects to this new repo;
  • It will still be possible to build/deploy apps separately; Every app in monorepo will have to support the same versions of dependencies, including the version of Angular;
  • Monorepo with just one app inside is a perfectly fine repo.

If you decide to go with Angular CLI, generate a workspace for multiple projects - you can find all the instructions in the Angular documentation: link (look for the “Multiple Projects” section). Also, in this case, I recommend you edit tsconfig.json/compilerOptions/paths to make paths pointing to the source code of libraries, not to dist folder.

Why Nx

No worries, I'm not part of the Nrwl Team and not trying to sell their product.

I recommend using Nx monorepo for Angular apps because:

  • With nx affected, you don't need to re-build and re-test everything when you modify the code;
  • With Nx caching, many of your “test” and “build” steps will have 0s execution time - and the cache will be shared with your CI server as well;
  • Nx Executors will help you with the integration of third-party tools;
  • Out-of-the-box integration with CI tools;
  • Libraries have better file structure in Nx-generated repo;
  • You'll retain access to Angular CLI generators, and will get more other generators.

The first 2 reasons will save a lot of time for you. Your development process will be much more comfortable. Your CI time will be shrunk significantly (for one of our mid-size projects, Nx reduced the average CI time from 50 minutes to 4 minutes).

Links:

  • Generate a workspace
  • @nrwl/angular package documentation

File Structure

. ├── 📂 apps │   ├── 📁 example-app-1 │   └── 📁 example-app-2  └── 📂 libs     ├── 📂 api     │   └── 📂 src     │       └── 📂 lib     │           ├── 📂 services     │           │   ├── 📂 users     │           │   │   ├── 📄 interfaces.ts     │           │   │   ├── 📄 README.md     │           │   │   └── 📄 users.service.ts     │           │   └── 📂 products     │           │       ├── 📄 README.md     │           │       └── 📄 products.service.ts     │           ├── 📂 models     │           │   ├── 📄 user.ts     │           │   ├── 📄 ticket.ts     │           │   └── 📄 index.ts     │           └── 📄 index.ts     ├── 📂 ui-components     │   └── 📂 src     │       └── 📂 lib     │           ├── 📄 README.md     │           └── 📂 example     │               ├── 📄 example.component.html     │               ├── 📄 example.component.scss     │               ├── 📄 example.component.spec.ts      │               ├── 📄 example.component.ts      │               ├── 📄 example.component.stories.ts     │               ├── 📄 example.store.ts     │               └── 📄 README.md     └── 📂 tickets         └── 📂 src             └── 📂 lib                 ├── 📄 README.md                 └── 📂 ticket                     ├── 📄 ticket.component.html                     ├── 📄 ticket.component.scss                     ├── 📄 ticket.component.spec.ts                      ├── 📄 ticket.component.ts                      ├── 📄 ticket.store.ts                     └── 📄 README.md 
Enter fullscreen mode Exit fullscreen mode

Here you can see:

  • 2 applications (reminder: it's perfectly fine to have just one application in a monorepo);
  • libs folder for libraries (in the Angular CLI workspace, libraries are located in the “projects” folder, together with applications);
  • 3 libraries: api, ui-components, tickets;
  • Library api has “grouping folders”: services and models - there are different ways to create grouping folders for libraries, and this one is the most simple;
  • ui-components contains components that will be shared across the entire application, maybe across multiple applications;
  • tickets is a “feature” library - there will be components, directives, pipes, and maybe some other kind of code, that will be used only by the “tickets” part of your application;
  • Some folders (like api/models) use the “barrel” approach and have index.ts file to export everything that should be public. Please use your favorite search engine to find more information about the “barrel” approach in TypeScript 😉

Where to put things

From the structure above, you can see that most of the things are located not in apps, but in libs. Applications become, essentially, shells and loaders of the code, located in libs - this approach will help you increase code reuse and will encourage and help you lazy-load your “features”.

So, put everything into libs, only put things into apps if they can not be moved to libs. 

To avoid circular dependencies, it's better to have more libraries. Also, some libraries (like api in our example) should be ready to be used by every other library (to read the models, at least) - it means that api library should not import any code from other libraries to avoid circular dependencies.

Imports

Your tsconfig.json will contain compilerOptions/paths section, with the mapping of your libraries' names to source code paths:

"paths": {     "@wrkspc/api": ["libs/api/src/index.ts"],     "@wrkspc/ui-components": ["libs/ui-components/src/index.ts"],     "@wrkspc/helpers": ["libs/helpers/src/index.ts"],     "@wrkspc/tickets": ["libs/tickets/src/index.ts"] } 
Enter fullscreen mode Exit fullscreen mode

Here “wrkspc” is an example of your workspace name - it will be used as a grouping prefix.

The symbol “@” can be omitted, but if you move one of your libraries to an external registry, then you will not need to modify your code (if “@prefix” will match the name of the published scope).

Because of this configuration section, Angular will understand imports like this:

import { ExampleDropdown } from '@wrkspc/ui-components'; import { User } from '@wrkspc/api'; 
Enter fullscreen mode Exit fullscreen mode

Although, you can't import a library to itself, so:

// In the code, located in `libs/api`,  // You can not use this: import { User } from '@wrkspc/api';  // it should be relative path: import { User } from '../models'; 
Enter fullscreen mode Exit fullscreen mode

Remember, that once you imported at least one file of a library, the size of the whole library will be added to the feature where you import it (if it's not an [import type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export) - it will not add any size and will not affect lazy loading). 

Advice

  • Never ever add any passwords (or other “secrets” - security-wise sensitive information) to the repository. If you need some specific file to contain secrets, first add this file to .gitignore then commit it, and only after that create that file with the sensitive information;
  • You might want to create file proxy.conf.json to avoid CORS limitations: angular.io docs;
  • Don't underestimate the value of README.md files - even a couple of lines of information might save hours of searching for the person who will read this code in a few months. And the chances that that person will be you are high. That's why I recommend creating README for every feature and service.

💙 If you enjoy my articles, consider following me on Twitter, and/or subscribing to receive my new articles by email.

🎩️ If you or your company is looking for an Angular consultant, you can purchase my consultations on Upwork.

angularbeginnersprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.