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

Commit

Permalink
perf: better management of cache (#83)
Browse files Browse the repository at this point in the history
* better management of cache

* add caching for templates
  • Loading branch information
elevatebart committed Jan 23, 2019
1 parent c9627b3 commit bd91ba1
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 79 deletions.
3 changes: 0 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { ComponentDoc, Documentation } from './Documentation'
import { parseFile, parseSource as parseSourceLocal } from './parse'
import * as utils from './utils'

export { utils }

export { ComponentDoc }

Expand Down
4 changes: 3 additions & 1 deletion src/parse-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as bt from '@babel/types'
import { NodePath } from 'ast-types'
import buildParser from './babel-parser'
import { Documentation } from './Documentation'
import cacher from './utils/cacher'
import resolveExportedComponent from './utils/resolveExportedComponent'

// tslint:disable-next-line:no-var-requires
Expand All @@ -19,7 +20,8 @@ export default function parseScript(
options: { lang: 'ts' | 'js'; filePath: string },
) {
const plugins: ParserPlugin[] = options.lang === 'ts' ? ['typescript'] : ['flow']
const ast = recast.parse(source, { parser: buildParser({ plugins }) })

const ast = cacher(() => recast.parse(source, { parser: buildParser({ plugins }) }), source)
if (!ast) {
throw new Error(ERROR_MISSING_DEFINITION)
}
Expand Down
3 changes: 2 additions & 1 deletion src/parse-template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as pug from 'pug'
import { ASTElement, ASTNode, compile, SFCBlock } from 'vue-template-compiler'
import { Documentation } from './Documentation'
import cacher from './utils/cacher'

export interface TemplateParserOptions {
functional: boolean
Expand All @@ -23,7 +24,7 @@ export default function parseTemplate(
tpl.attrs && tpl.attrs.lang === 'pug'
? pug.render(tpl.content, { filename: filePath })
: tpl.content
const ast = compile(template, { comments: true }).ast
const ast = cacher(() => compile(template, { comments: true }).ast, template)
if (ast) {
traverse(ast, documentation, handlers, { functional: !!tpl.attrs.functional })
}
Expand Down
6 changes: 3 additions & 3 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as fs from 'fs'
import * as path from 'path'
import { SFCDescriptor } from 'vue-template-compiler'
import { parseComponent, SFCDescriptor } from 'vue-template-compiler'
import { Documentation } from './Documentation'
import parseScript from './parse-script'
import parseTemplate from './parse-template'
import handlers from './script-handlers'
import templateHandlers from './template-handlers'
import scfParser from './utils/sfc-parser'
import cacher from './utils/cacher'

const ERROR_EMPTY_DOCUMENT = 'The passed source is empty'

Expand Down Expand Up @@ -38,7 +38,7 @@ export function parseSource(source: string, filePath: string, documentation: Doc
}

if (singleFileComponent) {
parts = scfParser(source, filePath)
parts = cacher(() => parseComponent(source), source)
}

const scriptSource = parts ? (parts.script ? parts.script.content : undefined) : source
Expand Down
41 changes: 0 additions & 41 deletions src/utils/__tests__/__snapshots__/sfc-parser.ts.snap

This file was deleted.

22 changes: 0 additions & 22 deletions src/utils/__tests__/sfc-parser.ts

This file was deleted.

11 changes: 5 additions & 6 deletions src/utils/sfc-parser.ts → src/utils/cacher.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { parseComponent, SFCDescriptor } from 'vue-template-compiler'
// tslint:disable-next-line:no-var-requires
const LRUCache = require('lru-cache')
// tslint:disable-next-line:no-var-requires
const hash = require('hash-sum')

const cache = new LRUCache(250)

export default function scfParser(source: string, filename: string): SFCDescriptor {
const hashTmp = hash
const cacheKey = hashTmp(filename + source)
export default function<T>(creator: () => T, ...argsKey: string[]): T {
const cacheKey = hash(argsKey.join(''))

// source-map cache busting for hot-reloadded modules
let output: SFCDescriptor = cache.get(cacheKey)
let output: T = cache.get(cacheKey)
if (output) {
return output
}
output = parseComponent(source)
output = creator()
cache.set(cacheKey, output)
return output
}
1 change: 0 additions & 1 deletion src/utils/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/components/button/button.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const button = path.join(__dirname, './Button.vue')
let docButton: ComponentDoc

describe('tests button', () => {
beforeEach(done => {
beforeAll(done => {
docButton = parse(button)
done()
})
Expand Down

0 comments on commit bd91ba1

Please sign in to comment.