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)[] errors: (CompilerError | SyntaxError)[]
} }
export const parseCache: export let parseCache:
| Map<string, SFCParseResult> | Map<string, SFCParseResult>
| LRUCache<string, SFCParseResult> = createCache<SFCParseResult>( | LRUCache<string, SFCParseResult>
COMPILER_CACHE_KEYS.parse,
)
export function parse( export function parse(
source: string, source: string,
@ -116,7 +114,10 @@ export function parse(
...options, ...options,
compiler: { parse: options.compiler?.parse }, compiler: { parse: options.compiler?.parse },
}) })
const cache = parseCache.get(sourceKey) const cache = (
parseCache ||
(parseCache = createCache<SFCParseResult>(COMPILER_CACHE_KEYS.parse))
).get(sourceKey)
if (cache) { if (cache) {
return cache return cache
} }

View File

@ -9,6 +9,7 @@ import {
} from '@vue/compiler-dom' } from '@vue/compiler-dom'
import { COMPILER_CACHE_KEYS, createCache } from '../cache' import { COMPILER_CACHE_KEYS, createCache } from '../cache'
import { camelize, capitalize, isBuiltInDirective } from '@vue/shared' import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
import type { LRUCache } from 'lru-cache'
/** /**
* Check if an identifier is used in the SFC's template. * Check if an identifier is used in the SFC's template.
@ -23,13 +24,16 @@ export function isUsedInTemplate(
return resolveTemplateUsedIdentifiers(sfc).has(identifier) return resolveTemplateUsedIdentifiers(sfc).has(identifier)
} }
const templateUsageCheckCache = createCache<Set<string>>( let templateUsageCheckCache: LRUCache<string, Set<string>>
COMPILER_CACHE_KEYS.templateUsageCheck,
)
function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> { function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> {
const { content, ast } = sfc.template! 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) { if (cached) {
return cached return cached
} }

View File

@ -42,6 +42,7 @@ import type TS from 'typescript'
import { dirname, extname, join } from 'path' import { dirname, extname, join } from 'path'
import { minimatch as isMatch } from 'minimatch' import { minimatch as isMatch } from 'minimatch'
import * as process from 'process' import * as process from 'process'
import type { LRUCache } from 'lru-cache'
export type SimpleTypeResolveOptions = Partial< export type SimpleTypeResolveOptions = Partial<
Pick< Pick<
@ -999,7 +1000,7 @@ interface CachedConfig {
cache?: TS.ModuleResolutionCache cache?: TS.ModuleResolutionCache
} }
const tsConfigCache = createCache<CachedConfig[]>(COMPILER_CACHE_KEYS.tsConfig) let tsConfigCache: LRUCache<string, CachedConfig[], unknown>
const tsConfigRefMap = new Map<string, string>() const tsConfigRefMap = new Map<string, string>()
function resolveWithTS( function resolveWithTS(
@ -1018,7 +1019,12 @@ function resolveWithTS(
if (configPath) { if (configPath) {
let configs: CachedConfig[] let configs: CachedConfig[]
const normalizedConfigPath = normalizePath(configPath) 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) { if (!cached) {
configs = loadTSConfig(configPath, ts, fs).map(config => ({ config })) configs = loadTSConfig(configPath, ts, fs).map(config => ({ config }))
tsConfigCache.set(normalizedConfigPath, configs) tsConfigCache.set(normalizedConfigPath, configs)
@ -1123,17 +1129,21 @@ function loadTSConfig(
return res return res
} }
const fileToScopeCache = createCache<TypeScope>(COMPILER_CACHE_KEYS.fileToScope) let fileToScopeCache: LRUCache<string, TypeScope>
/** /**
* @private * @private
*/ */
export function invalidateTypeCache(filename: string): void { export function invalidateTypeCache(filename: string): void {
filename = normalizePath(filename) filename = normalizePath(filename)
fileToScopeCache.delete(filename) if (fileToScopeCache) {
tsConfigCache.delete(filename) fileToScopeCache.delete(filename)
const affectedConfig = tsConfigRefMap.get(filename) }
if (affectedConfig) tsConfigCache.delete(affectedConfig) if (tsConfigCache) {
tsConfigCache.delete(filename)
const affectedConfig = tsConfigRefMap.get(filename)
if (affectedConfig) tsConfigCache.delete(affectedConfig)
}
} }
export function fileToScope( export function fileToScope(
@ -1141,7 +1151,12 @@ export function fileToScope(
filename: string, filename: string,
asGlobal = false, asGlobal = false,
): TypeScope { ): TypeScope {
const cached = fileToScopeCache.get(filename) const cached = (
fileToScopeCache ||
(fileToScopeCache = createCache<TypeScope>(
COMPILER_CACHE_KEYS.fileToScope,
) as LRUCache<string, TypeScope>)
).get(filename)
if (cached) { if (cached) {
return cached return cached
} }
@ -1151,7 +1166,7 @@ export function fileToScope(
const body = parseFile(filename, source, fs, ctx.options.babelParserPlugins) const body = parseFile(filename, source, fs, ctx.options.babelParserPlugins)
const scope = new TypeScope(filename, source, 0, recordImports(body)) const scope = new TypeScope(filename, source, 0, recordImports(body))
recordTypes(ctx, body, scope, asGlobal) recordTypes(ctx, body, scope, asGlobal)
fileToScopeCache.set(filename, scope) fileToScopeCache!.set(filename, scope)
return scope return scope
} }