Comparing version 1.1.0 to 2.0.0
@@ -12,54 +12,53 @@ /* | ||
var $ = require('cheerio'), | ||
var cheerio = require('cheerio'), | ||
MD5 = require('MD5'), | ||
fs = require('fs'); | ||
exports.busted = function(fileContents, options) { | ||
var self = this; | ||
function loadAttribute(content) { | ||
if (content.name.toLowerCase() === 'link') { | ||
return content.attribs.href; | ||
} | ||
options.basePath = options.basePath || ""; | ||
if (options.type === 'timestamp') { | ||
self.timestamp = new Date().getTime(); | ||
if (content.name.toLowerCase() === 'script') { | ||
return content.attribs.src; | ||
} | ||
var protocolRegEx = /^http(s)?/, | ||
scripts = $(fileContents).find('script'), | ||
styles = $(fileContents).find('link[rel=stylesheet]'), | ||
data; | ||
throw "No content awaited in this step of process"; | ||
} | ||
// Loop the stylesheet hrefs | ||
for (var i = 0; i < styles.length; i++) { | ||
var origHref = styles[i].attribs.href, | ||
styleHref = styles[i].attribs.href.split("?")[0]; | ||
// Test for http(s) and don't cache bust if (assumed) served from CDN | ||
if (!protocolRegEx.test(styleHref)) { | ||
if (options.type === 'timestamp') { | ||
fileContents = fileContents.replace(origHref, styleHref + '?t=' + self.timestamp); | ||
} else { | ||
data = fs.readFileSync(options.basePath + styleHref).toString(); | ||
fileContents = fileContents.replace(origHref, styleHref + '?v=' + MD5(data)); | ||
} | ||
} | ||
} | ||
exports.busted = function(fileContents, options) { | ||
var self = this, $ = cheerio.load(fileContents); | ||
// Loop the script srcs | ||
for (var j = 0; j < scripts.length; j++) { | ||
var origSrc = scripts[j].attribs.src, | ||
scriptSrc = scripts[j].attribs.src.split("?")[0]; | ||
self.MD5 = function(fileContents, originalAttrValue, options) { | ||
var originalAttrValueWithoutCacheBusting = originalAttrValue.split("?")[0], | ||
hash = MD5(fs.readFileSync(options.basePath + originalAttrValueWithoutCacheBusting).toString()); | ||
return fileContents.replace(originalAttrValue, originalAttrValueWithoutCacheBusting + '?v=' + hash); | ||
}; | ||
self.timestamp = function(fileContents, originalAttrValue, options) { | ||
var originalAttrValueWithoutCacheBusting = originalAttrValue.split("?")[0]; | ||
return fileContents.replace(originalAttrValue, originalAttrValueWithoutCacheBusting + '?t=' + options.currentTimestamp); | ||
}; | ||
options = { | ||
basePath : options.basePath || "", | ||
type : options.type || "MD5", | ||
currentTimestamp : new Date().getTime() | ||
}; | ||
var protocolRegEx = /^http(s)?/, elements = $('script[src], link[rel=stylesheet][href]'); | ||
for (var i = 0, len = elements.length; i < len; i++) { | ||
var originalAttrValue = loadAttribute(elements[i]); | ||
// Test for http(s) and don't cache bust if (assumed) served from CDN | ||
if (!protocolRegEx.test(scriptSrc)) { | ||
if (options.type === 'timestamp') { | ||
fileContents = fileContents.replace(origSrc, scriptSrc + '?t=' + self.timestamp); | ||
} else { | ||
data = fs.readFileSync(options.basePath + scriptSrc).toString(); | ||
fileContents = fileContents.replace(origSrc, scriptSrc + '?v=' + MD5(data)); | ||
} | ||
if (!protocolRegEx.test(originalAttrValue)) { | ||
fileContents = self[options.type](fileContents, originalAttrValue, options); | ||
} | ||
} | ||
return fileContents; | ||
}; |
{ | ||
"name": "cachebust", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"main": "lib/cachebust.js", | ||
@@ -27,4 +27,4 @@ "description": "Append a query string to your assets to bust that cache!", | ||
"dependencies": { | ||
"MD5": "^1.2.1", | ||
"cheerio": "^0.17.0" | ||
"md5": "^2.0.0", | ||
"cheerio": "^0.19.0" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
9167
171
+ Addedmd5@^2.0.0
+ Addedboolbase@1.0.0(transitive)
+ Addedcheerio@0.19.0(transitive)
+ Addedcss-select@1.0.0(transitive)
+ Addedcss-what@1.0.0(transitive)
+ Addeddom-serializer@0.1.1(transitive)
+ Addeddomelementtype@1.3.1(transitive)
+ Addeddomhandler@2.3.0(transitive)
+ Addedhtmlparser2@3.8.3(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedlodash@3.10.1(transitive)
+ Addedmd5@2.3.0(transitive)
+ Addednth-check@1.0.2(transitive)
- RemovedMD5@^1.2.1
- RemovedCSSselect@0.4.1(transitive)
- RemovedCSSwhat@0.4.7(transitive)
- RemovedMD5@1.3.0(transitive)
- Removedcheerio@0.17.0(transitive)
- Removeddom-serializer@0.0.1(transitive)
- Removeddomelementtype@1.1.3(transitive)
- Removeddomhandler@2.2.1(transitive)
- Removedhtmlparser2@3.7.3(transitive)
- Removedlodash@2.4.2(transitive)
Updatedcheerio@^0.19.0