v1.18.x
Julian Lam 10 years ago
parent b7c4647e9a
commit 8de2d1f58d

@ -12,41 +12,38 @@ var uglifyjs = require('uglify-js'),
}; };
/* Javascript */ /* Javascript */
Minifier.js.minify = function (scripts, relativePath, minify, callback) { Minifier.js.minify = function (scripts, minify, callback) {
var options = {
compress: false,
sourceMapURL: '/nodebb.min.js.map',
outSourceMap: 'nodebb.min.js.map',
sourceRoot: relativePath
};
scripts = scripts.filter(function(file) { scripts = scripts.filter(function(file) {
return fs.existsSync(file); return fs.existsSync(file);
}); });
if (minify) { if (minify) {
minifyScripts(scripts, options, callback); minifyScripts(scripts, function() {
callback.apply(this, arguments);
});
} else { } else {
concatenateScripts(scripts, options, callback); concatenateScripts(scripts, callback);
} }
}; };
process.on('message', function(payload) { process.on('message', function(payload) {
switch(payload.action) { switch(payload.action) {
case 'js': case 'js':
Minifier.js.minify(payload.scripts, payload.relativePath, payload.minify, function(data) { Minifier.js.minify(payload.scripts, payload.minify, function(minified) {
process.send({ process.send({
type: 'end', type: 'end',
data: data minified: minified
}); });
}); });
break; break;
} }
}); });
function minifyScripts(scripts, options, callback) { function minifyScripts(scripts, callback) {
try { try {
var minified = uglifyjs.minify(scripts, options), var minified = uglifyjs.minify(scripts, {
compress: false
}),
hasher = crypto.createHash('md5'), hasher = crypto.createHash('md5'),
hash; hash;
@ -58,19 +55,16 @@ function minifyScripts(scripts, options, callback) {
payload: hash.slice(0, 8) payload: hash.slice(0, 8)
}); });
callback({ callback(minified.code);
js: minified.code,
map: minified.map
});
} catch(err) { } catch(err) {
process.send({ process.send({
type: 'error', type: 'error',
payload: err payload: err.message
}); });
} }
} }
function concatenateScripts(scripts, options, callback) { function concatenateScripts(scripts, callback) {
async.map(scripts, fs.readFile, function(err, scripts) { async.map(scripts, fs.readFile, function(err, scripts) {
if (err) { if (err) {
process.send({ process.send({
@ -81,9 +75,6 @@ function concatenateScripts(scripts, options, callback) {
scripts = scripts.join(require('os').EOL + ';'); scripts = scripts.join(require('os').EOL + ';');
callback({ callback(scripts);
js: scripts,
map: ''
});
}); });
} }

@ -156,11 +156,8 @@ module.exports = function(Meta) {
minifier.on('message', function(message) { minifier.on('message', function(message) {
switch(message.type) { switch(message.type) {
case 'end': case 'end':
Meta.js.cache = message.data.js; Meta.js.cache = message.minified;
Meta.js.map = message.data.map;
onComplete(); onComplete();
break; break;
case 'hash': case 'hash':
Meta.js.hash = message.payload; Meta.js.hash = message.payload;
@ -180,7 +177,6 @@ module.exports = function(Meta) {
Meta.js.prepare(function() { Meta.js.prepare(function() {
minifier.send({ minifier.send({
action: 'js', action: 'js',
relativePath: nconf.get('url') + '/',
minify: global.env !== 'development', minify: global.env !== 'development',
scripts: Meta.js.scripts.all scripts: Meta.js.scripts.all
}); });
@ -200,11 +196,10 @@ module.exports = function(Meta) {
Meta.js.commitToFile = function() { Meta.js.commitToFile = function() {
async.parallel([ async.parallel([
async.apply(fs.writeFile, path.join(__dirname, '../../public/nodebb.min.js'), Meta.js.cache), async.apply(fs.writeFile, path.join(__dirname, '../../public/nodebb.min.js'), Meta.js.cache)
async.apply(fs.writeFile, path.join(__dirname, '../../public/nodebb.min.js.map'), Meta.js.map)
], function (err) { ], function (err) {
if (!err) { if (!err) {
winston.verbose('[meta/js] Client-side minfile and mapping committed to disk.'); winston.verbose('[meta/js] Client-side minfile committed to disk.');
emitter.emit('meta:js.compiled'); emitter.emit('meta:js.compiled');
} else { } else {
winston.error('[meta/js] ' + err.message); winston.error('[meta/js] ' + err.message);

@ -14,10 +14,6 @@ function sendMinifiedJS(req, res, next) {
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').status(200).send(meta.css.cache); res.type('text/css').status(200).send(meta.css.cache);
} }
@ -26,25 +22,6 @@ function sendACPStylesheet(req, res, next) {
res.type('text/css').status(200).send(meta.css.acpCache); res.type('text/css').status(200).send(meta.css.acpCache);
} }
function setupPluginSourceMapping(app) {
/*
These mappings are utilised by the source map file, as client-side
scripts defined in `scripts` in plugin.json are not normally
served to the end-user. These mappings are only accessible via
development mode (`./nodebb dev`)
*/
var routes = plugins.clientScripts,
prefix = __dirname.split(path.sep).length - 1,
mapping;
routes.forEach(function(route) {
mapping = '/' + route.split(path.sep).slice(prefix).join('/');
app.get(mapping, function(req, res) {
res.type('text/javascript').sendFile(route);
});
});
}
module.exports = function(app, middleware, controllers) { module.exports = function(app, middleware, controllers) {
app.get('/stylesheet.css', middleware.addExpiresHeaders, sendStylesheet); app.get('/stylesheet.css', middleware.addExpiresHeaders, sendStylesheet);
app.get('/admin.css', middleware.addExpiresHeaders, sendACPStylesheet); app.get('/admin.css', middleware.addExpiresHeaders, sendACPStylesheet);
@ -52,7 +29,4 @@ module.exports = function(app, middleware, controllers) {
app.get('/sitemap.xml', controllers.sitemap); app.get('/sitemap.xml', controllers.sitemap);
app.get('/robots.txt', controllers.robots); app.get('/robots.txt', controllers.robots);
app.get('/css/previews/:theme', controllers.admin.themes.get); app.get('/css/previews/:theme', controllers.admin.themes.get);
app.get('/nodebb.min.js.map', middleware.addExpiresHeaders, sendSourceMap);
setupPluginSourceMapping(app);
}; };

Loading…
Cancel
Save