Skip to content

Commit

Permalink
Merge pull request #11 from codebykenny/master
Browse files Browse the repository at this point in the history
Explicit Files & Preload type
  • Loading branch information
shinate authored May 13, 2019
2 parents 9261135 + a0b2379 commit 987cba0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ Add version number to js/css/image in HTML
'attr' : ['src', 'custom-src'] // String or Array, undefined this will use default. css: "href", js: ...
'key' : '_v',
'value' : '%DATE%',
'cover' : 1
'cover' : 1,
'files': ['build.js', /dependency.js/] // Array [{String|Regex}] of explicit files to append to
}
]
},
Expand Down
28 changes: 26 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,13 @@ function version(v) {
var DETECTION = {
css : /<link[^>]*rel=['"]?stylesheet['"]?[^>]*>/g,
js : /<script [^>]+>[^<]*<\/script>/g,
image: /<img [^>]+>/g
image: /<img [^>]+>/g,
preload: /<link[^>]*rel=['"]?preload['"]?[^>]*>/g
};

var DEFAULT_ATTR = {
css : 'href',
preload : 'href',
js : 'src',
image: 'src'
}
Expand Down Expand Up @@ -164,7 +166,6 @@ module.exports = function (options) {
}

function apply_append(content, config) {

var apList = [];
if (config['to']) {
if (config.to === 'all') {
Expand Down Expand Up @@ -216,6 +217,29 @@ module.exports = function (options) {
function appendto(content, k, v) {
this.attr = this['attr'] ? [].concat(this.attr) : [DEFAULT_ATTR[this.type]];
var sts = content.match(DETECTION[this.type]);

if (Array.isArray(sts) && sts.length && this['files']) {
var files = this.files;

sts = sts.filter(function(element) {
for (var i = 0; i < files.length; i++) {
var file = files[i];

if (typeof file == 'string') {
if (element.indexOf(file) > -1) {
return true;
}
} else if (file instanceof RegExp) {
if (element.match(file)) {
return true;
}
}
}

return false;
});
}

if (Array.isArray(sts) && sts.length) {
var regExp = new RegExp('(' + this.attr.join('|') + ')' + '=[\'"]?([^>\'"]*)[\'"]?', 'g');
sts.forEach(function (_s) {
Expand Down

0 comments on commit 987cba0

Please sign in to comment.