@@ -39,6 +39,144 @@ For this task, Vite Task records source files, config files, missing files the c |
39 | 39 |
|
40 | 40 | File system tracking also tracks outputs. If you omit [`cache.output`](/config/run#cache-output), Vite Task archives files the command writes after a successful run and restores them on a cache hit. |
41 | 41 |
|
| 42 | +### Rebuildable Inputs |
| 43 | + |
| 44 | +Some tools read files they also write: |
| 45 | + |
| 46 | +- With [`exports`](https://tsdown.dev/options/package-exports) enabled, tsdown reads `package.json` and writes the generated `exports` field back into it. |
| 47 | +- Many build tools list `dist` to delete the old build before they write a new one there. |
| 48 | +- Tools with their own cache read it and write it back, as `tsc --incremental` does with `tsconfig.tsbuildinfo`. |
| 49 | + |
| 50 | +This section explains how Vite Task handles files and folders that a task both reads and writes. |
| 51 | + |
| 52 | +Vite Task calls these files and folders **rebuildable**: the task produces them itself, so they don't have to exist before it runs. A task can rebuild: |
| 53 | + |
| 54 | +- Files it writes, such as `dist/index.mjs` or the `package.json` that tsdown updates. |
| 55 | +- Folders where the task wrote everything inside, such as `dist`. |
| 56 | + |
| 57 | +::: warning |
| 58 | +Rebuildable inputs assume that a task doesn't keep changing its outputs when its inputs stay the same. |
| 59 | + |
| 60 | +For example, a script that appends data to a file on every run breaks this assumption, and Vite Task can't detect it. Vite Task caches the file as the script left it, so the next run is a cache hit that restores the file without appending anything. |
| 61 | +::: |
| 62 | + |
| 63 | +After the task finishes, Vite Task fingerprints its inputs as the task left them. On the next run: |
| 64 | + |
| 65 | +- A rebuildable input may be missing, as on a fresh clone or after you delete it. The cache still hits and restores it with the task's outputs. |
| 66 | +- A rebuildable file that exists must match what the task left. If something else changed it, the cache misses. |
| 67 | +- Inputs that aren't rebuildable, such as your source files, are still checked strictly. Deleting or editing one causes a cache miss. |
| 68 | + |
| 69 | +#### Example: A Task Rewriting Sources |
| 70 | + |
| 71 | +```ts [vite.config.ts] |
| 72 | +tasks: { |
| 73 | + build: { |
| 74 | + // `--exports` makes tsdown write the generated `exports` field into package.json. |
| 75 | + command: 'tsdown --exports', |
| 76 | + }, |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +``` |
| 81 | +my-lib/ |
| 82 | +├── src/ |
| 83 | +│ └── index.ts # input: tsdown reads it |
| 84 | +└── package.json # rebuildable: tsdown reads it, then writes "exports" into it |
| 85 | +``` |
| 86 | + |
| 87 | +On the next run, Vite Task compares `package.json` with the file tsdown wrote, so the `exports` field that tsdown added doesn't cause a cache miss: |
| 88 | + |
| 89 | +```bash |
| 90 | +$ vp run build # first run: tsdown adds "exports" to package.json |
| 91 | +$ vp run build # cache hit |
| 92 | +``` |
| 93 | + |
| 94 | +Editing `package.json` yourself still causes a cache miss, as it should, because tsdown has to run again with your changes: |
| 95 | + |
| 96 | +```bash |
| 97 | +$ vp pm pkg set version=1.1.0 |
| 98 | +$ vp run build # cache miss: 'package.json' modified |
| 99 | +``` |
| 100 | + |
| 101 | +#### Example: A Task Listing Its Output Folder For Deletion |
| 102 | + |
| 103 | +```ts [vite.config.ts] |
| 104 | +tasks: { |
| 105 | + build: { |
| 106 | + // `--clean` deletes the old build in `dist` before tsdown writes a new one. |
| 107 | + command: 'tsdown --clean', |
| 108 | + }, |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +``` |
| 113 | +my-lib/ |
| 114 | +├── src/ |
| 115 | +│ └── index.ts # input |
| 116 | +├── package.json # input |
| 117 | +└── dist/ # rebuildable: the task wrote everything inside |
| 118 | + └── index.mjs # rebuildable: the task writes it |
| 119 | +``` |
| 120 | + |
| 121 | +Listing `dist` makes it an input. Since the task wrote everything inside `dist`, the whole folder is rebuildable: |
| 122 | + |
| 123 | +```bash |
| 124 | +$ vp run build # first run: tsdown writes dist/ |
| 125 | +$ rm -rf dist # as on a fresh clone |
| 126 | +$ vp run build # cache hit: dist/ is restored |
| 127 | +``` |
| 128 | + |
| 129 | +You don't need `'!dist/**'` in `cache.input` to keep `dist` out of the task's inputs, even though the task lists `dist` to delete the old build and then writes a new one. |
| 130 | + |
| 131 | +#### Example: A Task Reading And Writing Its Own Cache |
| 132 | + |
| 133 | +```ts [vite.config.ts] |
| 134 | +tasks: { |
| 135 | + typecheck: { |
| 136 | + command: 'tsc --incremental --noEmit', |
| 137 | + }, |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +``` |
| 142 | +my-app/ |
| 143 | +├── src/ |
| 144 | +│ └── main.ts # input: tsc reads it |
| 145 | +├── tsconfig.json # input: tsc reads it |
| 146 | +└── tsconfig.tsbuildinfo # rebuildable: tsc reads its cache from it, then writes it back |
| 147 | +``` |
| 148 | + |
| 149 | +Vite Task compares `tsconfig.tsbuildinfo` with the file tsc wrote, so tsc's updates to it don't cause a cache miss. If the file is missing, as on a fresh clone, a cache hit restores it: |
| 150 | + |
| 151 | +```bash |
| 152 | +$ vp run typecheck # first run: tsc writes tsconfig.tsbuildinfo |
| 153 | +$ vp run typecheck # cache hit |
| 154 | +$ rm tsconfig.tsbuildinfo # as on a fresh clone |
| 155 | +$ vp run typecheck # cache hit: tsconfig.tsbuildinfo is restored |
| 156 | +``` |
| 157 | + |
| 158 | +#### Avoid Caching Tasks That Modify Files In Place |
| 159 | + |
| 160 | +A task that rewrites your source files makes them rebuildable. If a cached `vp fmt` rewrites every file in `src/utils`, the whole folder becomes rebuildable: |
| 161 | + |
| 162 | +``` |
| 163 | +src/ |
| 164 | +└── utils/ # rebuildable: vp fmt rewrote everything inside |
| 165 | + ├── a.ts # rebuildable: vp fmt rewrote it |
| 166 | + └── b.ts # rebuildable: vp fmt rewrote it |
| 167 | +``` |
| 168 | + |
| 169 | +Deleting `src/utils` is then a cache hit that writes the folder back. |
| 170 | + |
| 171 | +Turn off auto-fix in the lint and format tasks you cache: |
| 172 | + |
| 173 | +```ts [vite.config.ts] |
| 174 | +tasks: { |
| 175 | + lint: 'vp lint', // instead of `vp lint --fix` |
| 176 | + format: 'vp fmt --check', // instead of `vp fmt` |
| 177 | +} |
| 178 | +``` |
| 179 | + |
42 | 180 | ### Limitations |
43 | 181 |
|
44 | 182 | 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. |
|