Skip to content

Commit a68f74e

Browse files
authored
feat(cache): add opt-out on a plugin level, fix internal root cache (#9154)
1 parent 122ff32 commit a68f74e

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

docs/config/experimental.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ export default defineConfig({
6262

6363
If you are a plugin author, consider defining a [cache key generator](/api/advanced/plugin#definecachekeygenerator) in your plugin if it can be registered with different options that affect the transform result.
6464

65+
On the other hand, if your plugin should not affect the cache key, you can opt-out by setting `api.vitest.experimental.ignoreFsModuleCache` to `true`:
66+
67+
```js [vitest.config.js]
68+
import { defineConfig } from 'vitest/config'
69+
70+
export default defineConfig({
71+
plugins: [
72+
{
73+
name: 'vitest-cache',
74+
api: {
75+
vitest: {
76+
experimental: {
77+
ignoreFsModuleCache: true,
78+
},
79+
},
80+
},
81+
},
82+
],
83+
test: {
84+
experimental: {
85+
fsModuleCache: true,
86+
},
87+
},
88+
})
89+
```
90+
91+
Note that you can still define the cache key generator even the plugin opt-out of module caching.
92+
6593
## experimental.fsModuleCachePath <Version type="experimental">4.0.11</Version> {#experimental-fsmodulecachepath}
6694

6795
- **Type:** `string`

packages/vitest/src/node/cache/fsModuleCache.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { DevEnvironment, FetchResult } from 'vite'
22
import type { Vitest } from '../core'
3-
import type { VitestResolver } from '../resolver'
43
import type { ResolvedConfig } from '../types/config'
54
import fs, { existsSync, mkdirSync, readFileSync } from 'node:fs'
65
import { readFile, rename, rm, stat, unlink, writeFile } from 'node:fs/promises'
@@ -92,7 +91,7 @@ export class FileSystemModuleCache {
9291
| undefined
9392
> {
9493
if (!existsSync(cachedFilePath)) {
95-
debugFs?.(`${c.red('[empty]')} ${cachedFilePath} doesn't exist, transforming by vite instead`)
94+
debugFs?.(`${c.red('[empty]')} ${cachedFilePath} doesn't exist, transforming by vite first`)
9695
return
9796
}
9897

@@ -176,15 +175,14 @@ export class FileSystemModuleCache {
176175
generateCachePath(
177176
vitestConfig: ResolvedConfig,
178177
environment: DevEnvironment,
179-
resolver: VitestResolver,
180178
id: string,
181179
fileContent: string,
182180
): string | null {
183181
// bail out if file has import.meta.glob because it depends on other files
184182
// TODO: figure out a way to still support it
185183
if (fileContent.includes('import.meta.glob(')) {
186184
this.saveMemoryCache(environment, id, null)
187-
debugMemory?.(`${c.yellow('[write]')} ${id} was bailed out`)
185+
debugMemory?.(`${c.yellow('[write]')} ${id} was bailed out because it has "import.meta.glob"`)
188186
return null
189187
}
190188

@@ -218,7 +216,9 @@ export class FileSystemModuleCache {
218216
resolve: config.resolve,
219217
// plugins can have different options, so this is not the best key,
220218
// but we canot access the options because there is no standard API for it
221-
plugins: config.plugins.map(p => p.name),
219+
plugins: config.plugins
220+
.filter(p => p.api?.vitest?.experimental?.ignoreFsModuleCache !== true)
221+
.map(p => p.name),
222222
// in case local plugins change
223223
// configFileDependencies also includes configFile
224224
configFileDependencies: config.configFileDependencies.map(file => tryReadFileSync(file)),
@@ -248,6 +248,7 @@ export class FileSystemModuleCache {
248248
let cacheRoot = this.fsCacheRoots.get(vitestConfig)
249249
if (cacheRoot == null) {
250250
cacheRoot = vitestConfig.experimental.fsModuleCachePath || this.rootCache
251+
this.fsCacheRoots.set(vitestConfig, cacheRoot)
251252
if (!existsSync(cacheRoot)) {
252253
mkdirSync(cacheRoot, { recursive: true })
253254
}

packages/vitest/src/node/environments/fetchModule.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ class ModuleFetcher {
191191
return this.fsCache.generateCachePath(
192192
this.config,
193193
environment,
194-
this.resolver,
195194
moduleGraphModule.id!,
196195
fileContent,
197196
)

0 commit comments

Comments
 (0)