Angular Project Folder Structure - JU-DEV-Bootcamps/ERAS GitHub Wiki

There are several alternatives to the traditional folder structure in Angular applications, particularly when considering principles like Atomic Design. Here’s a breakdown of various approaches:

Atomic Design Structure

Atomic Design is a methodology that categorizes components into five distinct levels: Atoms, Molecules, Organisms, Templates, and Pages. This structure can be adapted for Angular projects as follows: Atoms: Basic building blocks such as buttons, inputs, and labels. Each atom can have its own folder for styles and tests. Molecules: Combinations of atoms that form functional UI components, like a search form combining an input and a button. Organisms: More complex components composed of groups of molecules and/or atoms, such as a header that includes a logo, navigation links, and a search form. Templates: Page-level components that define the layout but not the content, showcasing how organisms fit together. Pages: Specific instances of templates filled with real content.

Example Folder Structure

src/
  components/
    atoms/
      button/
        button.component.ts
        button.component.scss
    molecules/
      search-form/
        search-form.component.ts
        search-form.component.scss
    organisms/
      header/
        header.component.ts
        header.component.scss
    templates/
      main-template/
        main-template.component.ts
    pages/
      home/
        home.component.ts

Feature-Based Structure

This structure enhances collaboration between design and development teams by using a common vocabulary and facilitates easier navigation through the codebase as it scales23. Feature-Based Structure Another popular approach is the Feature-Based Structure, where files are organized based on application features rather than types. This can be particularly useful for larger applications:

Example Folder Structure

src/
  app/
    features/
      featureA/
        featureA.module.ts
        featureA.component.ts
        services/
          featureA.service.ts
      featureB/
        featureB.module.ts
        featureB.component.ts
        services/
          featureB.service.ts
    shared/
      components/
      services/

Colocation Strategy

This method allows for better encapsulation of features, making it easier to manage dependencies and maintain the codebase14. Colocation Strategy The Colocation Strategy involves placing related files together within the same directory. For instance, keeping components alongside their styles and tests can reduce the cognitive load when navigating the project:

Example Folder Structure

src/
  app/
    features/
      featureA/
        featureA.component.ts
        featureA.component.scss
        featureA.component.spec.ts

This strategy promotes modularity and makes it easier to move or refactor components since all relevant files are in one place7. Conclusion Choosing the right folder structure in Angular depends on various factors including team size, project complexity, and personal preference. The Atomic Design approach offers a clear hierarchy that aligns well with UI design principles, while Feature-Based and Colocation strategies enhance maintainability and organization in larger applications. Ultimately, consistency within the chosen structure is key to effective development practices346.

Structure Type Pros Cons
Atomic Design - Promotes a clear hierarchy and organization- Facilitates collaboration between design and development teams- Encourages reusability of components- Makes it easier to maintain and scale UI components - Can become complex with many small files- May require additional initial setup and understanding of the methodology- Might not suit all types of applications, especially simpler ones
Feature-Based - Organizes code by feature, enhancing encapsulation- Easier to manage dependencies related to specific features- Facilitates team collaboration on distinct features- Simplifies navigation for larger applications - Can lead to duplication of shared components across features- May require more effort to find common/shared components- Potentially less intuitive for new developers unfamiliar with the structure
Colocation Strategy - Keeps related files together, reducing cognitive load- Simplifies refactoring since all relevant files are in one place- Enhances modularity of components- Makes it easier to understand component functionality at a glance - Can lead to deep nesting if not managed properly- May clutter directories if many files are colocated- Less clear separation between different types of files (e.g., services vs. components)

Angular folder structure - Proposal

The given Angular project structure is designed to promote scalability, maintainability, and separation of concerns, ensuring a clean and organized codebase as the application grows.

src/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ core/
|   |   β”œβ”€β”€ auth (example)
|   |   |   β”œβ”€β”€ auth.service.ts
|   |   |   |   auth.service.spec.ts
|   |   |   |   auth.model.ts
|   |   |   |   auth.guard.spec.ts
|   |   |   |   auth.guard.ts
|   |   |   |   auth.routes.ts
β”‚   β”‚   |   └── pages/       
β”‚   β”‚   └── other-core-function/
β”‚   β”‚
β”‚   β”œβ”€β”€ features/
β”‚   β”‚   β”œβ”€β”€ feature-one/     
β”‚   β”‚   β”‚   β”œβ”€β”€ components/  
β”‚   β”‚   β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”‚   β”œβ”€β”€ services/    
β”‚   β”‚   β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”‚   └── featureone.routes.ts
β”‚   β”‚   └── feature-two/     
β”‚   β”‚
β”‚   β”œβ”€β”€ shared/
β”‚   β”‚   β”œβ”€β”€ components/     
β”‚   β”‚   β”œβ”€β”€ directives/     
β”‚   β”‚   β”œβ”€β”€ pipes/          
β”‚   β”‚   └── shared.module.ts
β”‚   β”‚
β”‚   β”œβ”€β”€ app.component.css
β”‚   β”œβ”€β”€ app.component.html
β”‚   β”œβ”€β”€ app.component.spec.ts
β”‚   β”œβ”€β”€ app.component.ts
β”‚   β”œβ”€β”€ app.config.ts
β”‚   └── app.routes.ts        
β”‚
β”œβ”€β”€ assets/                  
β”œβ”€β”€ environments/            
β”‚   β”œβ”€β”€ environment.ts       
β”‚   └── environment.prod.ts  
β”‚
β”œβ”€β”€ styles/                  
β”œβ”€β”€ index.html               
└── main.ts      

Folder Explanation

  1. core/: Contains application-wide singleton services (e.g., AuthService, ApiService), models, and utilities. This folder is loaded once and remains in memory throughout the app's lifecycle.
  2. features/: Contains feature-specific modules, each encapsulating its functionality (e.g., a dashboard module or user-profile module). These modules often include their own components, services, and routing configuration.
  3. shared/: Contains reusable components (e.g., buttons, headers), directives, and pipes that can be shared across multiple modules.
  4. environments/: Manages environment-specific configurations, like API URLs for development and production.
  5. styles/: Holds global styles applied across the entire application.

Design Pattern In Folder Structure

Singleton Pattern (Core Module)

The Singleton Pattern is used in the core.module.ts and services within the core/services/ folder to ensure a single instance of shared services exists throughout the application. For example, an authentication service can manage the logged-in state and user details globally.

Module Pattern (Feature Modules)

The Module Pattern encapsulates related components, services, and routes into isolated feature modules. This promotes separation of concerns by organizing functionality into self-contained units.For instance, a dashboard module can contain all its components and routing logic, making the feature modular and easy to maintain.

Component Reusability Pattern (Shared Module)

The Component Reusability Pattern emphasizes defining reusable components in one location, the shared folder, and exporting them for use across the application. This reduces duplication code and ensures consistent behavior throughout the app. For example, a reusable Button can be declared in the SharedModule and exported for use in any feature module that requires a button.