Skip to content

Instantly share code, notes, and snippets.

@nfroidure
Forked from 0gust1/gist:8432208
Last active January 4, 2016 01:49
Show Gist options
  • Save nfroidure/8550658 to your computer and use it in GitHub Desktop.
Save nfroidure/8550658 to your computer and use it in GitHub Desktop.
#!/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);
var promises;
promises = $('link[rel="stylesheet"]').map(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);
return 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>');
// });
});
promises = promises.concat($('script').map(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);
return getRessourceP(script).then(function(data) {
//processInput(data);
console.log("replacing JS :");
_this.replaceWith('<script>' + processJSInput(data) + '</script>');
});
}));
Deferred.all(promises).then(function() {
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