Sfoglia il codice sorgente

feat(compiler-sfc): enhance cache management with configurable options

daiwei 7 mesi fa
parent
commit
c3a76a5c39

+ 23 - 2
packages/compiler-sfc/src/cache.ts

@@ -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
 }

+ 3 - 0
packages/compiler-sfc/src/index.ts

@@ -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,

+ 4 - 2
packages/compiler-sfc/src/parse.ts

@@ -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,

+ 4 - 2
packages/compiler-sfc/src/script/importUsageCheck.ts

@@ -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!

+ 3 - 3
packages/compiler-sfc/src/script/resolveType.ts

@@ -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