|
| 1 | +# Nx at Bitwarden |
| 2 | + |
| 3 | +## What is Nx? |
| 4 | + |
| 5 | +Nx is a powerful open-source build system designed specifically for monorepo development. It provides tools and techniques for enhancing developer productivity, optimizing CI performance, and maintaining code quality in complex JavaScript/TypeScript codebases that contain multiple applications and libraries within a single repository. |
| 6 | + |
| 7 | +## Why We're Using Nx |
| 8 | + |
| 9 | +We use Nx in the Bitwarden clients monorepo to improve our development workflow and build efficiency. Key advantages include: |
| 10 | + |
| 11 | +### 1. Unified Commands |
| 12 | +Instead of navigating to specific directories and running individual build commands, you can now execute commands for any project from the repository root: |
| 13 | + |
| 14 | +```bash |
| 15 | +# Old way |
| 16 | +cd apps/web |
| 17 | +npm run build |
| 18 | +``` |
| 19 | + |
| 20 | +```bash |
| 21 | +# New way with Nx |
| 22 | +nx build web |
| 23 | +nx serve desktop |
| 24 | +nx test common |
| 25 | +``` |
| 26 | + |
| 27 | +### 2. Build Caching |
| 28 | +Nx automatically caches build results. If code and dependencies haven't changed, subsequent builds are significantly faster, often restoring results from cache instantly. This saves considerable build time and memory. |
| 29 | + |
| 30 | +### 3. Intelligent Dependency Management |
| 31 | +Nx understands the dependencies between projects. Running a build for one project automatically builds its dependencies first if needed: |
| 32 | + |
| 33 | +You can visualize these dependencies with: |
| 34 | +```bash |
| 35 | +nx graph |
| 36 | +``` |
| 37 | + |
| 38 | +### 4. Configuration-Based Projects |
| 39 | +Each app and library can be defined as a project with a =project.json= file that specifies its build configurations, targets, and other settings. This replaces many scripts previously defined in individual =package.json= files. |
| 40 | + |
| 41 | +## Key Nx Terminology |
| 42 | + |
| 43 | +- *nx.json*: The primary configuration file for the Nx workspace, located at the root. Defines global settings, plugins, workspace layout, and target defaults. |
| 44 | + |
| 45 | +- *project.json*: A configuration file in each project's directory that defines the targets (tasks) for that specific project and the executors used to run them. |
| 46 | + |
| 47 | +- *Target*: A specific task that can be performed on a project, like =build=, =serve=, =lint=, or =test=. Run with =nx <target> <project-name>=. |
| 48 | + |
| 49 | +- *Executor*: The code responsible for performing a target's action, typically provided by Nx plugins (e.g., =@nx/webpack:webpack= for running Webpack builds). |
| 50 | + |
| 51 | +- *Configuration*: A named set of options for running a target in different modes, accessed via the =--configuration= flag (e.g., =nx build browser --configuration=chrome-mv3=). |
| 52 | + |
| 53 | +## Using Nx |
| 54 | + |
| 55 | +To use the Nx cli you have two options: |
| 56 | + |
| 57 | +1. Install nx globally |
| 58 | +2. Use npx |
| 59 | + |
| 60 | +## Common Commands |
| 61 | + |
| 62 | +### Building Projects |
| 63 | + |
| 64 | +```bash |
| 65 | +# Build a project with default configuration |
| 66 | +nx build <project-name> |
| 67 | + |
| 68 | +# Build with specific configuration |
| 69 | +nx build browser --configuration=chrome-mv3 |
| 70 | +nx build web --configuration=bit |
| 71 | + |
| 72 | +# Build all projects (rarely needed) |
| 73 | +nx run-many --target=build --all |
| 74 | +``` |
| 75 | + |
| 76 | +### Serving Projects for Development |
| 77 | + |
| 78 | +```bash |
| 79 | +# Start development server |
| 80 | +nx serve <project-name> |
| 81 | + |
| 82 | +# Serve with specific configuration |
| 83 | +nx serve browser --configuration=firefox-mv2-dev |
| 84 | +``` |
| 85 | + |
| 86 | +### Testing |
| 87 | + |
| 88 | +```bash |
| 89 | +# Run tests for a project |
| 90 | +nx test <project-name> |
| 91 | + |
| 92 | +# Run tests only for projects affected by your changes |
| 93 | +nx affected --target=test |
| 94 | +``` |
| 95 | + |
| 96 | +### Other Useful Commands |
| 97 | + |
| 98 | +```bash |
| 99 | +# Analyze dependencies between projects |
| 100 | +nx graph |
| 101 | + |
| 102 | +# Run a task for all projects affected by your changes |
| 103 | +nx affected --target=<target> |
| 104 | + |
| 105 | +# Show what commands would be run, but don't execute them |
| 106 | +nx affected --target=build --dry-run |
| 107 | +``` |
| 108 | + |
| 109 | +## Understanding the Cache |
| 110 | + |
| 111 | +Nx stores its cache in the =.nx/cache= directory at the root of the repository. This includes: |
| 112 | + |
| 113 | +- Terminal outputs |
| 114 | +- Build artifacts |
| 115 | +- Metadata about the task execution |
| 116 | + |
| 117 | +The cache is created based on the inputs defined for each target. If none of the inputs have changed, Nx will restore the previous output. |
| 118 | + |
| 119 | +## Contributing Guidelines |
| 120 | + |
| 121 | +When contributing to Bitwarden with Nx: |
| 122 | + |
| 123 | +1. *Use Nx commands* from the repository root instead of navigating to individual project directories. |
| 124 | +2. *Respect project boundaries* - imports between projects should follow the established dependency graph. |
| 125 | +3. *When adding new dependencies* between projects, ensure they're reflected in imports in the code. |
| 126 | +4. *For new scripts or build steps*, add them to the appropriate project's =project.json= file rather than to individual =package.json= files. |
| 127 | +5. *Test affected projects* before submitting a PR: |
| 128 | + ```bash |
| 129 | + nx affected --target=test |
| 130 | + nx affected --target=lint |
| 131 | + `` |
| 132 | + |
| 133 | +## Getting Help |
| 134 | + |
| 135 | +If you're having issues with Nx: |
| 136 | +
|
| 137 | +1. Check the [Nx documentation](https://nx.dev/getting-started/intro) |
| 138 | +2. Run =nx --help= or =nx <command> --help= for command-specific help |
| 139 | +3. Reach out to the Platform team for assistance with Nx-specific problems |
| 140 | +
|
| 141 | +## References |
| 142 | +
|
| 143 | +- [Nx Documentation](https://nx.dev/getting-started/intro) |
| 144 | +- [Nx Project Configuration Reference](https://nx.dev/reference/project-configuration) |
| 145 | +- [Nx Cache Documentation](https://nx.dev/concepts/how-caching-works) |
0 commit comments