Skip to content

Instantly share code, notes, and snippets.

@sshadmand
Created September 4, 2015 14:14
Show Gist options
  • Save sshadmand/144f6cd9a10708826390 to your computer and use it in GitHub Desktop.
Save sshadmand/144f6cd9a10708826390 to your computer and use it in GitHub Desktop.
Gulp Compiler Helpers
/*
Based on Cachebust
Fixes issue where the original cachebust doesn't see custom polymer elements. Ignores bower_components where polymer elements reside which will cause polymer issues.
*/
var $ = require('cheerio'),
MD5 = require('MD5');
exports.busted = function(fileContents, options) {
/* For elements in Polymer. Since Polymer
* doesn't use body or head tags (the $styles tag doesn't catch it) */
var contents = $.load(fileContents);
var links = contents("link");
for (i = 0; i < links.length; i++) {
var href = links[i].attribs.href;
if ( href.indexOf("?t=") === -1 && href.indexOf("bower_components") === -1) {
if (options.type === 'timestamp') {
fileContents = fileContents.replace(href, href + '?t=' + timestamp);
} else if (options.type === 'hash') {
fileContents = fileContents.replace(href, href + '?hash=' + MD5(fileContents));
} else {
fileContents = fileContents.replace(href, href + '?cbust=' + options.id);
}
}
}
return fileContents;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment