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'), var uglifyjs = require('uglify-js'),
less = require('less'), less = require('less'),
async = require('async'), async = require('async'),
@ -9,10 +11,21 @@ var uglifyjs = require('uglify-js'),
}; };
/* Javascript */ /* 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 { try {
var minified = uglifyjs.minify(scripts); var minified = uglifyjs.minify(scripts, options);
callback(minified.code); callback({
js: minified.code,
map: minified.map
});
} catch(err) { } catch(err) {
process.send({ process.send({
action: 'error', action: 'error',
@ -21,22 +34,22 @@ Minifier.js.minify = function (scripts, callback) {
} }
}; };
Minifier.js.concatenate = function(scripts, callback) { // Minifier.js.concatenate = function(scripts, callback) {
async.map(scripts, function(path, next) { // async.map(scripts, function(path, next) {
fs.readFile(path, { encoding: 'utf-8' }, next); // fs.readFile(path, { encoding: 'utf-8' }, next);
}, function(err, contents) { // }, function(err, contents) {
if (err) { // if (err) {
process.send({ // process.send({
action: 'error', // action: 'error',
error: err // error: err
}); // });
} else { // } else {
callback(contents.reduce(function(output, src) { // callback(contents.reduce(function(output, src) {
return output.length ? output + ';\n' + src : src; // return output.length ? output + ';\n' + src : src;
}, '')); // }, ''));
} // }
}); // });
}; // };
process.on('message', function(payload) { process.on('message', function(payload) {
var executeCallback = function(data) { var executeCallback = function(data) {
@ -47,12 +60,8 @@ process.on('message', function(payload) {
}; };
switch(payload.action) { switch(payload.action) {
case 'js.minify': case 'js':
Minifier.js.minify(payload.scripts, executeCallback); Minifier.js.minify(payload.scripts, payload.minify, executeCallback);
break;
case 'js.concatenate':
Minifier.js.concatenate(payload.scripts, executeCallback);
break; break;
} }
}) });

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

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

Loading…
Cancel
Save