import type { ImportBinding } from './compileScript'
import { isImportUsed } from './script/importUsageCheck'
import type { LRUCache } from 'lru-cache'
+import { genCacheKey } from '@vue/shared'
export const DEFAULT_FILENAME = 'anonymous.vue'
| Map<string, SFCParseResult>
| LRUCache<string, SFCParseResult> = createCache<SFCParseResult>()
-function genCacheKey(source: string, options: SFCParseOptions): string {
- return (
- source +
- JSON.stringify(
- {
- ...options,
- compiler: { parse: options.compiler?.parse },
- },
- (_, val) => (typeof val === 'function' ? val.toString() : val),
- )
- )
-}
-
export function parse(
source: string,
options: SFCParseOptions = {},
): SFCParseResult {
- const sourceKey = genCacheKey(source, options)
+ const sourceKey = genCacheKey(source, {
+ ...options,
+ compiler: { parse: options.compiler?.parse },
+ })
const cache = parseCache.get(sourceKey)
if (cache) {
return cache
? `__props.${name}`
: `__props[${JSON.stringify(name)}]`
}
+
+export function genCacheKey(source: string, options: any): string {
+ return (
+ source +
+ JSON.stringify(options, (_, val) =>
+ typeof val === 'function' ? val.toString() : val,
+ )
+ )
+}
registerRuntimeCompiler,
warn,
} from '@vue/runtime-dom'
-import { NOOP, extend, generateCodeFrame, isString } from '@vue/shared'
+import {
+ NOOP,
+ extend,
+ genCacheKey,
+ generateCodeFrame,
+ isString,
+} from '@vue/shared'
import type { InternalRenderFunction } from 'packages/runtime-core/src/component'
import * as runtimeDom from '@vue/runtime-dom'
import {
}
}
- const key = template
+ const key = genCacheKey(template, options)
const cached = compileCache[key]
if (cached) {
return cached
} from '@vue/runtime-dom'
import * as runtimeDom from '@vue/runtime-dom'
import {
- EMPTY_OBJ,
NOOP,
extend,
+ genCacheKey,
generateCodeFrame,
isString,
} from '@vue/shared'
initDev()
}
-const compileCache = new WeakMap<
- CompilerOptions,
- Record<string, RenderFunction>
->()
-
-function getCache(options?: CompilerOptions) {
- let c = compileCache.get(options ?? EMPTY_OBJ)
- if (!c) {
- c = Object.create(null) as Record<string, RenderFunction>
- compileCache.set(options ?? EMPTY_OBJ, c)
- }
- return c
-}
+const compileCache: Record<string, RenderFunction> = Object.create(null)
function compileToFunction(
template: string | HTMLElement,
}
}
- const key = template
- const cache = getCache(options)
- const cached = cache[key]
+ const key = genCacheKey(template, options)
+ const cached = compileCache[key]
if (cached) {
return cached
}
// mark the function as runtime compiled
;(render as InternalRenderFunction)._rc = true
- return (cache[key] = render)
+ return (compileCache[key] = render)
}
registerRuntimeCompiler(compileToFunction)