From ec715854ca12520b2afc9e9b3981cbae05ae5206 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Mon, 4 Dec 2023 08:43:30 +0000 Subject: [PATCH] fix(app): prevent template from being cached between apps with different options (#9724) close #9618 --- packages/vue/src/index.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts index 8215be7476e..f926f140a5e 100644 --- a/packages/vue/src/index.ts +++ b/packages/vue/src/index.ts @@ -4,14 +4,32 @@ import { initDev } from './dev' import { compile, CompilerOptions, CompilerError } from '@vue/compiler-dom' import { registerRuntimeCompiler, RenderFunction, warn } 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' if (__DEV__) { initDev() } -const compileCache: Record = Object.create(null) +const compileCache = new WeakMap< + CompilerOptions, + Record +>() + +function getCache(options?: CompilerOptions) { + let c = compileCache.get(options ?? EMPTY_OBJ) + if (!c) { + c = Object.create(null) as Record + compileCache.set(options ?? EMPTY_OBJ, c) + } + return c +} function compileToFunction( template: string | HTMLElement, @@ -27,7 +45,8 @@ function compileToFunction( } const key = template - const cached = compileCache[key] + const cache = getCache(options) + const cached = cache[key] if (cached) { return cached } @@ -84,7 +103,7 @@ function compileToFunction( // mark the function as runtime compiled ;(render as InternalRenderFunction)._rc = true - return (compileCache[key] = render) + return (cache[key] = render) } registerRuntimeCompiler(compileToFunction)