]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler-sfc): enhance cache management with configurable options
authordaiwei <daiwei521@126.com>
Thu, 25 Sep 2025 03:27:38 +0000 (11:27 +0800)
committerdaiwei <daiwei521@126.com>
Thu, 25 Sep 2025 03:59:34 +0000 (11:59 +0800)
packages/compiler-sfc/src/cache.ts
packages/compiler-sfc/src/index.ts
packages/compiler-sfc/src/parse.ts
packages/compiler-sfc/src/script/importUsageCheck.ts
packages/compiler-sfc/src/script/resolveType.ts

index 0bedc7b4d35f426e6c1b19d6c965cc9fe16c2fd1..abc81274d193980a5ae7d3a9a8447edd1c825e1a 100644 (file)
@@ -1,11 +1,32 @@
 import { LRUCache } from 'lru-cache'
 
+export const COMPILER_CACHE_KEYS = {
+  parse: 'parse',
+  templateUsageCheck: 'templateUsageCheck',
+  tsConfig: 'tsConfig',
+  fileToScope: 'fileToScope',
+} as const
+
+type CacheKey = keyof typeof COMPILER_CACHE_KEYS
+type CacheOptions = Partial<
+  Record<CacheKey, LRUCache.Options<string, any, unknown>>
+>
+
+let cacheOptions: CacheOptions = Object.create(null)
+
 export function createCache<T extends {}>(
-  max = 500,
+  key: CacheKey,
 ): Map<string, T> | LRUCache<string, T> {
   /* v8 ignore next 3 */
   if (__GLOBAL__ || __ESM_BROWSER__) {
     return new Map<string, T>()
   }
-  return new LRUCache({ max })
+  return new LRUCache<string, T>(cacheOptions[key] || { max: 500 })
+}
+
+/**
+ * @private
+ */
+export function configureCacheOptions(options: CacheOptions = {}): void {
+  cacheOptions = options
 }
index 5123a908976135978646c2f9d16b7d66f13b69d8..f890122b7b100fbe62aa4db03a0b8ce82de616b3 100644 (file)
@@ -44,6 +44,9 @@ export { invalidateTypeCache, registerTS } from './script/resolveType'
 export { extractRuntimeProps } from './script/defineProps'
 export { extractRuntimeEmits } from './script/defineEmits'
 
+// Internals for cache control
+export { configureCacheOptions } from './cache'
+
 // Types
 export type {
   SFCParseOptions,
index 9172cfc67ff31dea77991826a94baa8246053ce1..ab173d25464323ce886dc4010fc77e663b298108 100644 (file)
@@ -14,7 +14,7 @@ import * as CompilerDOM from '@vue/compiler-dom'
 import { SourceMapGenerator } from 'source-map-js'
 import type { TemplateCompiler } from './compileTemplate'
 import { parseCssVars } from './style/cssVars'
-import { createCache } from './cache'
+import { COMPILER_CACHE_KEYS, createCache } from './cache'
 import type { ImportBinding } from './compileScript'
 import { isUsedInTemplate } from './script/importUsageCheck'
 import type { LRUCache } from 'lru-cache'
@@ -104,7 +104,9 @@ export interface SFCParseResult {
 
 export const parseCache:
   | Map<string, SFCParseResult>
-  | LRUCache<string, SFCParseResult> = createCache<SFCParseResult>()
+  | LRUCache<string, SFCParseResult> = createCache<SFCParseResult>(
+  COMPILER_CACHE_KEYS.parse,
+)
 
 export function parse(
   source: string,
index 6fc16db446b8981205bd5e80f0ccd8f2f9f7f0ac..e5ead66772d2c44f756fb274cf261e33ea991aa2 100644 (file)
@@ -7,7 +7,7 @@ import {
   parserOptions,
   walkIdentifiers,
 } from '@vue/compiler-dom'
-import { createCache } from '../cache'
+import { COMPILER_CACHE_KEYS, createCache } from '../cache'
 import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
 
 /**
@@ -23,7 +23,9 @@ export function isUsedInTemplate(
   return resolveTemplateUsedIdentifiers(sfc).has(identifier)
 }
 
-const templateUsageCheckCache = createCache<Set<string>>()
+const templateUsageCheckCache = createCache<Set<string>>(
+  COMPILER_CACHE_KEYS.templateUsageCheck,
+)
 
 function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> {
   const { content, ast } = sfc.template!
index d8f430700502922f69767112b975121b6d1881ac..6d17d32a54f99b0f613672fe4b3735187ba34a55 100644 (file)
@@ -37,7 +37,7 @@ import type { ImportBinding, SFCScriptCompileOptions } from '../compileScript'
 import { capitalize, hasOwn } from '@vue/shared'
 import { parse as babelParse } from '@babel/parser'
 import { parse } from '../parse'
-import { createCache } from '../cache'
+import { COMPILER_CACHE_KEYS, createCache } from '../cache'
 import type TS from 'typescript'
 import { dirname, extname, join } from 'path'
 import { minimatch as isMatch } from 'minimatch'
@@ -999,7 +999,7 @@ interface CachedConfig {
   cache?: TS.ModuleResolutionCache
 }
 
-const tsConfigCache = createCache<CachedConfig[]>()
+const tsConfigCache = createCache<CachedConfig[]>(COMPILER_CACHE_KEYS.tsConfig)
 const tsConfigRefMap = new Map<string, string>()
 
 function resolveWithTS(
@@ -1123,7 +1123,7 @@ function loadTSConfig(
   return res
 }
 
-const fileToScopeCache = createCache<TypeScope>()
+const fileToScopeCache = createCache<TypeScope>(COMPILER_CACHE_KEYS.fileToScope)
 
 /**
  * @private