source map support, first pass

v1.18.x
Julian Lam 11 years ago
parent 5ae29ef33a
commit f8061e23ec

@ -1,3 +1,5 @@
"use strict";
var uglifyjs = require('uglify-js'),
less = require('less'),
async = require('async'),
@ -9,10 +11,21 @@ var uglifyjs = require('uglify-js'),
};
/* Javascript */
Minifier.js.minify = function (scripts, callback) {
Minifier.js.minify = function (scripts, minify, callback) {
var options = {};
if (!minify) {
options.outSourceMap = 'nodebb.min.js.map';
options.mangle = false;
options.compress = false;
}
try {
var minified = uglifyjs.minify(scripts);
callback(minified.code);
var minified = uglifyjs.minify(scripts, options);
callback({
js: minified.code,
map: minified.map
});
} catch(err) {
process.send({
action: 'error',
@ -21,22 +34,22 @@ Minifier.js.minify = function (scripts, callback) {
}
};
Minifier.js.concatenate = function(scripts, callback) {
async.map(scripts, function(path, next) {
fs.readFile(path, { encoding: 'utf-8' }, next);
}, function(err, contents) {
if (err) {
process.send({
action: 'error',
error: err
});
} else {
callback(contents.reduce(function(output, src) {
return output.length ? output + ';\n' + src : src;
}, ''));
}
});
};
// Minifier.js.concatenate = function(scripts, callback) {
// async.map(scripts, function(path, next) {
// fs.readFile(path, { encoding: 'utf-8' }, next);
// }, function(err, contents) {
// if (err) {
// process.send({
// action: 'error',
// error: err
// });
// } else {
// callback(contents.reduce(function(output, src) {
// return output.length ? output + ';\n' + src : src;
// }, ''));
// }
// });
// };
process.on('message', function(payload) {
var executeCallback = function(data) {
@ -47,12 +60,8 @@ process.on('message', function(payload) {
};
switch(payload.action) {
case 'js.minify':
Minifier.js.minify(payload.scripts, executeCallback);
break;
case 'js.concatenate':
Minifier.js.concatenate(payload.scripts, executeCallback);
case 'js':
Minifier.js.minify(payload.scripts, payload.minify, executeCallback);
break;
}
})
});

@ -300,7 +300,8 @@ var fs = require('fs'),
minifier.on('message', function(payload) {
if (payload.action !== 'error') {
winston.info('[meta/js] Compilation complete');
Meta.js.cache = payload.data;
Meta.js.cache = payload.data.js;
Meta.js.map = payload.data.map;
minifier.kill();
} else {
winston.error('[meta/js] Could not compile client-side scripts!');
@ -312,7 +313,8 @@ var fs = require('fs'),
this.prepare(function() {
minifier.send({
action: minify ? 'js.minify' : 'js.concatenate',
action: 'js',
minify: minify,
scripts: Meta.js.scripts
});
});

@ -11,18 +11,28 @@ var path = require('path'),
function sendMinifiedJS(req, res, next) {
if (!minificationEnabled) {
res.set('X-SourceMap', '/nodebb.min.js.map');
}
return res.type('text/javascript').send(meta.js.cache);
}
function sendSourceMap(req, res) {
return res.type('application/json').send(meta.js.map);
}
function sendStylesheet(req, res, next) {
res.type('text/css').send(200, meta.css.cache);
}
module.exports = function(app, middleware, controllers) {
minificationEnabled = app.enabled('minification');
app.get('/stylesheet.css', sendStylesheet);
app.get('/nodebb.min.js', sendMinifiedJS);
app.get('/sitemap.xml', controllers.sitemap);
app.get('/robots.txt', controllers.robots);
if (!minificationEnabled) {
app.get('/nodebb.min.js.map', sendSourceMap);
}
};

Loading…
Cancel
Save