chore: lazy call createCache

This commit is contained in:
daiwei 2025-09-25 16:26:23 +08:00
parent c3a76a5c39
commit a5786924c5
3 changed files with 38 additions and 18 deletions

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

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

View 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,25 +1129,34 @@ 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)
if (fileToScopeCache) {
fileToScopeCache.delete(filename)
}
if (tsConfigCache) {
tsConfigCache.delete(filename)
const affectedConfig = tsConfigRefMap.get(filename)
if (affectedConfig) tsConfigCache.delete(affectedConfig)
}
}
export function fileToScope(
ctx: TypeResolveContext,
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
}