Created
January 15, 2014 07:26
-
-
Save 0gust1/8432208 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
//module.exports = function(){ | |
var http = require("http"); | |
var optimist = require("optimist"); | |
var cheerio = require("cheerio"); | |
var path = require("path"); | |
var uglifyjs = require('uglifyjs'); | |
var uglifycss = require('uglifycss'); | |
var url = require('url'); | |
var Deferred = require("promised-io/promise").Deferred; | |
var when = require("promised-io/promise"); | |
var argv = require('optimist').argv; | |
//if (options.minify){ | |
var processJSInput = function processInput(input) { | |
return uglifyjs.minify(input, { | |
fromString: true | |
}).code; | |
}; | |
var processCSSInput = function processInput(input) { | |
return uglifycss.processString(input); | |
}; | |
// processInput = function processInput(i) { | |
// return i; | |
// }; | |
// } | |
var getRessource = function getRessource(url, callback) { | |
http.get(url, function(res) { | |
var data = ""; | |
res.on("data", function(chunk) { | |
data += chunk; | |
}); | |
res.on("end", function() { | |
callback(data, url); | |
}); | |
}).on("error", function() { | |
callback(null); | |
}); | |
}; | |
var getRessourceP = function getRessourceP(url){ | |
var deferred = new Deferred(); | |
http.get(url, function(res) { | |
var data = ""; | |
res.on("data", function(chunk) { | |
data += chunk; | |
}); | |
res.on("end", function() { | |
deferred.resolve(data); | |
}); | |
}).on("error", function(e) { | |
deferred.reject(e); | |
}); | |
return deferred.promise; | |
}; | |
var processContent = function processContent(data, baseUrl) { | |
//var deferred = new Deferred(); | |
var $ = cheerio.load(data); | |
$('link[rel="stylesheet"]').each(function() { | |
var _this = $(this); | |
var style = _this.attr('href'); | |
if (!style) { | |
return; | |
} | |
if (style.match(/^\/\//)) { | |
return; | |
} | |
if (url.parse(style).protocol) { | |
return; | |
} | |
style = url.resolve(baseUrl, style); | |
getRessourceP(style).then(function(data) { | |
console.log("replacing CSS :"); | |
_this.replaceWith('<style>' + processCSSInput(data) + '</style>'); | |
}); | |
// getRessource(style, function(data) { | |
// //processInput(data); | |
// console.log("replacing CSS :"); | |
// _this.replaceWith('<style>' + processCSSInput(data) + '</style>'); | |
// }); | |
}); | |
$('script').each(function() { | |
var _this = $(this); | |
var script = _this.attr('src'); | |
if (!script) { | |
return; | |
} | |
if (script.match(/^\/\//)) { | |
return; | |
} | |
if (url.parse(script).protocol) { | |
return; | |
} | |
script = url.resolve(baseUrl, script); | |
getRessourceP(script).then(function(data) { | |
//processInput(data); | |
console.log("replacing JS :"); | |
_this.replaceWith('<script>' + processJSInput(data) + '</script>'); | |
}); | |
}); | |
console.log($.html()); | |
}; | |
console.log(argv._); | |
argv._.forEach(function(element) { | |
getRessource(element, processContent); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment