Skip to content

Commit fa0ba24

Browse files
authoredSep 26, 2024··
fix(vue): properly cache runtime compilation (#12019)
1 parent 4da6881 commit fa0ba24

File tree

4 files changed

+27
-34
lines changed

4 files changed

+27
-34
lines changed
 

‎packages/compiler-sfc/src/parse.ts

+5-14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { createCache } from './cache'
1818
import type { ImportBinding } from './compileScript'
1919
import { isImportUsed } from './script/importUsageCheck'
2020
import type { LRUCache } from 'lru-cache'
21+
import { genCacheKey } from '@vue/shared'
2122

2223
export const DEFAULT_FILENAME = 'anonymous.vue'
2324

@@ -103,24 +104,14 @@ export const parseCache:
103104
| Map<string, SFCParseResult>
104105
| LRUCache<string, SFCParseResult> = createCache<SFCParseResult>()
105106

106-
function genCacheKey(source: string, options: SFCParseOptions): string {
107-
return (
108-
source +
109-
JSON.stringify(
110-
{
111-
...options,
112-
compiler: { parse: options.compiler?.parse },
113-
},
114-
(_, val) => (typeof val === 'function' ? val.toString() : val),
115-
)
116-
)
117-
}
118-
119107
export function parse(
120108
source: string,
121109
options: SFCParseOptions = {},
122110
): SFCParseResult {
123-
const sourceKey = genCacheKey(source, options)
111+
const sourceKey = genCacheKey(source, {
112+
...options,
113+
compiler: { parse: options.compiler?.parse },
114+
})
124115
const cache = parseCache.get(sourceKey)
125116
if (cache) {
126117
return cache

‎packages/shared/src/general.ts

+9
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,12 @@ export function genPropsAccessExp(name: string): string {
208208
? `__props.${name}`
209209
: `__props[${JSON.stringify(name)}]`
210210
}
211+
212+
export function genCacheKey(source: string, options: any): string {
213+
return (
214+
source +
215+
JSON.stringify(options, (_, val) =>
216+
typeof val === 'function' ? val.toString() : val,
217+
)
218+
)
219+
}

‎packages/vue-compat/src/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import {
1212
registerRuntimeCompiler,
1313
warn,
1414
} from '@vue/runtime-dom'
15-
import { NOOP, extend, generateCodeFrame, isString } from '@vue/shared'
15+
import {
16+
NOOP,
17+
extend,
18+
genCacheKey,
19+
generateCodeFrame,
20+
isString,
21+
} from '@vue/shared'
1622
import type { InternalRenderFunction } from 'packages/runtime-core/src/component'
1723
import * as runtimeDom from '@vue/runtime-dom'
1824
import {
@@ -35,7 +41,7 @@ function compileToFunction(
3541
}
3642
}
3743

38-
const key = template
44+
const key = genCacheKey(template, options)
3945
const cached = compileCache[key]
4046
if (cached) {
4147
return cached

‎packages/vue/src/index.ts

+5-18
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
} from '@vue/runtime-dom'
1414
import * as runtimeDom from '@vue/runtime-dom'
1515
import {
16-
EMPTY_OBJ,
1716
NOOP,
1817
extend,
18+
genCacheKey,
1919
generateCodeFrame,
2020
isString,
2121
} from '@vue/shared'
@@ -25,19 +25,7 @@ if (__DEV__) {
2525
initDev()
2626
}
2727

28-
const compileCache = new WeakMap<
29-
CompilerOptions,
30-
Record<string, RenderFunction>
31-
>()
32-
33-
function getCache(options?: CompilerOptions) {
34-
let c = compileCache.get(options ?? EMPTY_OBJ)
35-
if (!c) {
36-
c = Object.create(null) as Record<string, RenderFunction>
37-
compileCache.set(options ?? EMPTY_OBJ, c)
38-
}
39-
return c
40-
}
28+
const compileCache: Record<string, RenderFunction> = Object.create(null)
4129

4230
function compileToFunction(
4331
template: string | HTMLElement,
@@ -52,9 +40,8 @@ function compileToFunction(
5240
}
5341
}
5442

55-
const key = template
56-
const cache = getCache(options)
57-
const cached = cache[key]
43+
const key = genCacheKey(template, options)
44+
const cached = compileCache[key]
5845
if (cached) {
5946
return cached
6047
}
@@ -111,7 +98,7 @@ function compileToFunction(
11198
// mark the function as runtime compiled
11299
;(render as InternalRenderFunction)._rc = true
113100

114-
return (cache[key] = render)
101+
return (compileCache[key] = render)
115102
}
116103

117104
registerRuntimeCompiler(compileToFunction)

0 commit comments

Comments
 (0)
Please sign in to comment.