Skip to content
This repository was archived by the owner on Mar 8, 2019. It is now read-only.

Commit bd91ba1

Browse files
authored
perf: better management of cache (#83)
* better management of cache * add caching for templates
1 parent c9627b3 commit bd91ba1

File tree

9 files changed

+14
-79
lines changed

9 files changed

+14
-79
lines changed

src/main.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { ComponentDoc, Documentation } from './Documentation'
22
import { parseFile, parseSource as parseSourceLocal } from './parse'
3-
import * as utils from './utils'
4-
5-
export { utils }
63

74
export { ComponentDoc }
85

src/parse-script.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as bt from '@babel/types'
33
import { NodePath } from 'ast-types'
44
import buildParser from './babel-parser'
55
import { Documentation } from './Documentation'
6+
import cacher from './utils/cacher'
67
import resolveExportedComponent from './utils/resolveExportedComponent'
78

89
// tslint:disable-next-line:no-var-requires
@@ -19,7 +20,8 @@ export default function parseScript(
1920
options: { lang: 'ts' | 'js'; filePath: string },
2021
) {
2122
const plugins: ParserPlugin[] = options.lang === 'ts' ? ['typescript'] : ['flow']
22-
const ast = recast.parse(source, { parser: buildParser({ plugins }) })
23+
24+
const ast = cacher(() => recast.parse(source, { parser: buildParser({ plugins }) }), source)
2325
if (!ast) {
2426
throw new Error(ERROR_MISSING_DEFINITION)
2527
}

src/parse-template.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as pug from 'pug'
22
import { ASTElement, ASTNode, compile, SFCBlock } from 'vue-template-compiler'
33
import { Documentation } from './Documentation'
4+
import cacher from './utils/cacher'
45

56
export interface TemplateParserOptions {
67
functional: boolean
@@ -23,7 +24,7 @@ export default function parseTemplate(
2324
tpl.attrs && tpl.attrs.lang === 'pug'
2425
? pug.render(tpl.content, { filename: filePath })
2526
: tpl.content
26-
const ast = compile(template, { comments: true }).ast
27+
const ast = cacher(() => compile(template, { comments: true }).ast, template)
2728
if (ast) {
2829
traverse(ast, documentation, handlers, { functional: !!tpl.attrs.functional })
2930
}

src/parse.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
3-
import { SFCDescriptor } from 'vue-template-compiler'
3+
import { parseComponent, SFCDescriptor } from 'vue-template-compiler'
44
import { Documentation } from './Documentation'
55
import parseScript from './parse-script'
66
import parseTemplate from './parse-template'
77
import handlers from './script-handlers'
88
import templateHandlers from './template-handlers'
9-
import scfParser from './utils/sfc-parser'
9+
import cacher from './utils/cacher'
1010

1111
const ERROR_EMPTY_DOCUMENT = 'The passed source is empty'
1212

@@ -38,7 +38,7 @@ export function parseSource(source: string, filePath: string, documentation: Doc
3838
}
3939

4040
if (singleFileComponent) {
41-
parts = scfParser(source, filePath)
41+
parts = cacher(() => parseComponent(source), source)
4242
}
4343

4444
const scriptSource = parts ? (parts.script ? parts.script.content : undefined) : source

src/utils/__tests__/__snapshots__/sfc-parser.ts.snap

-41
This file was deleted.

src/utils/__tests__/sfc-parser.ts

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import { parseComponent, SFCDescriptor } from 'vue-template-compiler'
21
// tslint:disable-next-line:no-var-requires
32
const LRUCache = require('lru-cache')
43
// tslint:disable-next-line:no-var-requires
54
const hash = require('hash-sum')
65

76
const cache = new LRUCache(250)
87

9-
export default function scfParser(source: string, filename: string): SFCDescriptor {
10-
const hashTmp = hash
11-
const cacheKey = hashTmp(filename + source)
8+
export default function<T>(creator: () => T, ...argsKey: string[]): T {
9+
const cacheKey = hash(argsKey.join(''))
10+
1211
// source-map cache busting for hot-reloadded modules
13-
let output: SFCDescriptor = cache.get(cacheKey)
12+
let output: T = cache.get(cacheKey)
1413
if (output) {
1514
return output
1615
}
17-
output = parseComponent(source)
16+
output = creator()
1817
cache.set(cacheKey, output)
1918
return output
2019
}

src/utils/index.ts

-1
This file was deleted.

tests/components/button/button.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const button = path.join(__dirname, './Button.vue')
66
let docButton: ComponentDoc
77

88
describe('tests button', () => {
9-
beforeEach(done => {
9+
beforeAll(done => {
1010
docButton = parse(button)
1111
done()
1212
})

0 commit comments

Comments
 (0)