]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
chore: lazy call createCache edison/feat/LRUCacheControll 13926/head
authordaiwei <daiwei521@126.com>
Thu, 25 Sep 2025 08:26:23 +0000 (16:26 +0800)
committerdaiwei <daiwei521@126.com>
Thu, 25 Sep 2025 08:26:23 +0000 (16:26 +0800)
packages/compiler-sfc/src/parse.ts
packages/compiler-sfc/src/script/importUsageCheck.ts
packages/compiler-sfc/src/script/resolveType.ts

index ab173d25464323ce886dc4010fc77e663b298108..0e1d9547156c2865f7d3a7d370b0e51aa93794e6 100644 (file)
@@ -102,11 +102,9 @@ export interface SFCParseResult {
   errors: (CompilerError | SyntaxError)[]
 }
 
-export const parseCache:
+export let parseCache:
   | Map<string, SFCParseResult>
-  | LRUCache<string, SFCParseResult> = createCache<SFCParseResult>(
-  COMPILER_CACHE_KEYS.parse,
-)
+  | LRUCache<string, SFCParseResult>
 
 export function parse(
   source: string,
@@ -116,7 +114,10 @@ export function parse(
     ...options,
     compiler: { parse: options.compiler?.parse },
   })
-  const cache = parseCache.get(sourceKey)
+  const cache = (
+    parseCache ||
+    (parseCache = createCache<SFCParseResult>(COMPILER_CACHE_KEYS.parse))
+  ).get(sourceKey)
   if (cache) {
     return cache
   }
index e5ead66772d2c44f756fb274cf261e33ea991aa2..19c8c62e1d88f2e6d539c542d6a7df4e3e768d53 100644 (file)
@@ -9,6 +9,7 @@ import {
 } from '@vue/compiler-dom'
 import { COMPILER_CACHE_KEYS, createCache } from '../cache'
 import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
+import type { LRUCache } from 'lru-cache'
 
 /**
  * Check if an identifier is used in the SFC's template.
@@ -23,13 +24,16 @@ export function isUsedInTemplate(
   return resolveTemplateUsedIdentifiers(sfc).has(identifier)
 }
 
-const templateUsageCheckCache = createCache<Set<string>>(
-  COMPILER_CACHE_KEYS.templateUsageCheck,
-)
+let templateUsageCheckCache: LRUCache<string, Set<string>>
 
 function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> {
   const { content, ast } = sfc.template!
-  const cached = templateUsageCheckCache.get(content)
+  const cached = (
+    templateUsageCheckCache ||
+    (templateUsageCheckCache = createCache<Set<string>>(
+      COMPILER_CACHE_KEYS.templateUsageCheck,
+    ) as LRUCache<string, Set<string>>)
+  ).get(content)
   if (cached) {
     return cached
   }
index 6d17d32a54f99b0f613672fe4b3735187ba34a55..020eaf8da56fcba319a41e931620ef15e6970224 100644 (file)
@@ -42,6 +42,7 @@ import type TS from 'typescript'
 import { dirname, extname, join } from 'path'
 import { minimatch as isMatch } from 'minimatch'
 import * as process from 'process'
+import type { LRUCache } from 'lru-cache'
 
 export type SimpleTypeResolveOptions = Partial<
   Pick<
@@ -999,7 +1000,7 @@ interface CachedConfig {
   cache?: TS.ModuleResolutionCache
 }
 
-const tsConfigCache = createCache<CachedConfig[]>(COMPILER_CACHE_KEYS.tsConfig)
+let tsConfigCache: LRUCache<string, CachedConfig[], unknown>
 const tsConfigRefMap = new Map<string, string>()
 
 function resolveWithTS(
@@ -1018,7 +1019,12 @@ function resolveWithTS(
   if (configPath) {
     let configs: CachedConfig[]
     const normalizedConfigPath = normalizePath(configPath)
-    const cached = tsConfigCache.get(normalizedConfigPath)
+    const cached = (
+      tsConfigCache ||
+      (tsConfigCache = createCache<CachedConfig[]>(
+        COMPILER_CACHE_KEYS.tsConfig,
+      ) as LRUCache<string, CachedConfig[], unknown>)
+    ).get(normalizedConfigPath)
     if (!cached) {
       configs = loadTSConfig(configPath, ts, fs).map(config => ({ config }))
       tsConfigCache.set(normalizedConfigPath, configs)
@@ -1123,17 +1129,21 @@ function loadTSConfig(
   return res
 }
 
-const fileToScopeCache = createCache<TypeScope>(COMPILER_CACHE_KEYS.fileToScope)
+let fileToScopeCache: LRUCache<string, TypeScope>
 
 /**
  * @private
  */
 export function invalidateTypeCache(filename: string): void {
   filename = normalizePath(filename)
-  fileToScopeCache.delete(filename)
-  tsConfigCache.delete(filename)
-  const affectedConfig = tsConfigRefMap.get(filename)
-  if (affectedConfig) tsConfigCache.delete(affectedConfig)
+  if (fileToScopeCache) {
+    fileToScopeCache.delete(filename)
+  }
+  if (tsConfigCache) {
+    tsConfigCache.delete(filename)
+    const affectedConfig = tsConfigRefMap.get(filename)
+    if (affectedConfig) tsConfigCache.delete(affectedConfig)
+  }
 }
 
 export function fileToScope(
@@ -1141,7 +1151,12 @@ export function fileToScope(
   filename: string,
   asGlobal = false,
 ): TypeScope {
-  const cached = fileToScopeCache.get(filename)
+  const cached = (
+    fileToScopeCache ||
+    (fileToScopeCache = createCache<TypeScope>(
+      COMPILER_CACHE_KEYS.fileToScope,
+    ) as LRUCache<string, TypeScope>)
+  ).get(filename)
   if (cached) {
     return cached
   }
@@ -1151,7 +1166,7 @@ export function fileToScope(
   const body = parseFile(filename, source, fs, ctx.options.babelParserPlugins)
   const scope = new TypeScope(filename, source, 0, recordImports(body))
   recordTypes(ctx, body, scope, asGlobal)
-  fileToScopeCache.set(filename, scope)
+  fileToScopeCache!.set(filename, scope)
   return scope
 }