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

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

@ -156,11 +156,8 @@ module.exports = function(Meta) {
minifier.on('message', function(message) {
switch(message.type) {
case 'end':
Meta.js.cache = message.data.js;
Meta.js.map = message.data.map;
Meta.js.cache = message.minified;
onComplete();
break;
case 'hash':
Meta.js.hash = message.payload;
@ -180,7 +177,6 @@ module.exports = function(Meta) {
Meta.js.prepare(function() {
minifier.send({
action: 'js',
relativePath: nconf.get('url') + '/',
minify: global.env !== 'development',
scripts: Meta.js.scripts.all
});
@ -200,11 +196,10 @@ module.exports = function(Meta) {
Meta.js.commitToFile = function() {
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.map'), Meta.js.map)
async.apply(fs.writeFile, path.join(__dirname, '../../public/nodebb.min.js'), Meta.js.cache)
], function (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');
} else {
winston.error('[meta/js] ' + err.message);

@ -14,10 +14,6 @@ function sendMinifiedJS(req, res, next) {
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').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);
}
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) {
app.get('/stylesheet.css', middleware.addExpiresHeaders, sendStylesheet);
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('/robots.txt', controllers.robots);
app.get('/css/previews/:theme', controllers.admin.themes.get);
app.get('/nodebb.min.js.map', middleware.addExpiresHeaders, sendSourceMap);
setupPluginSourceMapping(app);
};

Loading…
Cancel
Save