Skip to content

Commit c3a76a5

Browse files
committed
feat(compiler-sfc): enhance cache management with configurable options
1 parent 3b5e13c commit c3a76a5

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

packages/compiler-sfc/src/cache.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
import { LRUCache } from 'lru-cache'
22

3+
export const COMPILER_CACHE_KEYS = {
4+
parse: 'parse',
5+
templateUsageCheck: 'templateUsageCheck',
6+
tsConfig: 'tsConfig',
7+
fileToScope: 'fileToScope',
8+
} as const
9+
10+
type CacheKey = keyof typeof COMPILER_CACHE_KEYS
11+
type CacheOptions = Partial<
12+
Record<CacheKey, LRUCache.Options<string, any, unknown>>
13+
>
14+
15+
let cacheOptions: CacheOptions = Object.create(null)
16+
317
export function createCache<T extends {}>(
4-
max = 500,
18+
key: CacheKey,
519
): Map<string, T> | LRUCache<string, T> {
620
/* v8 ignore next 3 */
721
if (__GLOBAL__ || __ESM_BROWSER__) {
822
return new Map<string, T>()
923
}
10-
return new LRUCache({ max })
24+
return new LRUCache<string, T>(cacheOptions[key] || { max: 500 })
25+
}
26+
27+
/**
28+
* @private
29+
*/
30+
export function configureCacheOptions(options: CacheOptions = {}): void {
31+
cacheOptions = options
1132
}

packages/compiler-sfc/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export { invalidateTypeCache, registerTS } from './script/resolveType'
4444
export { extractRuntimeProps } from './script/defineProps'
4545
export { extractRuntimeEmits } from './script/defineEmits'
4646

47+
// Internals for cache control
48+
export { configureCacheOptions } from './cache'
49+
4750
// Types
4851
export type {
4952
SFCParseOptions,

packages/compiler-sfc/src/parse.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as CompilerDOM from '@vue/compiler-dom'
1414
import { SourceMapGenerator } from 'source-map-js'
1515
import type { TemplateCompiler } from './compileTemplate'
1616
import { parseCssVars } from './style/cssVars'
17-
import { createCache } from './cache'
17+
import { COMPILER_CACHE_KEYS, createCache } from './cache'
1818
import type { ImportBinding } from './compileScript'
1919
import { isUsedInTemplate } from './script/importUsageCheck'
2020
import type { LRUCache } from 'lru-cache'
@@ -104,7 +104,9 @@ export interface SFCParseResult {
104104

105105
export const parseCache:
106106
| Map<string, SFCParseResult>
107-
| LRUCache<string, SFCParseResult> = createCache<SFCParseResult>()
107+
| LRUCache<string, SFCParseResult> = createCache<SFCParseResult>(
108+
COMPILER_CACHE_KEYS.parse,
109+
)
108110

109111
export function parse(
110112
source: string,

packages/compiler-sfc/src/script/importUsageCheck.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
parserOptions,
88
walkIdentifiers,
99
} from '@vue/compiler-dom'
10-
import { createCache } from '../cache'
10+
import { COMPILER_CACHE_KEYS, createCache } from '../cache'
1111
import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
1212

1313
/**
@@ -23,7 +23,9 @@ export function isUsedInTemplate(
2323
return resolveTemplateUsedIdentifiers(sfc).has(identifier)
2424
}
2525

26-
const templateUsageCheckCache = createCache<Set<string>>()
26+
const templateUsageCheckCache = createCache<Set<string>>(
27+
COMPILER_CACHE_KEYS.templateUsageCheck,
28+
)
2729

2830
function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> {
2931
const { content, ast } = sfc.template!

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import type { ImportBinding, SFCScriptCompileOptions } from '../compileScript'
3737
import { capitalize, hasOwn } from '@vue/shared'
3838
import { parse as babelParse } from '@babel/parser'
3939
import { parse } from '../parse'
40-
import { createCache } from '../cache'
40+
import { COMPILER_CACHE_KEYS, createCache } from '../cache'
4141
import type TS from 'typescript'
4242
import { dirname, extname, join } from 'path'
4343
import { minimatch as isMatch } from 'minimatch'
@@ -999,7 +999,7 @@ interface CachedConfig {
999999
cache?: TS.ModuleResolutionCache
10001000
}
10011001

1002-
const tsConfigCache = createCache<CachedConfig[]>()
1002+
const tsConfigCache = createCache<CachedConfig[]>(COMPILER_CACHE_KEYS.tsConfig)
10031003
const tsConfigRefMap = new Map<string, string>()
10041004

10051005
function resolveWithTS(
@@ -1123,7 +1123,7 @@ function loadTSConfig(
11231123
return res
11241124
}
11251125

1126-
const fileToScopeCache = createCache<TypeScope>()
1126+
const fileToScopeCache = createCache<TypeScope>(COMPILER_CACHE_KEYS.fileToScope)
11271127

11281128
/**
11291129
* @private

0 commit comments

Comments
 (0)