|
| 1 | +# Caching Example Nx |
| 2 | + |
| 3 | +To cache plugin runner output, you can use the `--cache.write` and `--cache.read` options in combination with `--onlyPlugins` and `--persist.skipReports` command options. |
| 4 | + |
| 5 | +## `{projectRoot}/code-pushup.config.ts` |
| 6 | + |
| 7 | +```ts |
| 8 | +import coveragePlugin from '@code-pushup/coverage-plugin'; |
| 9 | +import jsPackagesPlugin from '@code-pushup/js-packages-plugin'; |
| 10 | +import type { CoreConfig } from '@code-pushup/models'; |
| 11 | + |
| 12 | +export default { |
| 13 | + plugins: [ |
| 14 | + await coveragePlugin({ |
| 15 | + reports: ['coverage/lcov.info'], |
| 16 | + coverageTypes: ['function', 'branch', 'line'], |
| 17 | + }), |
| 18 | + await jsPackagesPlugin({ |
| 19 | + packageManager: 'npm', |
| 20 | + }), |
| 21 | + ], |
| 22 | + upload: { |
| 23 | + server: 'https://portal.code-pushup.dev/api', |
| 24 | + organization: 'my-org', |
| 25 | + project: 'lib-a', |
| 26 | + apiKey: process.env.CP_API_KEY, |
| 27 | + }, |
| 28 | +} satisfies CoreConfig; |
| 29 | +``` |
| 30 | + |
| 31 | +## `nx.json` |
| 32 | + |
| 33 | +```json |
| 34 | +{ |
| 35 | + "defaultTarget": { |
| 36 | + "code-pushup": { |
| 37 | + "cache": true, |
| 38 | + "outputs": ["{options.outputPath}"], |
| 39 | + "executor": "nx:run-commands", |
| 40 | + "options": { |
| 41 | + "command": "node packages/cli/src/index.ts", |
| 42 | + "config": "{projectRoot}/code-pushup.config.ts", |
| 43 | + "cache.read": true, |
| 44 | + "upload.project": "{projectName}", |
| 45 | + "outputPath": "{projectRoot}/.code-pushup" |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + "code-pushup:coverage": { |
| 50 | + "cache": true, |
| 51 | + "outputs": ["{options.outputPath}"], |
| 52 | + "executor": "nx:run-commands", |
| 53 | + "options": { |
| 54 | + "command": "npx @code-pushup/cli collect", |
| 55 | + "config": "{projectRoot}/code-pushup.config.ts", |
| 56 | + "cache.write": true, |
| 57 | + "persist.skipReports": true, |
| 58 | + "persist.outputDir": "{projectRoot}/.code-pushup", |
| 59 | + "upload.project": "{projectName}", |
| 60 | + "outputPath": "{projectRoot}/.code-pushup/coverage" |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## `{projectRoot}/package.json` |
| 67 | + |
| 68 | +```json |
| 69 | +{ |
| 70 | + "name": "lib-a", |
| 71 | + "targets": { |
| 72 | + "int-test": { |
| 73 | + "cache": true, |
| 74 | + "outputs": ["{options.coverage.reportsDirectory}"], |
| 75 | + "executor": "@nx/vite:test", |
| 76 | + "options": { |
| 77 | + "configFile": "packages/lib-a/vitest.int.config.ts", |
| 78 | + "coverage.reportsDirectory": "{projectRoot}/coverage/int-test" |
| 79 | + } |
| 80 | + }, |
| 81 | + "unit-test": { |
| 82 | + "cache": true, |
| 83 | + "outputs": ["{options.coverage.reportsDirectory}"], |
| 84 | + "executor": "@nx/vite:test", |
| 85 | + "options": { |
| 86 | + "configFile": "packages/lib-a/vitest.unit.config.ts", |
| 87 | + "coverage.reportsDirectory": "{projectRoot}/coverage/unit-test" |
| 88 | + } |
| 89 | + }, |
| 90 | + "code-pushup:coverage": { |
| 91 | + "dependsOn": ["unit-test", "int-test"] |
| 92 | + }, |
| 93 | + "code-pushup": { |
| 94 | + "dependsOn": ["code-pushup:coverage"] |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Nx Task Graph |
| 101 | + |
| 102 | +This configuration creates the following task dependency graph: |
| 103 | + |
| 104 | +**Legend:** |
| 105 | + |
| 106 | +- 🐳 = Cached target |
| 107 | + |
| 108 | +```mermaid |
| 109 | +graph TD |
| 110 | + A[lib-a:code-pushup 🐳] --> B[lib-a:code-pushup:coverage] |
| 111 | + A --> E[lib-a:code-pushup:js-packages] |
| 112 | + B --> C[lib-a:unit-test 🐳] |
| 113 | + B --> D[lib-a:int-test 🐳] |
| 114 | +``` |
| 115 | + |
| 116 | +## Command Line Example |
| 117 | + |
| 118 | +```bash |
| 119 | +# Run all affected projects e.g. nx run lib-a:code-pushup:coverage |
| 120 | +nx affected --target=code-pushup:* |
| 121 | + |
| 122 | +# Run all affected projects and upload the report to the portal |
| 123 | +nx run reop-source:code-pushup autorun |
| 124 | +``` |
| 125 | + |
| 126 | +This approach has the following benefits: |
| 127 | + |
| 128 | +1. **Parallel Execution**: Plugins can run in parallel |
| 129 | +2. **Finegrained Caching**: Code level cache invalidation enables usage of [affected](https://nx.dev/recipes/affected-tasks) command |
| 130 | +3. **Dependency Management**: Leverage Nx task dependencies and its caching strategy |
| 131 | +4. **Clear Separation**: Each plugin has its own target for better debugging and maintainability |
0 commit comments