|
| 1 | +# Automatic Data Tracking |
| 2 | + |
| 3 | +Automatic data tracking is how Vite Task learns what inputs a task needs for caching outputs without explicit config. |
| 4 | + |
| 5 | +When you run a cache-enabled task, Vite Task observes the task's execution and records what files were read and written, as well as any metadata reported by the task. On the next run, Vite Task uses the recorded fingerprint to decide whether to replay the cache or run the task. |
| 6 | + |
| 7 | +Use this page when you need to understand why a task hits or misses the cache, or when you need to decide whether to add `input`, `output`, `env`, or `untrackedEnv` config. |
| 8 | + |
| 9 | +## Tracking Tiers |
| 10 | + |
| 11 | +Automatic data tracking has two tiers: |
| 12 | + |
| 13 | +| Tier | Applies to | Records | |
| 14 | +| -------------------- | ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 15 | +| File system tracking | All tasks with cache enabled | <ul><li>Files read by the command</li><li>Missing-file probes</li><li>Directory listings</li><li>Written output files</li></ul> | |
| 16 | +| Cooperative tracking | Cache-reporting tools (`vp build` today) | <ul><li>Environment variables reported by the tool</li><li>Tool-managed paths that should not be inputs or outputs, such as `node_modules/.vite-temp`</li></ul> | |
| 17 | + |
| 18 | +Vite Task starts with file system tracking for any command. A cache-reporting tool can add information that only the tool knows while it runs. |
| 19 | + |
| 20 | +## File System Tracking |
| 21 | + |
| 22 | +File system tracking applies to every cache-enabled task. If you omit [`input`](/config/run#input), Vite Task tracks the files a command reads while it runs: |
| 23 | + |
| 24 | +```ts [vite.config.ts] |
| 25 | +import { defineConfig } from 'vite-plus'; |
| 26 | + |
| 27 | +export default defineConfig({ |
| 28 | + run: { |
| 29 | + tasks: { |
| 30 | + build: { |
| 31 | + command: 'tsc', |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +For this task, Vite Task records source files, config files, missing files the command checked, and directories the command scanned. Subsequent runs re-run the task when one of those tracked inputs changes. |
| 39 | + |
| 40 | +File system tracking also tracks outputs. If you omit [`output`](/config/run#output), Vite Task archives files the command writes after a successful run and restores them on a cache hit. |
| 41 | + |
| 42 | +### Limitations |
| 43 | + |
| 44 | +Vite Task cannot track environment variable reads, and it cannot always tell which tracked paths are stable inputs, generated outputs, or tool-managed cache paths that should not become inputs or outputs. |
| 45 | + |
| 46 | +Use [Override Inputs And Outputs](#override-inputs-and-outputs) when file system tracking includes files that should not affect the cache, misses files that should, or restores the wrong outputs. |
| 47 | + |
| 48 | +Use [`env`](/config/run#env) when a command needs an environment variable and the value should affect the cache, or [`untrackedEnv`](/config/run#untrackedenv) when the value should not affect the cache. |
| 49 | + |
| 50 | +These limitations do not apply to `vp build`: Vite reports [Cooperative Tracking](#cooperative-tracking) metadata automatically, including `VITE_*`, `NODE_ENV`, and Vite-managed cache paths that should not become inputs or outputs. A standard `vp build` task does not need manual `input`, `output`, or `env`. |
| 51 | + |
| 52 | +### Override Inputs And Outputs |
| 53 | + |
| 54 | +[`input`](/config/run#input) controls what invalidates the cache. [`output`](/config/run#output) controls which files Vite Task restores on a cache hit. |
| 55 | + |
| 56 | +Both options use the same syntax and can be configured separately. |
| 57 | + |
| 58 | +- Omit the option to keep automatic tracking. |
| 59 | +- Add `{ auto: true }` to keep automatic tracking while adding glob rules. |
| 60 | +- Use string globs to include paths. |
| 61 | +- Use `!` globs to exclude paths. |
| 62 | +- Use `[]` to replace automatic tracking with an empty list. |
| 63 | + |
| 64 | +```ts [vite.config.ts] |
| 65 | +tasks: { |
| 66 | + build: { |
| 67 | + command: 'node build.mjs', |
| 68 | + |
| 69 | + // Keep automatic input tracking, but exclude `dist` from inputs. |
| 70 | + input: [{ auto: true }, '!dist/**'], |
| 71 | + |
| 72 | + // Disable automatic output tracking and restore only `dist/**` on a cache hit. |
| 73 | + output: ['dist/**'], |
| 74 | + }, |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Use explicit `input` globs only when you know the command's full input set. This lint task overrides inputs only, so output tracking stays automatic: |
| 79 | + |
| 80 | +```ts [vite.config.ts] |
| 81 | +tasks: { |
| 82 | + lint: { |
| 83 | + command: 'vp lint', |
| 84 | + // Disable automatic input tracking and fingerprint only these files. |
| 85 | + input: ['src/**', 'vite.config.ts'], |
| 86 | + }, |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +Set `input: []` when no files should affect the cache fingerprint. This is rarely useful. For example, a download task can be cached when the same URL always serves the same file. No input files should be fingerprinted for this task, but changing the URL still invalidates the cache: |
| 91 | + |
| 92 | +```ts [vite.config.ts] |
| 93 | +tasks: { |
| 94 | + downloadSchema: { |
| 95 | + command: 'curl -O https://example.com/schema.json', |
| 96 | + input: [], |
| 97 | + }, |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Set `output: []` when no files should be restored on a cache hit. |
| 102 | + |
| 103 | +## Cooperative Tracking |
| 104 | + |
| 105 | +File system tracking records access. It cannot know why a tool used each path. |
| 106 | + |
| 107 | +`vp build` knows more about a Vite build than Vite Task can infer from file access. When `vp build` runs with cache enabled, Vite reports that metadata to Vite Task. Vite Task merges the report with file system tracking to build a more accurate cache fingerprint. |
| 108 | + |
| 109 | +For a standard Vite build, you do not need to add these entries yourself because Vite reports them automatically at runtime: |
| 110 | + |
| 111 | +- `env: ['VITE_*']` or `env: ['NODE_ENV']` |
| 112 | +- `output: ['dist/**']` |
| 113 | +- input or output exclusions for temporary paths like `node_modules/.vite-temp` |
| 114 | + |
| 115 | +You only need to define the task with `vp build`: |
| 116 | + |
| 117 | +```ts [vite.config.ts] |
| 118 | +import { defineConfig } from 'vite-plus'; |
| 119 | + |
| 120 | +export default defineConfig({ |
| 121 | + run: { |
| 122 | + tasks: { |
| 123 | + frontendBuild: 'vp build', |
| 124 | + }, |
| 125 | + }, |
| 126 | +}); |
| 127 | +``` |
| 128 | + |
| 129 | +Run this task with `vpr frontendBuild` or `vp run frontendBuild`. |
| 130 | + |
| 131 | +Manual config overrides reported metadata. Add `input`, `output`, `env`, or `untrackedEnv` when your project has behavior that Vite cannot report. |
| 132 | + |
| 133 | +Vite+ supports cooperative tracking for `vp build` today. It will extend this support to more first-party tools in the future. Third-party tools can report cache metadata with [`@voidzero-dev/vite-task-client`](https://npmx.dev/package/@voidzero-dev/vite-task-client). |
| 134 | + |
| 135 | +## When To Add Manual Config |
| 136 | + |
| 137 | +Add config when your project has behavior the command or tool cannot know. |
| 138 | + |
| 139 | +| Case | Example | |
| 140 | +| ----------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- | |
| 141 | +| Exclude an output directory from inputs | `input: [{ auto: true }, '!dist/**']` | |
| 142 | +| Exclude a temporary generated file from input and output tracking | `input: [{ auto: true }, '!.tmp/config.mjs']`<br>`output: [{ auto: true }, '!.tmp/config.mjs']` | |
| 143 | +| Avoid automatic file tracking for a task | `input: ['src/**']`<br>`output: ['dist/**']` | |
| 144 | +| Track and pass an env var | `env: ['NODE_ENV']` | |
| 145 | +| Pass an env var without fingerprinting it | `untrackedEnv: ['GITHUB_ACTIONS']` | |
0 commit comments