gulp-combo
Advanced tools
Comparing version 0.1.4 to 0.1.5
207
index.js
@@ -6,97 +6,150 @@ 'use strict'; | ||
module.exports = function(baseUri, options) { | ||
baseUri = baseUri || 'http://mc.meituan.net/combo/?f='; | ||
options = options || {}; | ||
baseUri = baseUri || 'http://mc.meituan.net/combo/?f='; | ||
options = options || {}; | ||
return through.obj(function(file, enc, cb) { | ||
var $ = cheerio.load(String(file.contents)); | ||
var scripts, links, results; | ||
var filteredScripts = [], filteredLinks = []; | ||
var src = { | ||
scripts: [], | ||
links: [] | ||
}; | ||
return through.obj(function(file, enc, cb) { | ||
/* | ||
var $ = cheerio.load(String(file.contents)); | ||
var scripts, links, results; | ||
var filteredScripts = [], filteredLinks = []; | ||
*/ | ||
var genComboScriptUriTag = function() { | ||
var uri = baseUri + src.scripts.join(options.splitter || ';'); | ||
var scriptTag = '<script type="text/javascript" src="' + uri + '"></script>'; | ||
var async = options.async || false; | ||
var chunk = String(file.contents); | ||
var src = { | ||
scripts: [], | ||
links: [] | ||
}; | ||
if(async === true) { | ||
scriptTag = '<script type="text/javascript" src="' + uri + '" async="async"></script>'; | ||
} | ||
var genComboScriptUriTag = function() { | ||
var uri = baseUri + src.scripts.join(options.splitter || ';'); | ||
var scriptTag = '<script type="text/javascript" src="' + uri + '"></script>'; | ||
var async = options.async || false; | ||
return scriptTag; | ||
}; | ||
if(async === true) { | ||
scriptTag = '<script type="text/javascript" src="' + uri + '" async="async"></script>'; | ||
} | ||
var genComboLinkUriTag = function() { | ||
var uri = baseUri + src.links.join(options.splitter || ';'); | ||
var linkTag = '<link rel="stylesheet" href="' + uri + '" />'; | ||
return linkTag; | ||
}; | ||
return scriptTag; | ||
}; | ||
scripts = $('script[src]'); | ||
links = $('link[rel="stylesheet"][href]'); | ||
var genComboLinkUriTag = function() { | ||
var uri = baseUri + src.links.join(options.splitter || ';'); | ||
var linkTag = '<link rel="stylesheet" href="' + uri + '" />'; | ||
return linkTag; | ||
}; | ||
scripts.each(function(i, script) { | ||
var src_attr = $(script).attr('src'); | ||
/* | ||
if(src_attr.match(/^http:\/\//igm)) { | ||
if(src_attr.match(/^http:\/\/mc.meituan.net\//igm)) { | ||
filteredScripts.push(script); | ||
} | ||
} else { | ||
filteredScripts.push(script); | ||
} | ||
}); | ||
scripts = $('script[src]'); | ||
links = $('link[rel="stylesheet"][href]'); | ||
links.each(function(i, link) { | ||
var href_attr = $(link).attr('href'); | ||
scripts.each(function(i, script) { | ||
var src_attr = $(script).attr('src'); | ||
if(href_attr.match(/^http:\/\//igm)) { | ||
if(href_attr.match(/^http:\/\/mc.meituan.net\//igm)) { | ||
filteredLinks.push(link); | ||
} | ||
} else { | ||
filteredLinks.push(link); | ||
} | ||
}); | ||
if(src_attr.match(/^http:\/\//igm)) { | ||
if(src_attr.match(/^http:\/\/mc.meituan.net\//igm)) { | ||
filteredScripts.push(script); | ||
} | ||
} else { | ||
filteredScripts.push(script); | ||
} | ||
}); | ||
filteredScripts.forEach(function(script, i) { | ||
var src_attr = $(script).attr('src'); | ||
links.each(function(i, link) { | ||
var href_attr = $(link).attr('href'); | ||
if(filteredScripts.length <= 1) { | ||
return; | ||
} | ||
src.scripts.push(src_attr.replace('http://mc.meituan.net/', '')); | ||
if(href_attr.match(/^http:\/\//igm)) { | ||
if(href_attr.match(/^http:\/\/mc.meituan.net\//igm)) { | ||
filteredLinks.push(link); | ||
} | ||
} else { | ||
filteredLinks.push(link); | ||
} | ||
}); | ||
if(i === scripts.length - 1) { | ||
$(script).replaceWith('<!--SCRIPT PLACEHOLDER-->'); | ||
} else { | ||
$(script).remove(); | ||
} | ||
}); | ||
filteredScripts.forEach(function(script, i) { | ||
var src_attr = $(script).attr('src'); | ||
filteredLinks.forEach(function(link, i) { | ||
var href_attr = $(link).attr('href'); | ||
if(filteredScripts.length <= 1) { | ||
return; | ||
} | ||
src.scripts.push(src_attr.replace('http://mc.meituan.net/', '')); | ||
if(filteredLinks.length <= 1) { | ||
return; | ||
} | ||
src.links.push(href_attr.replace('http://mc.meituan.net/', '')); | ||
if(i === scripts.length - 1) { | ||
$(script).replaceWith('<!--SCRIPT PLACEHOLDER-->'); | ||
} else { | ||
$(script).remove(); | ||
} | ||
}); | ||
if(i === links.length - 1) { | ||
$(link).replaceWith('<!--LINK PLACEHOLDER-->'); | ||
} else { | ||
$(link).remove(); | ||
} | ||
}); | ||
filteredLinks.forEach(function(link, i) { | ||
var href_attr = $(link).attr('href'); | ||
results = $.html(null, {decodeEntities: false}).replace('<!--SCRIPT PLACEHOLDER-->', genComboScriptUriTag()) | ||
.replace('<!--LINK PLACEHOLDER-->', genComboLinkUriTag()); | ||
if(filteredLinks.length <= 1) { | ||
return; | ||
} | ||
src.links.push(href_attr.replace('http://mc.meituan.net/', '')); | ||
file.contents = new Buffer(results); | ||
cb(null, file); | ||
}); | ||
if(i === links.length - 1) { | ||
$(link).replaceWith('<!--LINK PLACEHOLDER-->'); | ||
} else { | ||
$(link).remove(); | ||
} | ||
}); | ||
results = $.html(null, {decodeEntities: false}).replace('<!--SCRIPT PLACEHOLDER-->', genComboScriptUriTag()) | ||
.replace('<!--LINK PLACEHOLDER-->', genComboLinkUriTag()); | ||
file.contents = new Buffer(results); | ||
cb(null, file); | ||
*/ | ||
var scriptProcessor = function($, $1) { | ||
if($.match(/http:\/\//igm)) { | ||
if($1.match(/^http:\/\/mc.meituan.net\//igm)) { | ||
src.scripts.push($1.replace('http://mc.meituan.net/', '')); | ||
} else { | ||
return $; | ||
} | ||
} else { | ||
src.scripts.push($1.replace(/(.+\/)?[^\/]+\/\.\.\//igm, '$1')); | ||
} | ||
if(src.scripts.length === 1) { | ||
return '<%%%SCRIPT_HOLDER%%%>'; | ||
} | ||
return ''; | ||
}; | ||
var linkProcessor = function($, $1) { | ||
if($.match(/http:\/\//igm)) { | ||
if($1.match(/^http:\/\/mc.meituan.net\//igm)) { | ||
src.links.push($1.replace('http://mc.meituan.net/', '')); | ||
} else { | ||
return $; | ||
} | ||
} else { | ||
src.links.push($1.replace(/(.+\/)?[^\/]+\/\.\.\//igm, '$1')); | ||
} | ||
if(src.links.length === 1) { | ||
return '<%%%STYLES_HOLDER%%%>'; | ||
} | ||
return ''; | ||
}; | ||
chunk = chunk.replace(/<script[^>]+?src="([^"]+)"><\/script>/igm, scriptProcessor); | ||
chunk = chunk.replace(/<link[^>]+?href="([^"]+?)"[^>]+?rel="stylesheet"[^>]*>/igm, linkProcessor); | ||
chunk = chunk.replace(/<link[^>]+?rel="stylesheet"[^>]+?href="([^"]+?)"[^>]*>/igm, linkProcessor); | ||
chunk = chunk.replace('<%%%SCRIPT_HOLDER%%%>', genComboScriptUriTag()); | ||
chunk = chunk.replace('<%%%STYLES_HOLDER%%%>', genComboLinkUriTag()); | ||
file.contents = new Buffer(chunk); | ||
cb(null, file); | ||
}); | ||
}; |
{ | ||
"name": "gulp-combo", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "html scripts, links combo for gulp", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
8928
122