Explore an optimized Angular folder structure for large-scale applications using CSS. Learn best practices for modularization, lazy loading, state management, and component organization to ensure maintainability, scalability, and performance in your Angular projects.
1. Base Folder Structure
src/
│
├── app/
│ ├── core/ # Core module (singleton services, utility classes)
│ ├── shared/ # Shared module (components, directives, pipes, etc.)
│ ├── features/ # Feature modules (for specific domain functionality)
│ ├── assets/ # Static files (images, fonts, etc.)
│ ├── environments/ # Environment-specific configurations
│ ├── app.component.ts # Root component
│ ├── app.module.ts # Root module
│ └── app-routing.module.ts
│
├── assets/ # Global assets (images, styles)
│
├── environments/ # Environment configuration files (dev, prod, etc.)
│
└── styles/ # Global styles
2. Detailed Folder Structure Explanation
src/app/
- This is the root folder of your Angular application. All your app-specific modules and code will reside here.
src/app/core/
- Purpose: Contains singleton services, utility functions, and configuration files that are used globally throughout the application.
- Contents:
- Services: Authentication, authorization, data management, API services, etc.
- Models/Interfaces: Shared data models.
- Interceptors: HTTP interceptors for handling requests globally.
- Guards: Route guards for authentication/authorization checks.
- Environment: Core environment configurations.
- CoreModule: A module where core functionality is provided (usually imported only in
AppModule
).
Example:
core/
├── services/
│ ├── auth.service.ts
│ ├── api.service.ts
├── guards/
│ ├── auth.guard.ts
├── interceptors/
│ ├── http.interceptor.ts
├── models/
│ ├── user.model.ts
└── core.module.ts
src/app/shared/
- Purpose: Contains reusable components, directives, pipes, and services that are shared across multiple feature modules.
- Contents:
- Components: Generic components like buttons, modals, forms, etc.
- Directives: Custom directives used globally.
- Pipes: Shared pipes like date formatting, filtering, etc.
- Utilities: Helper functions or services.
- SharedModule: Imports and exports common components, directives, pipes, and services to be used across the app.
Example:
shared/
├── components/
│ ├── button/
│ ├── modal/
├── directives/
│ ├── tooltip.directive.ts
├── pipes/
│ ├── date.pipe.ts
├── utils/
│ ├── date-utils.ts
└── shared.module.ts
src/app/features/
- Purpose: Contains feature-specific modules that encapsulate functionality for distinct sections of the app. Each feature should be self-contained and only import the shared module when needed.
- Contents:
- Each feature module is typically divided into components, services, and models that pertain to that feature.
- Modules for different features (e.g., Dashboard, Profile, Settings, etc.).
Example:
features/
├── dashboard/
│ ├── components/
│ ├── dashboard.module.ts
│ ├── dashboard.component.ts
├── profile/
│ ├── components/
│ ├── profile.module.ts
│ ├── profile.component.ts
└── settings/
├── components/
├── settings.module.ts
├── settings.component.ts
src/app/assets/
- Purpose: Store static files like images, fonts, and icons. This can also include any static JSON files or other assets that need to be loaded by the app.
Example:
assets/
├── images/
├── fonts/
└── icons/
src/app/environments/
- Purpose: Contains environment-specific configuration files (e.g., API endpoints, logging levels). Angular CLI automatically replaces these files based on the build environment (e.g., development, production).
- Contents:
environment.ts
(development)environment.prod.ts
(production)
Example:
environments/
├── environment.ts # Development configuration
└── environment.prod.ts # Production configuration
src/app/styles/
- Purpose: Global styles for the application.
- Contents:
- Global CSS files, including variables, mixins, and resets.
- Use
styles.css
as the main entry point for all styles.
Example:
styles/
├── _variables.css
├── _mixins.css
└── styles.css
3. Additional Considerations for Large-Scale Applications
- Lazy Loading: Use lazy-loaded modules for features that are not required initially. This helps in reducing the initial load time.
- State Management: If your app requires complex state management, consider using NgRx or Akita to manage state globally.
- Routing: For large applications, use a feature-based routing strategy where each feature module has its own routing module (e.g.,
dashboard-routing.module.ts
). - Testing: Each module (core, shared, features) should have its own testing strategy. Store unit and integration tests next to the components, services, or modules they test.
- Internationalization (i18n): If you are supporting multiple languages, consider a dedicated folder for translations or an i18n service in the core module.
Example:
src/
├── app/
│ ├── core/
│ ├── shared/
│ ├── features/
│ ├── i18n/ # For translations
│ ├── app.component.ts
│ ├── app.module.ts
│ └── app-routing.module.ts
4. Modularization Best Practices
- Break down large components into smaller, reusable components.
- Each feature should encapsulate its own logic (components, services, etc.) to promote maintainability and reusability.
- Avoid large monolithic components; instead, create small, single-responsibility components.
- Use Angular services for business logic and data manipulation, and keep components focused on rendering the UI.
5. Sample Full Structure
src/
├── app/
│ ├── core/
│ │ ├── services/
│ │ │ ├── auth.service.ts
│ │ ├── interceptors/
│ │ │ ├── http.interceptor.ts
│ │ ├── guards/
│ │ │ ├── auth.guard.ts
│ │ └── core.module.ts
│ ├── shared/
│ │ ├── components/
│ │ │ ├── button/
│ │ ├── pipes/
│ │ │ ├── date.pipe.ts
│ │ └── shared.module.ts
│ ├── features/
│ │ ├── dashboard/
│ │ │ ├── dashboard.module.ts
│ │ │ ├── dashboard.component.ts
│ │ ├── profile/
│ │ │ ├── profile.module.ts
│ │ │ ├── profile.component.ts
│ │ └── settings/
│ │ ├── settings.module.ts
│ │ ├── settings.component.ts
│ ├── app.component.ts
│ ├── app.module.ts
│ └── app-routing.module.ts
├── assets/
│ ├── images/
│ ├── icons/
├── environments/
│ ├── environment.ts
│ └── environment.prod.ts
└── styles/
├── _variables.css
├── _mixins.css
└── styles.css
This structure ensures that the application remains modular, maintainable, and scalable as the application grows. You can adjust or extend this structure based on specific project needs and team preferences. By using CSS instead of SCSS, you'll avoid the need for preprocessing and simplify the setup if you're looking for a lightweight approach.