Skip to content
advanced Phase 16 · Enterprise Patterns and Capstone

Shared Libraries and Monorepo Layout

Extract reusable code into buildable workspace libraries.

40m
0 problems
Topic Progress 0%

Monorepo Structure

A monorepo places apps and libraries under one workspace with a single dependency graph, enabling code sharing and consistent tooling.

Angular CLI Workspace

# Create a new workspace
ng new my-workspace --create-application=false

# Generate an application
ng generate application my-app

# Generate a library
generate library my-lib

Workspace Structure

my-workspace/
├── angular.json
├── package.json
├── tsconfig.json
├── nx.json                    # If using Nx
├── apps/
│   ├── my-app/
│   │   ├── src/
│   │   ├── angular.json
│   │   └── project.json
│   └── my-app-e2e/
│
├── libs/
│   ├── shared-ui/           # Reusable UI components
│   │   ├── src/
│   │   │   ├── lib/
│   │   │   └── index.ts      # Public API
│   │   ├── ng-package.json
│   │   └── project.json
│   │
│   ├── shared-utils/         # Utility functions
│   │   ├── src/
│   │   │   ├── lib/
│   │   │   └── index.ts
│   │   └── ng-package.json
│   │
│   └── feature-users/        # Feature library
│       ├── src/
│       │   ├── lib/
│       │   └── index.ts
│       └── ng-package.json
│
└── tools/
    └── schematics/

Buildable and Publishable Libraries

Buildable libraries compile independently, speeding up large workspace builds. Publishable libraries can be packaged for npm.

Library Configuration

// libs/shared-ui/ng-package.json
{
  "$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../../dist/libs/shared-ui",
  "lib": {
    "entryFile": "src/index.ts"
  }
}

Public API (index.ts)

// libs/shared-ui/src/index.ts
export * from './lib/button/button.component';
export * from './lib/modal/modal.component';
export * from './lib/card/card.component';

Building Libraries

# Build a specific library
ng build shared-ui

# Build all libraries
ng run-many --target=build --all

# Build with dependencies
ng build shared-ui --with-deps

Using Libraries in Apps

// In app.config.ts or component imports
import { ButtonComponent } from '@my-workspace/shared-ui';

@Component({
  imports: [ButtonComponent],
})
export class MyComponent {}

Library Types

Type Purpose Buildable Publishable
UI Reusable components Yes Yes
Utils Utility functions Yes Yes
Feature Feature modules Yes No
Data State management Yes No

Nx Integration

Nx provides additional monorepo features:

# Install Nx
npm install nx --save-dev

# Generate a library
nx generate @nx/angular:library shared-ui

# Run affected builds
nx affected:build

# Visualize dependencies
nx graph

Quiz

1. What is a monorepo?

Question 1 options

2. What is the purpose of an entry point file (index.ts)?

Question 2 options

3. What is the benefit of buildable libraries?

Question 3 options

Flashcards

Question

What is a monorepo?

Answer

Apps and libraries under one workspace. Shared dependency graph.

Question

Purpose of entry point file?

Answer

Defines the public API of a library.

Question

Benefit of buildable libraries?

Answer

Compile independently. Faster incremental builds.

Revision Notes

Key Takeaways

  • 1. Monorepo: apps + libraries under one workspace
  • 2. Entry point files define public APIs
  • 3. Buildable libraries compile independently
  • 4. Nx provides additional monorepo tooling

Interview Tips

  • • Explain monorepo structure and benefits
  • • Be ready to describe library configuration
  • • Mention Nx for advanced monorepo features

Cheat Sheet

Monorepo and Libraries Cheat Sheet

Create workspace:

ng new my-workspace --create-application=false
ng generate application my-app
ng generate library my-lib

Public API:

// libs/my-lib/src/index.ts
export * from './lib/my-component';

Build:

ng build my-lib

Continue Learning