diff --git a/packages/compiler-sfc/package.json b/packages/compiler-sfc/package.json index cf842880..013fc7e2 100644 --- a/packages/compiler-sfc/package.json +++ b/packages/compiler-sfc/package.json @@ -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" } } diff --git a/packages/compiler-sfc/src/parse.ts b/packages/compiler-sfc/src/parse.ts index de8945dd..7a5828d1 100644 --- a/packages/compiler-sfc/src/parse.ts +++ b/packages/compiler-sfc/src/parse.ts @@ -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(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 } diff --git a/yarn.lock b/yarn.lock index 04b04a87..7498feb0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1253,6 +1253,11 @@ dependencies: 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@*": version "3.0.3" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"