feat(compiler-sfc): add cache for parsing sfc (#453)

This commit is contained in:
QuincyChen
2019-11-19 02:29:04 +08:00
committed by Evan You
parent 9e16ea3d30
commit 4e538ac465
3 changed files with 17 additions and 2 deletions

View File

@@ -35,5 +35,8 @@
"postcss": "^7.0.21",
"postcss-selector-parser": "^6.0.2",
"source-map": "^0.7.3"
},
"devDependencies": {
"@types/lru-cache": "^5.1.0"
}
}

View File

@@ -7,6 +7,7 @@ import {
SourceLocation
} from '@vue/compiler-core'
import { RawSourceMap } from 'source-map'
import LRUCache from 'lru-cache'
import { generateCodeFrame } from '@vue/shared'
export interface SFCParseOptions {
@@ -48,6 +49,8 @@ export interface SFCDescriptor {
customBlocks: SFCBlock[]
}
const SFC_CACHE_MAX_SIZE = 500
const sourceToSFC = new LRUCache<string, SFCDescriptor>(SFC_CACHE_MAX_SIZE)
export function parse(
source: string,
{
@@ -56,7 +59,11 @@ export function parse(
sourceRoot = ''
}: SFCParseOptions = {}
): SFCDescriptor {
// TODO check cache
const sourceKey = source + needMap + filename + sourceRoot
const cache = sourceToSFC.get(sourceKey)
if (cache) {
return cache
}
const sfc: SFCDescriptor = {
filename,
@@ -101,7 +108,7 @@ export function parse(
if (needMap) {
// TODO source map
}
// TODO set cache
sourceToSFC.set(sourceKey, sfc)
return sfc
}