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:
parent
ff49d2b217
commit
da8ef7acc6
@ -38,7 +38,9 @@
|
||||
"irc":"0.3.6",
|
||||
"follow-redirects":"0.0.3",
|
||||
"cors":"2.2.0",
|
||||
"mkdirp":"0.3.5"
|
||||
"mkdirp":"0.3.5",
|
||||
"cheerio":"0.15.0",
|
||||
"uglify-js":"2.4.13"
|
||||
},
|
||||
"devDependencies": {
|
||||
"grunt": "0.4.4",
|
||||
|
@ -19,6 +19,9 @@ var when = require("when");
|
||||
var whenNode = require('when/node');
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var cheerio = require("cheerio");
|
||||
var UglifyJS = require("uglify-js");
|
||||
|
||||
var events = require("../events");
|
||||
|
||||
var Node;
|
||||
@ -26,11 +29,12 @@ var settings;
|
||||
|
||||
var node_types = {};
|
||||
var node_configs = [];
|
||||
var node_scripts = [];
|
||||
|
||||
function loadTemplate(templateFilename) {
|
||||
return when.promise(function(resolve,reject) {
|
||||
whenNode.call(fs.readFile,templateFilename,'utf8').done(function(content) {
|
||||
typeRegistry.registerConfig(content);
|
||||
registerConfig(content);
|
||||
resolve();
|
||||
}, function(err) {
|
||||
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 = {
|
||||
init:init,
|
||||
load:load,
|
||||
@ -240,9 +270,6 @@ var typeRegistry = module.exports = {
|
||||
node_types[type] = node;
|
||||
events.emit("type-registered",type);
|
||||
},
|
||||
registerConfig: function(config) {
|
||||
node_configs.push(config);
|
||||
},
|
||||
get: function(type) {
|
||||
return node_types[type];
|
||||
},
|
||||
@ -251,6 +278,11 @@ var typeRegistry = module.exports = {
|
||||
for (var i=0;i<node_configs.length;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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user