1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Minify node .html files before sending

This commit is contained in:
Nick O'Leary 2014-05-04 17:30:55 +01:00
parent ff49d2b217
commit da8ef7acc6
2 changed files with 39 additions and 5 deletions

View File

@ -38,7 +38,9 @@
"irc":"0.3.6", "irc":"0.3.6",
"follow-redirects":"0.0.3", "follow-redirects":"0.0.3",
"cors":"2.2.0", "cors":"2.2.0",
"mkdirp":"0.3.5" "mkdirp":"0.3.5",
"cheerio":"0.15.0",
"uglify-js":"2.4.13"
}, },
"devDependencies": { "devDependencies": {
"grunt": "0.4.4", "grunt": "0.4.4",

View File

@ -19,6 +19,9 @@ var when = require("when");
var whenNode = require('when/node'); var whenNode = require('when/node');
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
var cheerio = require("cheerio");
var UglifyJS = require("uglify-js");
var events = require("../events"); var events = require("../events");
var Node; var Node;
@ -26,11 +29,12 @@ var settings;
var node_types = {}; var node_types = {};
var node_configs = []; var node_configs = [];
var node_scripts = [];
function loadTemplate(templateFilename) { function loadTemplate(templateFilename) {
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
whenNode.call(fs.readFile,templateFilename,'utf8').done(function(content) { whenNode.call(fs.readFile,templateFilename,'utf8').done(function(content) {
typeRegistry.registerConfig(content); registerConfig(content);
resolve(); resolve();
}, function(err) { }, function(err) {
reject("missing template file"); reject("missing template file");
@ -232,6 +236,32 @@ function load() {
}); });
} }
function registerConfig(config) {
$ = cheerio.load(config);
var template = "";
$("*").each(function(i,el) {
if (el.type == "script" && el.attribs.type == "text/javascript") {
var content = el.children[0].data;
el.children[0].data = UglifyJS.minify(content, {fromString: true}).code;
node_scripts.push($(this).text());
} else if (el.name == "script" || el.name == "style") {
var openTag = "<"+el.name;
var closeTag = "</"+el.name+">";
if (el.attribs) {
for (var i in el.attribs) {
openTag += " "+i+'="'+el.attribs[i]+'"';
}
}
openTag += ">";
template += openTag+$(el).text()+closeTag;
}
});
node_configs.push(template);
}
var typeRegistry = module.exports = { var typeRegistry = module.exports = {
init:init, init:init,
load:load, load:load,
@ -240,9 +270,6 @@ var typeRegistry = module.exports = {
node_types[type] = node; node_types[type] = node;
events.emit("type-registered",type); events.emit("type-registered",type);
}, },
registerConfig: function(config) {
node_configs.push(config);
},
get: function(type) { get: function(type) {
return node_types[type]; return node_types[type];
}, },
@ -251,6 +278,11 @@ var typeRegistry = module.exports = {
for (var i=0;i<node_configs.length;i++) { for (var i=0;i<node_configs.length;i++) {
result += node_configs[i]; result += node_configs[i];
} }
result += '<script type="text/javascript">';
for (var i=0;i<node_scripts.length;i++) {
result += node_scripts[i];
}
result += '</script>';
return result; return result;
} }
} }