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
}
export { extractRuntimeProps } from './script/defineProps'
export { extractRuntimeEmits } from './script/defineEmits'
+// Internals for cache control
+export { configureCacheOptions } from './cache'
+
// Types
export type {
SFCParseOptions,
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'
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,
parserOptions,
walkIdentifiers,
} from '@vue/compiler-dom'
-import { createCache } from '../cache'
+import { COMPILER_CACHE_KEYS, createCache } from '../cache'
import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
/**
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!
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'
cache?: TS.ModuleResolutionCache
}
-const tsConfigCache = createCache<CachedConfig[]>()
+const tsConfigCache = createCache<CachedConfig[]>(COMPILER_CACHE_KEYS.tsConfig)
const tsConfigRefMap = new Map<string, string>()
function resolveWithTS(
return res
}
-const fileToScopeCache = createCache<TypeScope>()
+const fileToScopeCache = createCache<TypeScope>(COMPILER_CACHE_KEYS.fileToScope)
/**
* @private