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": "^7.0.21",
"postcss-selector-parser": "^6.0.2", "postcss-selector-parser": "^6.0.2",
"source-map": "^0.7.3" "source-map": "^0.7.3"
},
"devDependencies": {
"@types/lru-cache": "^5.1.0"
} }
} }

View File

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

View File

@ -1253,6 +1253,11 @@
dependencies: dependencies:
jest-diff "^24.3.0" jest-diff "^24.3.0"
"@types/lru-cache@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@types/lru-cache/-/lru-cache-5.1.0.tgz#57f228f2b80c046b4a1bd5cac031f81f207f4f03"
integrity sha512-RaE0B+14ToE4l6UqdarKPnXwVDuigfFv+5j9Dze/Nqr23yyuqdNvzcZi3xB+3Agvi5R4EOgAksfv3lXX4vBt9w==
"@types/minimatch@*": "@types/minimatch@*":
version "3.0.3" version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"