Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions packages/compiler-sfc/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createCache } from './cache'
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'

Expand Down Expand Up @@ -103,24 +104,14 @@ export const parseCache:
| 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
Expand Down
9 changes: 9 additions & 0 deletions packages/shared/src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,12 @@ export function genPropsAccessExp(name: string): string {
? `__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,
)
)
}
10 changes: 8 additions & 2 deletions packages/vue-compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import {
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 {
Expand All @@ -35,7 +41,7 @@ function compileToFunction(
}
}

const key = template
const key = genCacheKey(template, options)
const cached = compileCache[key]
if (cached) {
return cached
Expand Down
23 changes: 5 additions & 18 deletions packages/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
} from '@vue/runtime-dom'
import * as runtimeDom from '@vue/runtime-dom'
import {
EMPTY_OBJ,
NOOP,
extend,
genCacheKey,
generateCodeFrame,
isString,
} from '@vue/shared'
Expand All @@ -25,19 +25,7 @@ if (__DEV__) {
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,
Expand All @@ -52,9 +40,8 @@ function compileToFunction(
}
}

const key = template
const cache = getCache(options)
const cached = cache[key]
const key = genCacheKey(template, options)
const cached = compileCache[key]
if (cached) {
return cached
}
Expand Down Expand Up @@ -111,7 +98,7 @@ function compileToFunction(
// mark the function as runtime compiled
;(render as InternalRenderFunction)._rc = true

return (cache[key] = render)
return (compileCache[key] = render)
}

registerRuntimeCompiler(compileToFunction)
Expand Down