From d82695ab4aac8c5a0fc353220c6ce507dea72c0a Mon Sep 17 00:00:00 2001 From: wszerad Date: Mon, 11 Apr 2016 00:06:24 +0200 Subject: [PATCH 1/2] Add parts cache --- lib/cache.js | 35 +++++++++++++++++++++++++++++++++++ lib/compiler.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 lib/cache.js diff --git a/lib/cache.js b/lib/cache.js new file mode 100644 index 0000000..93debef --- /dev/null +++ b/lib/cache.js @@ -0,0 +1,35 @@ +var crypto = require('crypto') + +var Cache = function() { + this.cache = {} +} + +Cache.prototype.check = function(key, source) { + var cache = this.cache[key], + checksum = hash(source) + + if(cache && cache.checksum === checksum) { + return false + } + + return checksum +} + +Cache.prototype.get = function(key) { + return this.cache[key] +} + +Cache.prototype.set = function(key, checksum, data) { + return this.cache[key] = { + checksum: checksum, + data: data + } +} + +function hash(str) { + var hash = crypto.createHash('md5') + hash.update(str) + return hash.digest('hex') +} + +module.exports = new Cache() \ No newline at end of file diff --git a/lib/compiler.js b/lib/compiler.js index 84499d0..0721250 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -12,6 +12,7 @@ var chalk = require('chalk') var assign = require('object-assign') var deindent = require('de-indent') var Emitter = require('events').EventEmitter +var cache = require('./cache') var htmlMinifyOptions = { collapseWhitespace: true, @@ -203,11 +204,17 @@ function isScoped (node) { function processTemplate (node, filePath, id, hasScopedStyle, fullSource) { var lang = checkLang(node) + var key = filePath + ':template' var template = checkSrc(node, filePath) || ( lang ? getRawTemplate(node, fullSource) // custom template, extract as raw string : parse5.serialize(node.content) // normal HTML, use serialization ) + var checksum = cache.check(key, template) + if (!checksum) { + return cache.get(key).data + } + template = deindent(template) if (!lang) { var warnings = validateTemplate(node.content, fullSource) @@ -230,6 +237,7 @@ function processTemplate (node, filePath, id, hasScopedStyle, fullSource) { if (process.env.NODE_ENV === 'production') { res.source = require('html-minifier').minify(res.source, htmlMinifyOptions) } + cache.set(key, checksum, res) return res }) } @@ -265,11 +273,22 @@ function getRawTemplate (node, source) { function processStyle (node, filePath, id) { var style = checkSrc(node, filePath) || parse5.serialize(node) var lang = checkLang(node) + var key = filePath + ':style' + var checksum = cache.check(key, style) + + if (!checksum) { + return cache.get(key).data + } + style = deindent(style) return compileAsPromise('style', style, lang, filePath) .then(function (res) { return rewriteStyle(id, res.source, isScoped(node)) }) + .then(function (data) { + cache.set(key, checksum, data) + return data + }) } /** @@ -284,6 +303,8 @@ function processStyle (node, filePath, id) { function processScript (node, filePath, content) { var lang = checkLang(node) || 'babel' var script = checkSrc(node, filePath) + var key = filePath + ':script' + if (!script) { script = parse5.serialize(node) // pad the script to ensure correct line number for syntax errors @@ -291,8 +312,20 @@ function processScript (node, filePath, content) { var before = padContent(content.slice(0, location)) script = before + script } + + script = script.trimLeft() + var checksum = cache.check(key, script) + + if (!checksum) { + return cache.get(key).data + } + script = deindent(script) return compileAsPromise('script', script, lang, filePath) + .then(function(data) { + cache.set(key, checksum, data) + return data + }) } /** From b837ef74cda4a93f9c1d2d96d53571be51fc0748 Mon Sep 17 00:00:00 2001 From: wszerad Date: Sat, 16 Apr 2016 21:30:48 +0200 Subject: [PATCH 2/2] cache only for scripts --- lib/compiler.js | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 0721250..71ffd79 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -204,16 +204,11 @@ function isScoped (node) { function processTemplate (node, filePath, id, hasScopedStyle, fullSource) { var lang = checkLang(node) - var key = filePath + ':template' var template = checkSrc(node, filePath) || ( lang ? getRawTemplate(node, fullSource) // custom template, extract as raw string : parse5.serialize(node.content) // normal HTML, use serialization ) - var checksum = cache.check(key, template) - if (!checksum) { - return cache.get(key).data - } template = deindent(template) if (!lang) { @@ -237,7 +232,6 @@ function processTemplate (node, filePath, id, hasScopedStyle, fullSource) { if (process.env.NODE_ENV === 'production') { res.source = require('html-minifier').minify(res.source, htmlMinifyOptions) } - cache.set(key, checksum, res) return res }) } @@ -273,22 +267,12 @@ function getRawTemplate (node, source) { function processStyle (node, filePath, id) { var style = checkSrc(node, filePath) || parse5.serialize(node) var lang = checkLang(node) - var key = filePath + ':style' - var checksum = cache.check(key, style) - - if (!checksum) { - return cache.get(key).data - } - + style = deindent(style) return compileAsPromise('style', style, lang, filePath) .then(function (res) { return rewriteStyle(id, res.source, isScoped(node)) - }) - .then(function (data) { - cache.set(key, checksum, data) - return data - }) + }); } /**