Skip to content
This repository has been archived by the owner on Dec 26, 2018. It is now read-only.

Add parts cache #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/cache.js
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 18 additions & 1 deletion lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
})
});
}

/**
Expand All @@ -284,15 +287,29 @@ 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
var location = content.indexOf(script)
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
})
}

/**
Expand Down