diff --git a/docs/config/experimental.md b/docs/config/experimental.md index ab0107169..984763a8f 100644 --- a/docs/config/experimental.md +++ b/docs/config/experimental.md @@ -62,6 +62,34 @@ export default defineConfig({ 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. +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`: + +```js [vitest.config.js] +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + plugins: [ + { + name: 'vitest-cache', + api: { + vitest: { + experimental: { + ignoreFsModuleCache: true, + }, + }, + }, + }, + ], + test: { + experimental: { + fsModuleCache: true, + }, + }, +}) +``` + +Note that you can still define the cache key generator even the plugin opt-out of module caching. + ## experimental.fsModuleCachePath 4.0.11 {#experimental-fsmodulecachepath} - **Type:** `string` diff --git a/packages/vitest/src/node/cache/fsModuleCache.ts b/packages/vitest/src/node/cache/fsModuleCache.ts index 61289b238..7bd9c008a 100644 --- a/packages/vitest/src/node/cache/fsModuleCache.ts +++ b/packages/vitest/src/node/cache/fsModuleCache.ts @@ -1,6 +1,5 @@ import type { DevEnvironment, FetchResult } from 'vite' import type { Vitest } from '../core' -import type { VitestResolver } from '../resolver' import type { ResolvedConfig } from '../types/config' import fs, { existsSync, mkdirSync, readFileSync } from 'node:fs' import { readFile, rename, rm, stat, unlink, writeFile } from 'node:fs/promises' @@ -92,7 +91,7 @@ export class FileSystemModuleCache { | undefined > { if (!existsSync(cachedFilePath)) { - debugFs?.(`${c.red('[empty]')} ${cachedFilePath} doesn't exist, transforming by vite instead`) + debugFs?.(`${c.red('[empty]')} ${cachedFilePath} doesn't exist, transforming by vite first`) return } @@ -176,7 +175,6 @@ export class FileSystemModuleCache { generateCachePath( vitestConfig: ResolvedConfig, environment: DevEnvironment, - resolver: VitestResolver, id: string, fileContent: string, ): string | null { @@ -184,7 +182,7 @@ export class FileSystemModuleCache { // TODO: figure out a way to still support it if (fileContent.includes('import.meta.glob(')) { this.saveMemoryCache(environment, id, null) - debugMemory?.(`${c.yellow('[write]')} ${id} was bailed out`) + debugMemory?.(`${c.yellow('[write]')} ${id} was bailed out because it has "import.meta.glob"`) return null } @@ -218,7 +216,9 @@ export class FileSystemModuleCache { resolve: config.resolve, // plugins can have different options, so this is not the best key, // but we canot access the options because there is no standard API for it - plugins: config.plugins.map(p => p.name), + plugins: config.plugins + .filter(p => p.api?.vitest?.experimental?.ignoreFsModuleCache !== true) + .map(p => p.name), // in case local plugins change // configFileDependencies also includes configFile configFileDependencies: config.configFileDependencies.map(file => tryReadFileSync(file)), @@ -248,6 +248,7 @@ export class FileSystemModuleCache { let cacheRoot = this.fsCacheRoots.get(vitestConfig) if (cacheRoot == null) { cacheRoot = vitestConfig.experimental.fsModuleCachePath || this.rootCache + this.fsCacheRoots.set(vitestConfig, cacheRoot) if (!existsSync(cacheRoot)) { mkdirSync(cacheRoot, { recursive: true }) } diff --git a/packages/vitest/src/node/environments/fetchModule.ts b/packages/vitest/src/node/environments/fetchModule.ts index 525ed26a0..f50f610a0 100644 --- a/packages/vitest/src/node/environments/fetchModule.ts +++ b/packages/vitest/src/node/environments/fetchModule.ts @@ -191,7 +191,6 @@ class ModuleFetcher { return this.fsCache.generateCachePath( this.config, environment, - this.resolver, moduleGraphModule.id!, fileContent, )