Skip to content

thdk/nx-monorepo-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

178 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nx Monorepo Demo

CI

Welcome to my Nx Monorepo Demo! This repository showcases how Nx can be leveraged to manage a monorepo effectively, combining libraries, applications, and infrastructure projects. The goal is to demonstrate the versatility and power of Nx in handling diverse setups.

Features

  • Centralized Management: Manage multiple projects, including applications, libraries, and infrastructure, within a single repository.
  • Custom Nx Plugin: Includes a custom Nx plugin for Terraform projects, enabling seamless integration and management of infrastructure-as-code.
  • Optimized Build System: Utilize Nx's caching and dependency graph to optimize builds and CI/CD workflows.
  • Diverse Project Types: Support for Node.js, React, and Terraform projects, showcasing Nx's flexibility.

Projects in this Repository

Applications

app-1

  • Environment: Node.js
  • Framework: Fastify
  • Module Type: CommonJS
  • Bundler: Esbuild
    • Bundle: true
    • Third-party: true
  • Local Dependencies:
    • lib-a
    • lib-b (ESM module imported in CJS app)
    • lib-c

react-app-1

  • Environment: Browser
  • Framework: React
  • Module Type: ES Module
  • Bundler: Vite
  • E2E Testing: Playwright

react-router-app-1

  • Environment: Browser
  • Framework: React router (with SSR)
  • Module Type: ES Module
  • Bundler: Vite

Libraries

lib-a

  • Bundler: TypeScript Compiler (tsc)
  • Published: No
  • type: cjs

lib-b

  • Bundler: Esbuild
  • Published: No
  • type: module

lib-c

  • Bundler: TypeScript Compiler (tsc)
  • Published: Yes
  • type: cjs

Terraform Projects

bootstrap-infra

Other Projects

scripts

Utility scripts for managing projects in this repository.

  • release.ts: Automates release processes.

tools/nx-terraform

Custom Nx package to manage Terraform targets for projects containing Terraform files.

releases/thdk

A project dedicated to maintaining a unified version number across multiple independently versioned and deployed applications.

Development

Setup

# Install tools
asdf install
# Install dependencies
npm install

Running Targets

# Run all relevant targets (build, lint, test, ...) for every project
npx nx run-all

# Run all relevant targets (build, lint, test, ...) for affected projects only
npx nx run-affected

Syncpack

This is an external tool not related with nx but works very well together with nx repos.

Why syncpack?

  • ensures all packages use the same version for dependencies
  • ensures app dependencies use the same range specifier

Provided commands in the repo:

npx nx syncpack
npx nx syncpack-fix

However these are automatically run for you in CI and will block any thing that doesn't follow the rules.

IaC with terraform and Nx

Terraform project generator

Default terraform generator settigns for this repo can be found in nx.json under generators.@thdk/nx-terraform.project.

npx nx generate @thdk/nx-terraform:project domain-b-infra

Inferred targets

A custom terraform plugin will infer terraform targets for each project with a main.tf file.

Terraform state for this repo is kept in a GCP bucket for which you must be authenticated if you would want to run this locally (CI authorizes with GCP using Workload Identity Federation)

gcloud auth login
gcloud auth application-default login

# if using docker
gcloud auth configure-docker
# if using podman (aliased as docker)
gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://europe-west1-docker.pkg.dev
# init target is a dependency of other terraform targets so usually you do not have to run it explicitly
npx nx run-many --target terraform-init

npx nx run-many --target terraform-plan

# currently will use --auto-approve so use with caution after inspecting output of terraform-plan command
# TODO: Make this target interactive so user can manually confirm the action for each project.
npx nx run-many --target terraform-apply

Version & release

This is all managed by nx release.

Docs:

Next the terraform setup will read the version from git tags for each affected application and update the deployed service to use that new docker image.

Future improvements

  • Dependencies should be managed by each project itself and no longer add each dependency to root package.json.

About

Nx sample monorepo containing personal best practices for common challenges

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors