fix(app): prevent template from being cached between apps with different options (#9724)

close #9618
This commit is contained in:
Carlos Rodrigues 2023-12-04 08:43:30 +00:00 committed by GitHub
parent f2f5f763e2
commit ec715854ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 4 deletions

View File

@ -4,14 +4,32 @@ import { initDev } from './dev'
import { compile, CompilerOptions, CompilerError } from '@vue/compiler-dom' import { compile, CompilerOptions, CompilerError } from '@vue/compiler-dom'
import { registerRuntimeCompiler, RenderFunction, warn } from '@vue/runtime-dom' import { registerRuntimeCompiler, RenderFunction, warn } from '@vue/runtime-dom'
import * as runtimeDom from '@vue/runtime-dom' import * as runtimeDom from '@vue/runtime-dom'
import { isString, NOOP, generateCodeFrame, extend } from '@vue/shared' import {
isString,
NOOP,
generateCodeFrame,
extend,
EMPTY_OBJ
} from '@vue/shared'
import { InternalRenderFunction } from 'packages/runtime-core/src/component' import { InternalRenderFunction } from 'packages/runtime-core/src/component'
if (__DEV__) { if (__DEV__) {
initDev() initDev()
} }
const compileCache: Record<string, RenderFunction> = Object.create(null) 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
}
function compileToFunction( function compileToFunction(
template: string | HTMLElement, template: string | HTMLElement,
@ -27,7 +45,8 @@ function compileToFunction(
} }
const key = template const key = template
const cached = compileCache[key] const cache = getCache(options)
const cached = cache[key]
if (cached) { if (cached) {
return cached return cached
} }
@ -84,7 +103,7 @@ function compileToFunction(
// mark the function as runtime compiled // mark the function as runtime compiled
;(render as InternalRenderFunction)._rc = true ;(render as InternalRenderFunction)._rc = true
return (compileCache[key] = render) return (cache[key] = render)
} }
registerRuntimeCompiler(compileToFunction) registerRuntimeCompiler(compileToFunction)