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..71ffd79 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, @@ -208,6 +209,7 @@ function processTemplate (node, filePath, id, hasScopedStyle, fullSource) { ? getRawTemplate(node, fullSource) // custom template, extract as raw string : parse5.serialize(node.content) // normal HTML, use serialization ) + template = deindent(template) if (!lang) { var warnings = validateTemplate(node.content, fullSource) @@ -265,11 +267,12 @@ function getRawTemplate (node, source) { function processStyle (node, filePath, id) { var style = checkSrc(node, filePath) || parse5.serialize(node) var lang = checkLang(node) + style = deindent(style) return compileAsPromise('style', style, lang, filePath) .then(function (res) { return rewriteStyle(id, res.source, isScoped(node)) - }) + }); } /** @@ -284,6 +287,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 +296,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 + }) } /**